2009-12-08 13:48:31 +00:00
|
|
|
require 'ostruct'
|
|
|
|
|
2009-12-09 13:12:23 +00:00
|
|
|
class Column
|
2009-12-10 13:57:05 +00:00
|
|
|
attr_accessor :name, :type, :limit#, :precision, :scale
|
2009-12-09 13:12:23 +00:00
|
|
|
|
|
|
|
def initialize(attrs={})
|
2009-12-10 13:57:05 +00:00
|
|
|
self.name = attrs[:name]
|
|
|
|
self.type = attrs[:type]
|
|
|
|
self.limit = attrs[:limit]
|
2009-12-09 13:12:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-08 13:48:31 +00:00
|
|
|
class User < OpenStruct
|
2009-12-10 02:22:53 +00:00
|
|
|
attr_reader :id
|
2009-12-08 13:48:31 +00:00
|
|
|
|
2009-12-10 02:22:53 +00:00
|
|
|
def initialize(attributes={})
|
|
|
|
@id = attributes.delete(:id)
|
|
|
|
super
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|
|
|
|
|
2009-12-10 20:03:27 +00:00
|
|
|
def new_record!
|
|
|
|
@new_record = true
|
|
|
|
end
|
|
|
|
|
2009-12-08 13:48:31 +00:00
|
|
|
def new_record?
|
2009-12-10 20:03:27 +00:00
|
|
|
@new_record || false
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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]
|
2009-12-08 13:48:31 +00:00
|
|
|
when :description then :text
|
|
|
|
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
|
|
|
|
end
|
2009-12-10 13:57:05 +00:00
|
|
|
Column.new(:name => attribute, :type => column_type, :limit => 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
|
|
|
|
when 'name' then 'Super User Name!'
|
2009-12-09 22:36:21 +00:00
|
|
|
when 'description' then 'User Description!'
|
|
|
|
else nil
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|
|
|
|
end
|
2009-12-08 18:38:03 +00:00
|
|
|
|
2009-12-10 20:03:27 +00:00
|
|
|
def self.human_name
|
|
|
|
"User"
|
|
|
|
end
|
|
|
|
|
2009-12-08 18:38:03 +00: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"],
|
|
|
|
}
|
|
|
|
end
|
2009-12-08 13:48:31 +00:00
|
|
|
end
|