1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00
heartcombo--simple_form/test/support/models.rb

95 lines
2.2 KiB
Ruby
Raw Normal View History

2009-12-08 08:48:31 -05:00
require 'ostruct'
2009-12-10 21:18:14 -05:00
Column = Struct.new(:name, :type, :limit)
Association = Struct.new(:klass, :name, :macro, :options)
2009-12-10 21:18:14 -05:00
class Company < Struct.new(:id, :name)
def self.all(options={})
all = (1..3).map{|i| Company.new(i, "Company #{i}")}
return [all.first] if options[:conditions]
return [all.last] if options[:order]
return all[0..1] if options[:include]
return all[1..2] if options[:joins]
2009-12-10 21:18:14 -05:00
all
end
2010-01-09 23:12:57 -05:00
def new_record?
false
end
end
class Tag < Struct.new(:id, :name)
def self.all(options={})
(1..3).map{|i| Tag.new(i, "Tag #{i}")}
2009-12-08 08:48:31 -05:00
end
end
class User < OpenStruct
# Get rid of deprecation warnings
undef_method :id
2009-12-08 08:48:31 -05:00
2009-12-10 15:03:27 -05:00
def new_record!
@new_record = true
end
2009-12-08 08:48:31 -05:00
def new_record?
2009-12-10 15:03:27 -05:00
@new_record || false
2009-12-08 08:48:31 -05:00
end
2010-01-09 23:12:57 -05:00
def company_attributes=(foo)
end
2009-12-08 08:48:31 -05:00
def column_for_attribute(attribute)
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
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
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 15:03:27 -05:00
def self.human_name
"User"
end
2009-12-10 21:18:14 -05:00
def self.reflect_on_association(association)
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, {})
end
2009-12-10 21:18:14 -05:00
end
2009-12-08 13:38:03 -05:00
def errors
@errors ||= {
:name => "can't be blank",
:description => "must be longer than 15 characters",
:age => ["is not a number", "must be greater than 18"],
2009-12-10 22:18:56 -05:00
:company => "company must be present",
:company_id => "must be valid"
2009-12-08 13:38:03 -05:00
}
end
2009-12-08 08:48:31 -05:00
end