2012-03-02 23:20:17 -05:00
|
|
|
module ActiveModel
|
2012-03-03 02:19:39 -05:00
|
|
|
|
2012-10-21 02:26:01 -04:00
|
|
|
# == Active \Model Basic \Model
|
2012-03-03 02:19:39 -05:00
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# Includes the required interface for an object to interact with
|
|
|
|
# <tt>ActionPack</tt>, using different <tt>ActiveModel</tt> modules.
|
|
|
|
# It includes model name introspections, conversions, translations and
|
|
|
|
# validations. Besides that, it allows you to initialize the object with a
|
|
|
|
# hash of attributes, pretty much like <tt>ActiveRecord</tt> does.
|
2012-03-03 02:19:39 -05:00
|
|
|
#
|
|
|
|
# A minimal implementation could be:
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :name, :age
|
|
|
|
# end
|
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# person = Person.new(name: 'bob', age: '18')
|
2012-03-03 02:19:39 -05:00
|
|
|
# person.name # => 'bob'
|
2012-06-23 16:12:33 -04:00
|
|
|
# person.age # => 18
|
2012-03-03 02:19:39 -05:00
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# Note that, by default, <tt>ActiveModel::Model</tt> implements <tt>persisted?</tt>
|
|
|
|
# to return +false+, which is the most common case. You may want to override
|
|
|
|
# it in your class to simulate a different scenario:
|
2012-03-03 02:19:39 -05:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :id, :name
|
|
|
|
#
|
|
|
|
# def persisted?
|
|
|
|
# self.id == 1
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# person = Person.new(id: 1, name: 'bob')
|
2012-03-03 02:19:39 -05:00
|
|
|
# person.persisted? # => true
|
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# Also, if for some reason you need to run code on <tt>initialize</tt>, make
|
|
|
|
# sure you call +super+ if you want the attributes hash initialization to
|
|
|
|
# happen.
|
2012-03-03 02:19:39 -05:00
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :id, :name, :omg
|
|
|
|
#
|
2012-04-14 03:35:44 -04:00
|
|
|
# def initialize(attributes={})
|
2012-03-03 02:19:39 -05:00
|
|
|
# super
|
|
|
|
# @omg ||= true
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# person = Person.new(id: 1, name: 'bob')
|
2012-03-03 02:19:39 -05:00
|
|
|
# person.omg # => true
|
|
|
|
#
|
2012-06-23 16:12:33 -04:00
|
|
|
# For more detailed information on other functionalities available, please
|
|
|
|
# refer to the specific modules included in <tt>ActiveModel::Model</tt>
|
|
|
|
# (see below).
|
2012-03-02 23:20:17 -05:00
|
|
|
module Model
|
2012-06-22 17:29:59 -04:00
|
|
|
def self.included(base) #:nodoc:
|
2012-03-02 23:20:17 -05:00
|
|
|
base.class_eval do
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
extend ActiveModel::Translation
|
|
|
|
include ActiveModel::Validations
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-23 16:12:33 -04:00
|
|
|
# Initializes a new model with the given +params+.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :name, :age
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new(name: 'bob', age: '18')
|
|
|
|
# person.name # => "bob"
|
|
|
|
# person.age # => 18
|
2012-03-02 23:20:17 -05:00
|
|
|
def initialize(params={})
|
|
|
|
params.each do |attr, value|
|
2012-03-03 10:36:03 -05:00
|
|
|
self.public_send("#{attr}=", value)
|
2012-03-02 23:20:17 -05:00
|
|
|
end if params
|
|
|
|
end
|
|
|
|
|
2012-06-23 16:12:33 -04:00
|
|
|
# Indicates if the model is persisted. Default is +false+.
|
|
|
|
#
|
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Model
|
|
|
|
# attr_accessor :id, :name
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# person = Person.new(id: 1, name: 'bob')
|
|
|
|
# person.persisted? # => false
|
2012-03-02 23:20:17 -05:00
|
|
|
def persisted?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|