2012-03-02 23:20:17 -05:00
|
|
|
module ActiveModel
|
2012-03-03 02:19:39 -05:00
|
|
|
|
2013-12-25 08:20:44 -05: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')
|
2014-06-13 12:56:55 -04:00
|
|
|
# person.name # => "bob"
|
|
|
|
# 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
|
2014-08-24 17:51:46 -04:00
|
|
|
extend ActiveSupport::Concern
|
2015-01-23 17:36:43 -05:00
|
|
|
include ActiveModel::AttributeAssignment
|
2014-08-24 17:51:46 -04:00
|
|
|
include ActiveModel::Validations
|
|
|
|
include ActiveModel::Conversion
|
|
|
|
|
|
|
|
included do
|
|
|
|
extend ActiveModel::Naming
|
|
|
|
extend ActiveModel::Translation
|
2012-03-02 23:20:17 -05:00
|
|
|
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"
|
2014-06-13 12:56:55 -04:00
|
|
|
# person.age # => "18"
|
2015-01-23 17:36:43 -05:00
|
|
|
def initialize(attributes={})
|
|
|
|
assign_attributes(attributes) if attributes
|
2013-06-18 21:21:35 -04:00
|
|
|
|
2013-06-19 22:16:17 -04:00
|
|
|
super()
|
2012-03-02 23:20:17 -05:00
|
|
|
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
|