Module: ActiveModel::Model
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
|
|
Instance Chain:
|
|
Defined in: | activemodel/lib/active_model/model.rb |
Overview
Active Model Basic Model
Includes the required interface for an object to interact with Action Pack and Action View, using different Active Model 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 Active Record does.
A minimal implementation could be:
class Person
include ActiveModel::Model
attr_accessor :name, :age
end
person = Person.new(name: 'bob', age: '18')
person.name # => "bob"
person.age # => "18"
Note that, by default, Model
implements #persisted? to return false
, which is the most common case. You may want to override it in your class to simulate a different scenario:
class Person
include ActiveModel::Model
attr_accessor :id, :name
def persisted?
self.id == 1
end
end
person = Person.new(id: 1, name: 'bob')
person.persisted? # => true
Also, if for some reason you need to run code on #initialize, make sure you call super
if you want the attributes hash initialization to happen.
class Person
include ActiveModel::Model
attr_accessor :id, :name, :omg
def initialize(attributes={})
super
@omg ||= true
end
end
person = Person.new(id: 1, name: 'bob')
person.omg # => true
For more detailed information on other functionalities available, please refer to the specific modules included in Model
(see below).
Validations
- Attributes & Methods
- ._validators rw
- #_validators readonly
- ._validators? ⇒ Boolean rw
- #_validators? ⇒ Boolean readonly
- #validation_context readonly
Class Method Summary
Naming
- self
param_key | Returns string to use for params names. |
plural | Returns the plural class name of a record or class. |
route_key | Returns string to use while generating route names. |
singular | Returns the singular class name of a record or class. |
singular_route_key | Returns string to use while generating route names. |
uncountable? | Identifies whether the class name of a record or class is uncountable. |
::ActiveSupport::Concern
- Extended
class_methods | Define class methods from given block. |
included | Evaluate given block in context of base class, so that you can write class macros here. |
prepended | Evaluate given block in context of base class, so that you can write class macros here. |
Instance Attribute Summary
-
#persisted? ⇒ Boolean
readonly
Indicates if the model is persisted.
AttributeAssignment
- Included
Instance Method Summary
-
#initialize(attributes = {})
Initializes a new model with the given
params
.
Conversion
- Included
#to_key | Returns an |
#to_model | If your object is already designed to implement all of the Active Model you can use the default |
#to_param | Returns a |
#to_partial_path | Returns a |
Validations
- Included
#errors | Returns the |
#invalid? | Performs the opposite of |
#read_attribute_for_validation | Hook method defining how an attribute value should be retrieved. |
#valid? | Runs all the specified validations and returns |
#validate | Alias for Validations#valid?. |
#validate! | Runs all the validations within the specified context. |
#validates_with | Passes the record off to the class or classes specified and allows them to add errors based on more complex conditions. |
#raise_validation_error |
AttributeAssignment
- Included
#assign_attributes | Allows you to set all the attributes by passing in a hash of attributes with keys matching the attribute names. |
DSL Calls
included
[ GitHub ]65 66 67 68
# File 'activemodel/lib/active_model/model.rb', line 65
included do extend ActiveModel::Naming extend ActiveModel::Translation end
Class Attribute Details
._validators (rw)
[ GitHub ]# File 'activemodel/lib/active_model/validations.rb', line 52
class_attribute :_validators, instance_writer: false, default: Hash.new { |h, k| h[k] = [] }
._validators? ⇒ Boolean
(rw)
[ GitHub ]
# File 'activemodel/lib/active_model/validations.rb', line 52
class_attribute :_validators, instance_writer: false, default: Hash.new { |h, k| h[k] = [] }
Instance Attribute Details
#_validators (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/validations.rb', line 52
class_attribute :_validators, instance_writer: false, default: Hash.new { |h, k| h[k] = [] }
#_validators? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activemodel/lib/active_model/validations.rb', line 52
class_attribute :_validators, instance_writer: false, default: Hash.new { |h, k| h[k] = [] }
#persisted? ⇒ Boolean
(readonly)
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
# File 'activemodel/lib/active_model/model.rb', line 95
def persisted? false end
#validation_context (rw)
[ GitHub ]# File 'activemodel/lib/active_model/validations.rb', line 48
attr_accessor :validation_context
Instance Method Details
#initialize(attributes = {})
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"
# File 'activemodel/lib/active_model/model.rb', line 80
def initialize(attributes = {}) assign_attributes(attributes) if attributes super() end