Sha256: ec1960bee6c9de954a8584b86524c6b14686f56c94a5b5cc54e4fc0ea283e405

Contents?: true

Size: 1.41 KB

Versions: 30

Compression:

Stored size: 1.41 KB

Contents

module ActiveRecord
  module AttributeMethods
    module Write
      extend ActiveSupport::Concern

      included do
        attribute_method_suffix "="
      end

      module ClassMethods
        protected
          def define_method_attribute=(attr_name)
            if attr_name =~ /^[a-zA-Z_]\w*[!?=]?$/
              generated_attribute_methods.module_eval("def #{attr_name}=(new_value); write_attribute('#{attr_name}', new_value); end", __FILE__, __LINE__)
            else
              generated_attribute_methods.send(:define_method, "#{attr_name}=") do |new_value|
                write_attribute(attr_name, new_value)
              end
            end
          end
      end

      # Updates the attribute identified by <tt>attr_name</tt> with the specified +value+. Empty strings
      # for fixnum and float columns are turned into +nil+.
      def write_attribute(attr_name, value)
        attr_name = attr_name.to_s
        attr_name = self.class.primary_key if attr_name == 'id'
        @attributes_cache.delete(attr_name)
        if (column = column_for_attribute(attr_name)) && column.number?
          @attributes[attr_name] = convert_number_column_value(value)
        else
          @attributes[attr_name] = value
        end
      end

      private
        # Handle *= for method_missing.
        def attribute=(attribute_name, value)
          write_attribute(attribute_name, value)
        end
    end
  end
end

Version data entries

30 entries across 30 versions & 2 rubygems

Version Path
activerecord-3.0.20 lib/active_record/attribute_methods/write.rb
activerecord-3.0.19 lib/active_record/attribute_methods/write.rb
activerecord-3.0.18 lib/active_record/attribute_methods/write.rb
activerecord-3.0.17 lib/active_record/attribute_methods/write.rb
activerecord-3.0.16 lib/active_record/attribute_methods/write.rb
activerecord-3.0.15 lib/active_record/attribute_methods/write.rb
activerecord-3.0.14 lib/active_record/attribute_methods/write.rb
activerecord-3.0.13 lib/active_record/attribute_methods/write.rb
activerecord-3.0.13.rc1 lib/active_record/attribute_methods/write.rb
activerecord-3.0.12 lib/active_record/attribute_methods/write.rb
activerecord-3.0.12.rc1 lib/active_record/attribute_methods/write.rb
activerecord-3.0.11 lib/active_record/attribute_methods/write.rb
activerecord-3.0.10 lib/active_record/attribute_methods/write.rb
activerecord-3.0.10.rc1 lib/active_record/attribute_methods/write.rb
activerecord-3.0.9 lib/active_record/attribute_methods/write.rb
activerecord-3.0.9.rc5 lib/active_record/attribute_methods/write.rb
activerecord-3.0.9.rc4 lib/active_record/attribute_methods/write.rb
activerecord-3.0.9.rc3 lib/active_record/attribute_methods/write.rb
activerecord-3.0.9.rc1 lib/active_record/attribute_methods/write.rb
activerecord-3.0.8 lib/active_record/attribute_methods/write.rb