Sha256: cb8a4271bc7849858ad1fee7633f2882678c3dfdae9d38ed145c1af86508499c

Contents?: true

Size: 1.44 KB

Versions: 18

Compression:

Stored size: 1.44 KB

Contents

module ActiveRecord
  module AttributeMethods
    module Write
      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 #{attr_name.inspect} and values #{value.inspect} 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
      alias_method :raw_write_attribute, :write_attribute
    end
  end
end

ActiveRecord::Base.class_eval do
  alias_method :raw_write_attribute, :write_attribute
  alias :[]= :write_attribute
  public :[]=
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
composite_primary_keys-6.0.8 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-6.0.7 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.9 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.8 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.7 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.6 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.5 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.4 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.3 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.2 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.1 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-7.0.0 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-6.0.6 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-6.0.5 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-6.0.3 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-5.0.14 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-6.0.1 lib/composite_primary_keys/attribute_methods/write.rb
composite_primary_keys-6.0.0 lib/composite_primary_keys/attribute_methods/write.rb