Module: ActiveRecord::AttributeMethods::BeforeTypeCast
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::Concern
|
|
Defined in: | activerecord/lib/active_record/attribute_methods/before_type_cast.rb |
Overview
BeforeTypeCast
provides a way to read the value of the attributes before typecasting and deserialization.
class Task < ActiveRecord::Base
end
task = Task.new(id: '1', completed_on: '2012-10-21')
task.id # => 1
task.completed_on # => Sun, 21 Oct 2012
task.attributes_before_type_cast
# => {"id"=>"1", "completed_on"=>"2012-10-21", ... }
task.read_attribute_before_type_cast('id') # => "1"
task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"
In addition to #read_attribute_before_type_cast and #attributes_before_type_cast, it declares a method for all attributes with the *_before_type_cast
suffix.
task.id_before_type_cast # => "1"
task.completed_on_before_type_cast # => "2012-10-21"
Class Method Summary
::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. |
append_features, prepend_features |
Instance Method Summary
-
#attributes_before_type_cast
Returns a hash of attributes before typecasting and deserialization.
-
#attributes_for_database
Returns a hash of attributes for assignment to the database.
-
#read_attribute_before_type_cast(attr_name)
Returns the value of the attribute identified by
attr_name
before typecasting and deserialization. -
#read_attribute_for_database(attr_name)
Returns the value of the attribute identified by
attr_name
after serialization. -
#attribute_before_type_cast(attr_name)
private
Dispatch target for
*_before_type_cast
attribute methods. - #attribute_came_from_user?(attr_name) ⇒ Boolean private
- #attribute_for_database(attr_name) private
DSL Calls
included
[ GitHub ]31 32 33 34
# File 'activerecord/lib/active_record/attribute_methods/before_type_cast.rb', line 31
included do attribute_method_suffix "_before_type_cast", "_for_database", parameters: false attribute_method_suffix "_came_from_user?", parameters: false end
Instance Method Details
#attribute_before_type_cast(attr_name) (private)
Dispatch target for *_before_type_cast
attribute methods.
# File 'activerecord/lib/active_record/attribute_methods/before_type_cast.rb', line 93
def attribute_before_type_cast(attr_name) @attributes[attr_name].value_before_type_cast end
#attribute_came_from_user?(attr_name) ⇒ Boolean
(private)
# File 'activerecord/lib/active_record/attribute_methods/before_type_cast.rb', line 101
def attribute_came_from_user?(attr_name) @attributes[attr_name].came_from_user? end
#attribute_for_database(attr_name) (private)
[ GitHub ]# File 'activerecord/lib/active_record/attribute_methods/before_type_cast.rb', line 97
def attribute_for_database(attr_name) @attributes[attr_name].value_for_database end
#attributes_before_type_cast
Returns a hash of attributes before typecasting and deserialization.
class Task < ActiveRecord::Base
end
task = Task.new(title: nil, is_done: true, completed_on: '2012-10-21')
task.attributes
# => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>Sun, 21 Oct 2012, "created_at"=>nil, "updated_at"=>nil}
task.attributes_before_type_cast
# => {"id"=>nil, "title"=>nil, "is_done"=>true, "completed_on"=>"2012-10-21", "created_at"=>nil, "updated_at"=>nil}
# File 'activerecord/lib/active_record/attribute_methods/before_type_cast.rb', line 82
def attributes_before_type_cast @attributes.values_before_type_cast end
#attributes_for_database
Returns a hash of attributes for assignment to the database.
# File 'activerecord/lib/active_record/attribute_methods/before_type_cast.rb', line 87
def attributes_for_database @attributes.values_for_database end
#read_attribute_before_type_cast(attr_name)
Returns the value of the attribute identified by attr_name
before typecasting and deserialization.
class Task < ActiveRecord::Base
end
task = Task.new(id: '1', completed_on: '2012-10-21')
task.read_attribute('id') # => 1
task.read_attribute_before_type_cast('id') # => '1'
task.read_attribute('completed_on') # => Sun, 21 Oct 2012
task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"
task.read_attribute_before_type_cast(:completed_on) # => "2012-10-21"
# File 'activerecord/lib/active_record/attribute_methods/before_type_cast.rb', line 48
def read_attribute_before_type_cast(attr_name) name = attr_name.to_s name = self.class.attribute_aliases[name] || name attribute_before_type_cast(name) end
#read_attribute_for_database(attr_name)
Returns the value of the attribute identified by attr_name
after serialization.
class Book < ActiveRecord::Base
enum :status, { draft: 1, published: 2 }
end
book = Book.new(status: "published")
book.read_attribute(:status) # => "published"
book.read_attribute_for_database(:status) # => 2
# File 'activerecord/lib/active_record/attribute_methods/before_type_cast.rb', line 65
def read_attribute_for_database(attr_name) name = attr_name.to_s name = self.class.attribute_aliases[name] || name attribute_for_database(name) end