2009-12-08 08:48:31 -05:00
|
|
|
require 'ostruct'
|
|
|
|
|
2009-12-10 21:18:14 -05:00
|
|
|
Column = Struct.new(:name, :type, :limit)
|
2009-12-11 07:43:43 -05:00
|
|
|
Association = Struct.new(:klass, :name, :macro, :options)
|
2009-12-09 08:12:23 -05:00
|
|
|
|
2009-12-10 21:18:14 -05:00
|
|
|
class Company < Struct.new(:id, :name)
|
2010-02-06 14:41:35 -05:00
|
|
|
extend ActiveModel::Naming
|
2010-08-31 04:19:10 -04:00
|
|
|
include ActiveModel::Conversion
|
2010-02-06 14:41:35 -05:00
|
|
|
|
2009-12-10 21:18:14 -05:00
|
|
|
def self.all(options={})
|
|
|
|
all = (1..3).map{|i| Company.new(i, "Company #{i}")}
|
2010-01-10 05:02:15 -05: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-10 21:18:14 -05:00
|
|
|
all
|
2009-12-09 08:12:23 -05:00
|
|
|
end
|
2010-01-09 23:12:57 -05:00
|
|
|
|
2010-01-10 05:02:15 -05:00
|
|
|
def self.merge_conditions(a, b)
|
|
|
|
(a || {}).merge(b || {})
|
|
|
|
end
|
|
|
|
|
2010-03-05 18:39:42 -05:00
|
|
|
def persisted?
|
|
|
|
true
|
2010-01-09 23:12:57 -05:00
|
|
|
end
|
2009-12-09 08:12:23 -05:00
|
|
|
end
|
|
|
|
|
2010-01-10 05:02:15 -05:00
|
|
|
class Tag < Company
|
2010-02-06 14:41:35 -05:00
|
|
|
extend ActiveModel::Naming
|
2010-08-31 04:19:10 -04:00
|
|
|
include ActiveModel::Conversion
|
2010-02-06 14:41:35 -05:00
|
|
|
|
2009-12-11 07:43:43 -05:00
|
|
|
def self.all(options={})
|
|
|
|
(1..3).map{|i| Tag.new(i, "Tag #{i}")}
|
2009-12-08 08:48:31 -05:00
|
|
|
end
|
2009-12-11 07:43:43 -05:00
|
|
|
end
|
|
|
|
|
2010-08-31 04:19:10 -04:00
|
|
|
class User
|
2010-02-06 14:41:35 -05:00
|
|
|
extend ActiveModel::Naming
|
2010-08-31 04:19:10 -04:00
|
|
|
include ActiveModel::Conversion
|
2010-02-06 14:41:35 -05:00
|
|
|
|
2010-08-31 04:19:10 -04:00
|
|
|
attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :description, :created_at, :updated_at,
|
|
|
|
:credit_limit, :age, :password, :delivery_time, :born_at, :special_company_id, :country, :url, :tag_ids,
|
|
|
|
:avatar, :email, :status, :residence_country
|
|
|
|
|
|
|
|
def initialize(options={})
|
|
|
|
options.each do |key, value|
|
|
|
|
send("#{key}=", value)
|
|
|
|
end if options
|
|
|
|
end
|
2009-12-08 08:48:31 -05:00
|
|
|
|
2009-12-10 15:03:27 -05:00
|
|
|
def new_record!
|
|
|
|
@new_record = true
|
|
|
|
end
|
|
|
|
|
2010-03-05 18:39:42 -05:00
|
|
|
def persisted?
|
|
|
|
!(@new_record || false)
|
2009-12-08 08:48:31 -05:00
|
|
|
end
|
|
|
|
|
2010-02-06 14:41:35 -05:00
|
|
|
def company_attributes=(*)
|
2010-01-09 23:12:57 -05:00
|
|
|
end
|
|
|
|
|
2009-12-08 08:48:31 -05:00
|
|
|
def column_for_attribute(attribute)
|
2009-12-10 08:57:05 -05:00
|
|
|
column_type, limit = case attribute.to_sym
|
|
|
|
when :name, :status, :password then [:string, 100]
|
2009-12-08 08:48:31 -05:00
|
|
|
when :description then :text
|
|
|
|
when :age then :integer
|
2009-12-10 08:57:05 -05:00
|
|
|
when :credit_limit then [:decimal, 15]
|
2009-12-08 08:48:31 -05: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
|
|
|
|
end
|
2009-12-10 21:18:14 -05:00
|
|
|
Column.new(attribute, column_type, limit)
|
2009-12-08 08:48:31 -05:00
|
|
|
end
|
|
|
|
|
2009-12-10 15:03:27 -05:00
|
|
|
def self.human_attribute_name(attribute)
|
2009-12-08 08:48:31 -05:00
|
|
|
case attribute
|
2009-12-10 23:00:37 -05:00
|
|
|
when 'name'
|
|
|
|
'Super User Name!'
|
|
|
|
when 'description'
|
|
|
|
'User Description!'
|
|
|
|
when 'company'
|
|
|
|
'Company Human Name!'
|
|
|
|
else
|
|
|
|
attribute.humanize
|
2009-12-08 08:48:31 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 13:38:03 -05:00
|
|
|
|
2009-12-10 21:18:14 -05:00
|
|
|
def self.reflect_on_association(association)
|
2009-12-11 07:43:43 -05: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 05:02:15 -05:00
|
|
|
when :special_company
|
|
|
|
Association.new(Company, association, :belongs_to, { :conditions => { :id => 1 } })
|
2009-12-11 07:43:43 -05:00
|
|
|
end
|
2009-12-10 21:18:14 -05:00
|
|
|
end
|
|
|
|
|
2009-12-08 13:38:03 -05:00
|
|
|
def errors
|
2010-07-06 05:38:35 -04:00
|
|
|
@errors ||= begin
|
|
|
|
hash = Hash.new { |h,k| h[k] = [] }
|
|
|
|
hash.merge!(
|
|
|
|
: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"]
|
|
|
|
)
|
|
|
|
end
|
2009-12-08 13:38:03 -05:00
|
|
|
end
|
2009-12-08 08:48:31 -05:00
|
|
|
end
|
2010-05-14 17:58:36 -04:00
|
|
|
|
|
|
|
class ValidatingUser < User
|
|
|
|
include ActiveModel::Validations
|
|
|
|
validates :name, :presence => true
|
2010-08-06 13:52:19 -04:00
|
|
|
validates :company, :presence => true
|
2010-09-05 11:32:13 -04:00
|
|
|
validates_numericality_of :age,
|
|
|
|
:greater_than_or_equal_to => 18,
|
|
|
|
:less_than_or_equal_to => 99,
|
|
|
|
:only_integer => true
|
2010-05-14 17:58:36 -04:00
|
|
|
end
|