Sha256: 4bd4ca27b3310b6fc2ac796873c3e2ea94a2dba46a55f109b4a9802f48619923

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

# encoding: UTF-8
module MongoMapper
  module Plugins
    module Dirty
      extend ActiveSupport::Concern
      include ::ActiveModel::Dirty

      module ClassMethods
        def create_accessors_for(key)
          super.tap do
            define_attribute_methods([key.name])
          end
        end
      end

      def save_to_collection(*)
        super.tap do
          changes_applied
        end
      end

      def reload!
        super.tap do
          clear_changes_information
        end
      end

    private

      def write_key(key_name, value)
        key_name = unalias_key(key_name)

        if !keys.key?(key_name)
          super
        else
          # find the MongoMapper::Plugins::Keys::Key
          _, key = keys.detect { |n, v| n == key_name }

          # typecast to the new value
          old_value = read_key(key_name)
          new_value = key.get(key.set(value))

          # only mark changed if really changed value (after typecasting)
          unless old_value == new_value
            attribute_will_change!(key_name)
          end

          super
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_mapper-0.15.3 lib/mongo_mapper/plugins/dirty.rb
mongo_mapper-0.15.2 lib/mongo_mapper/plugins/dirty.rb