Module: ActiveRecord::AttributeMethods::Write

Defined in:
lib/composite_primary_keys/attribute_methods/write.rb

Instance Method Summary collapse

Instance Method Details

#write_attribute(attr_name, value) ⇒ Object Also known as: raw_write_attribute



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/composite_primary_keys/attribute_methods/write.rb', line 4

def write_attribute(attr_name, value)
  # CPK
  if attr_name.kind_of?(Array)
    value = [nil]*attr_name.length if value.nil?
    unless value.length == attr_name.length
      raise "Number of attr_names and values do not match"
    end
    [attr_name, value].transpose.map {|name,val| write_attribute(name, val)}
    value
  else
    attr_name = attr_name.to_s
    # CPK
    # attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key
    attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key && !self.composite?
    @attributes_cache.delete(attr_name)
    column = column_for_attribute(attr_name)

    unless column || @attributes.has_key?(attr_name)
      ActiveSupport::Deprecation.warn(
          "You're trying to create an attribute `#{attr_name}'. Writing arbitrary " \
          "attributes on a model is deprecated. Please just use `attr_writer` etc."
      )
    end
    @attributes[attr_name] = type_cast_attribute_for_write(column, value)
  end
end