lib/her/model/attributes.rb
# Assign new attributes to a resource
#
# @example
#   class User
#     include Her::Model
#   end
#
#   user = User.find(1) # => #<User id=1 name="Tobias">
#   user.assign_attributes(name: "Lindsay")
#   user.changes # => { :name => ["Tobias", "Lindsay"] }
Assign new attributes to a resource
Assign new attributes to a resource
def assign_attributes(new_attributes)
        if !new_attributes.respond_to?(:to_hash)
          raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
        end
        new_attributes = new_attributes.to_hash
        @_her_attributes ||= attributes
        unset_attributes = self.class.use_setter_methods(self, new_attributes)
        associations = self.class.parse_associations(unset_attributes)
        @_her_attributes.merge!(associations)
      end
def assign_attributes ( new_attributes ) if ! new_attributes . respond_to? ( :to_hash ) raise ArgumentError , "When assigning attributes, you must pass a hash as an argument." end new_attributes = new_attributes . to_hash @_her_attributes ||= attributes unset_attributes = self . class . use_setter_methods ( self , new_attributes ) associations = self . class . parse_associations ( unset_attributes ) @_her_attributes . merge! ( associations ) end
Assign new attributes to a resource
Assign new attributes to a resource
# Coerce new_attributes to hash in case of strong parameters # Use setter methods first # Then translate attributes of associations into association instances # Then merge the associations into @_her_attributes.
new_attributes
<not_specific>
example
class User include Her::Model end user = User.find(1) # => # user.assign_attributes(name: "Lindsay") user.changes # => { :name => ["Tobias", "Lindsay"] }
class User include Her :: Model end user = User . find ( 1 ) # = > # user . assign_attributes ( name : " Lindsay " ) user . changes # = > { : name = > [ " Tobias " " Lindsay " ] }