2009-12-11 12:43:43 +00:00
|
|
|
Association = Struct.new(:klass, :name, :macro, :options)
|
2009-12-09 13:12:23 +00:00
|
|
|
|
2012-04-04 19:05:01 +00:00
|
|
|
class Column < Struct.new(:name, :type, :limit)
|
|
|
|
# Returns +true+ if the column is either of type integer, float or decimal.
|
|
|
|
def number?
|
|
|
|
type == :integer || type == :float || type == :decimal
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-11 02:18:14 +00:00
|
|
|
class Company < Struct.new(:id, :name)
|
2010-02-06 19:41:35 +00:00
|
|
|
extend ActiveModel::Naming
|
2010-08-31 08:19:10 +00:00
|
|
|
include ActiveModel::Conversion
|
2010-02-06 19:41:35 +00:00
|
|
|
|
2009-12-11 02:18:14 +00:00
|
|
|
def self.all(options={})
|
|
|
|
all = (1..3).map{|i| Company.new(i, "Company #{i}")}
|
2010-01-10 10:02:15 +00:00
|
|
|
return [all.first] if options[:conditions].present?
|
|
|
|
return [all.last] if options[:order].present?
|
|
|
|
return all[0..1] if options[:include].present?
|
|
|
|
return all[1..2] if options[:joins].present?
|
2009-12-11 02:18:14 +00:00
|
|
|
all
|
2009-12-09 13:12:23 +00:00
|
|
|
end
|
2010-01-10 04:12:57 +00:00
|
|
|
|
2010-01-10 10:02:15 +00:00
|
|
|
def self.merge_conditions(a, b)
|
|
|
|
(a || {}).merge(b || {})
|
|
|
|
end
|
|
|
|
|
2010-03-05 23:39:42 +00:00
|
|
|
def persisted?
|
|
|
|
true
|
2010-01-10 04:12:57 +00:00
|
|
|
end
|
2009-12-09 13:12:23 +00:00
|
|
|
end
|
|
|
|
|
2010-01-10 10:02:15 +00:00
|
|
|
class Tag < Company
|
2009-12-11 12:43:43 +00:00
|
|
|
def self.all(options={})
|
|
|
|
(1..3).map{|i| Tag.new(i, "Tag #{i}")}
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|
2009-12-11 12:43:43 +00:00
|
|
|
end
|
|
|
|
|
2012-01-24 13:48:38 +00:00
|
|
|
class TagGroup < Struct.new(:id, :name, :tags)
|
|
|
|
end
|
|
|
|
|
2010-08-31 08:19:10 +00:00
|
|
|
class User
|
2010-02-06 19:41:35 +00:00
|
|
|
extend ActiveModel::Naming
|
2010-08-31 08:19:10 +00:00
|
|
|
include ActiveModel::Conversion
|
2010-02-06 19:41:35 +00:00
|
|
|
|
2011-05-17 02:17:09 +00:00
|
|
|
attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :age,
|
|
|
|
:description, :created_at, :updated_at, :credit_limit, :password, :url,
|
|
|
|
:delivery_time, :born_at, :special_company_id, :country, :tags, :tag_ids,
|
|
|
|
:avatar, :home_picture, :email, :status, :residence_country, :phone_number,
|
2012-01-24 16:52:00 +00:00
|
|
|
:post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender
|
2010-08-31 08:19:10 +00:00
|
|
|
|
|
|
|
def initialize(options={})
|
2011-12-17 10:57:43 +00:00
|
|
|
@new_record = false
|
2010-08-31 08:19:10 +00:00
|
|
|
options.each do |key, value|
|
|
|
|
send("#{key}=", value)
|
|
|
|
end if options
|
|
|
|
end
|
2009-12-08 13:48:31 +00:00
|
|
|
|
2009-12-10 20:03:27 +00:00
|
|
|
def new_record!
|
|
|
|
@new_record = true
|
|
|
|
end
|
|
|
|
|
2010-03-05 23:39:42 +00:00
|
|
|
def persisted?
|
2011-12-17 10:57:43 +00:00
|
|
|
!@new_record
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|
|
|
|
|
2010-02-06 19:41:35 +00:00
|
|
|
def company_attributes=(*)
|
2010-01-10 04:12:57 +00:00
|
|
|
end
|
|
|
|
|
2011-05-03 19:33:48 +00:00
|
|
|
def tags_attributes=(*)
|
|
|
|
end
|
|
|
|
|
2009-12-08 13:48:31 +00:00
|
|
|
def column_for_attribute(attribute)
|
2009-12-10 13:57:05 +00:00
|
|
|
column_type, limit = case attribute.to_sym
|
|
|
|
when :name, :status, :password then [:string, 100]
|
2011-08-30 23:51:53 +00:00
|
|
|
when :description then [:text, 200]
|
2009-12-08 13:48:31 +00:00
|
|
|
when :age then :integer
|
2009-12-10 13:57:05 +00:00
|
|
|
when :credit_limit then [:decimal, 15]
|
2009-12-08 13:48:31 +00:00
|
|
|
when :active then :boolean
|
|
|
|
when :born_at then :date
|
|
|
|
when :delivery_time then :time
|
|
|
|
when :created_at then :datetime
|
|
|
|
when :updated_at then :timestamp
|
2011-02-03 16:06:33 +00:00
|
|
|
when :lock_version then :integer
|
2011-02-07 11:12:56 +00:00
|
|
|
when :home_picture then :string
|
2011-03-11 02:54:59 +00:00
|
|
|
when :amount then :integer
|
2011-03-12 04:12:01 +00:00
|
|
|
when :attempts then :integer
|
2011-08-31 02:47:55 +00:00
|
|
|
when :action then :string
|
2011-12-05 01:41:54 +00:00
|
|
|
when :credit_card then :string
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|
2009-12-11 02:18:14 +00:00
|
|
|
Column.new(attribute, column_type, limit)
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|
|
|
|
|
2009-12-10 20:03:27 +00:00
|
|
|
def self.human_attribute_name(attribute)
|
2009-12-08 13:48:31 +00:00
|
|
|
case attribute
|
2009-12-11 04:00:37 +00:00
|
|
|
when 'name'
|
|
|
|
'Super User Name!'
|
|
|
|
when 'description'
|
|
|
|
'User Description!'
|
|
|
|
when 'company'
|
|
|
|
'Company Human Name!'
|
|
|
|
else
|
|
|
|
attribute.humanize
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 18:38:03 +00:00
|
|
|
|
2009-12-11 02:18:14 +00:00
|
|
|
def self.reflect_on_association(association)
|
2009-12-11 12:43:43 +00:00
|
|
|
case association
|
|
|
|
when :company
|
|
|
|
Association.new(Company, association, :belongs_to, {})
|
|
|
|
when :tags
|
|
|
|
Association.new(Tag, association, :has_many, {})
|
|
|
|
when :first_company
|
|
|
|
Association.new(Company, association, :has_one, {})
|
2010-01-10 10:02:15 +00:00
|
|
|
when :special_company
|
2013-01-28 21:02:59 +00:00
|
|
|
Association.new(Company, association, :belongs_to, { conditions: { id: 1 } })
|
2009-12-11 12:43:43 +00:00
|
|
|
end
|
2009-12-11 02:18:14 +00:00
|
|
|
end
|
|
|
|
|
2009-12-08 18:38:03 +00:00
|
|
|
def errors
|
2010-07-06 09:38:35 +00:00
|
|
|
@errors ||= begin
|
|
|
|
hash = Hash.new { |h,k| h[k] = [] }
|
|
|
|
hash.merge!(
|
2013-01-28 21:02:59 +00:00
|
|
|
name: ["can't be blank"],
|
|
|
|
description: ["must be longer than 15 characters"],
|
|
|
|
age: ["is not a number", "must be greater than 18"],
|
|
|
|
company: ["company must be present"],
|
|
|
|
company_id: ["must be valid"]
|
2010-07-06 09:38:35 +00:00
|
|
|
)
|
|
|
|
end
|
2009-12-08 18:38:03 +00:00
|
|
|
end
|
2011-12-05 01:41:54 +00:00
|
|
|
|
|
|
|
def self.readonly_attributes
|
2012-06-21 13:29:28 +00:00
|
|
|
["credit_card"]
|
2011-12-05 01:41:54 +00:00
|
|
|
end
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|
2010-05-14 21:58:36 +00:00
|
|
|
|
|
|
|
class ValidatingUser < User
|
|
|
|
include ActiveModel::Validations
|
2013-01-28 21:02:59 +00:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :company, presence: true
|
|
|
|
validates :age, presence: true, if: Proc.new { |user| user.name }
|
|
|
|
validates :amount, presence: true, unless: Proc.new { |user| user.age }
|
2011-09-03 10:00:23 +00:00
|
|
|
|
2013-01-28 21:02:59 +00:00
|
|
|
validates :action, presence: true, on: :create
|
|
|
|
validates :credit_limit, presence: true, on: :save
|
|
|
|
validates :phone_number, presence: true, on: :update
|
2011-09-03 10:00:23 +00:00
|
|
|
|
2010-09-05 15:32:13 +00:00
|
|
|
validates_numericality_of :age,
|
2013-01-28 21:02:59 +00:00
|
|
|
greater_than_or_equal_to: 18,
|
|
|
|
less_than_or_equal_to: 99,
|
|
|
|
only_integer: true
|
2011-03-11 02:54:59 +00:00
|
|
|
validates_numericality_of :amount,
|
2013-01-28 21:02:59 +00:00
|
|
|
greater_than: :min_amount,
|
|
|
|
less_than: :max_amount,
|
|
|
|
only_integer: true
|
2011-03-12 04:12:01 +00:00
|
|
|
validates_numericality_of :attempts,
|
2013-01-28 21:02:59 +00:00
|
|
|
greater_than_or_equal_to: :min_attempts,
|
|
|
|
less_than_or_equal_to: :max_attempts,
|
|
|
|
only_integer: true
|
|
|
|
validates_length_of :name, maximum: 25
|
|
|
|
validates_length_of :description, maximum: 50
|
|
|
|
validates_length_of :action, maximum: 10, tokenizer: lambda { |str| str.scan(/\w+/) }
|
|
|
|
validates_length_of :home_picture, is: 12
|
2011-03-11 02:54:59 +00:00
|
|
|
|
|
|
|
def min_amount
|
|
|
|
10
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_amount
|
|
|
|
100
|
|
|
|
end
|
2011-03-12 04:12:01 +00:00
|
|
|
|
|
|
|
def min_attempts
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_attempts
|
|
|
|
100
|
|
|
|
end
|
2010-05-14 21:58:36 +00:00
|
|
|
end
|
2010-09-05 16:46:02 +00:00
|
|
|
|
|
|
|
class OtherValidatingUser < User
|
|
|
|
include ActiveModel::Validations
|
|
|
|
validates_numericality_of :age,
|
2013-01-28 21:02:59 +00:00
|
|
|
greater_than: 17,
|
|
|
|
less_than: 100,
|
|
|
|
only_integer: true
|
2011-03-11 02:54:59 +00:00
|
|
|
validates_numericality_of :amount,
|
2013-01-28 21:02:59 +00:00
|
|
|
greater_than: Proc.new { |user| user.age },
|
|
|
|
less_than: Proc.new { |user| user.age + 100},
|
|
|
|
only_integer: true
|
2011-03-12 04:12:01 +00:00
|
|
|
validates_numericality_of :attempts,
|
2013-01-28 21:02:59 +00:00
|
|
|
greater_than_or_equal_to: Proc.new { |user| user.age },
|
|
|
|
less_than_or_equal_to: Proc.new { |user| user.age + 100},
|
|
|
|
only_integer: true
|
2011-08-31 00:16:00 +00:00
|
|
|
|
2013-01-28 21:02:59 +00:00
|
|
|
validates_format_of :country, with: /\w+/
|
|
|
|
validates_format_of :name, with: Proc.new { /\w+/ }
|
2013-02-28 06:57:57 +00:00
|
|
|
validates_format_of :description, without: /\d+/
|
2010-09-05 16:46:02 +00:00
|
|
|
end
|
2011-07-19 15:25:40 +00:00
|
|
|
|
|
|
|
class HashBackedAuthor < Hash
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
|
|
|
|
def persisted?; false; end
|
|
|
|
|
|
|
|
def name
|
|
|
|
'hash backed author'
|
|
|
|
end
|
|
|
|
end
|