module DatastaxRails::AttributeAssignment
Model.new expects that a hash of attributes will be passed in and expects them to be set on the new instance. This module takes care of that while also delegating nested attribute assignment to the respective models.
Public Instance Methods
assign_attributes(new_attributes)
click to toggle source
Allows you to set all the attributes by passing in a hash of attributes with keys matching the attribute names (which again matches the column names).
If the passed hash responds to permitted?
method and the return value of this method is false
an ActiveModel::ForbiddenAttributesError
exception is raised. XXX: Is the above statement still true?
# File lib/datastax_rails/attribute_assignment.rb, line 18 def assign_attributes(new_attributes) return if new_attributes.blank? attributes = new_attributes.stringify_keys nested_parameter_attributes = [] attributes = sanitize_for_mass_assignment(attributes) attributes.each do |k, v| if v.is_a?(Hash) nested_parameter_attributes << [k, v] else _assign_attribute(k, v) end end assign_nested_parameter_attributes(nested_parameter_attributes) unless nested_parameter_attributes.empty? end
Also aliased as: attributes=
Private Instance Methods
_assign_attribute(k, v)
click to toggle source
# File lib/datastax_rails/attribute_assignment.rb, line 40 def _assign_attribute(k, v) public_send("#{k}=", v) rescue NoMethodError if respond_to?("#{k}=") raise else raise UnknownAttributeError, "unknown attribute: #{k}" end end
assign_nested_parameter_attributes(pairs)
click to toggle source
Assign any deferred nested attributes after the base attributes have been set.
# File lib/datastax_rails/attribute_assignment.rb, line 51 def assign_nested_parameter_attributes(pairs) pairs.each { |k, v| _assign_attribute(k, v) } end