Module: Fakecrm::TolerantMassAssignment

Included in:
EventContact
Defined in:
lib/fakecrm/tolerant_mass_assignment.rb

Instance Method Summary collapse

Instance Method Details

#attributes=(attributes) ⇒ Hash

Note:

unknown custom_* attributes are ignored with warning

Assign values to multiple attributes in one call (mass assignment)

Parameters:

  • attributes (Hash)

    names and values of attributes to assign

Returns:

  • (Hash)

    names and values of attributes assigned



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fakecrm/tolerant_mass_assignment.rb', line 14

def attributes=(attributes)
  model = self.model
  attributes.each do |name, value|
    case name
      when String, Symbol
        if model.allowed_writer_methods.include?(setter = "#{name}=")
          __send__(setter, value)
        else
          if name.to_s =~ /^custom_/
            ::Fakecrm.logger.error("Ignoring the unknown attribute '#{name}' in #{model}")
          else
            raise ArgumentError, "The attribute '#{name}' is not accessible in #{model}"
          end
        end
      when Associations::Relationship, Property
        # only call a public #typecast (e.g. on Property instances)
        if name.respond_to?(:typecast)
          value = name.typecast(value)
        end
        self.persistence_state = persistence_state.set(name, value)
    end
  end
end