Sha256: 96a9f55419b21bca8bbd8c472808862908769351b22d5e1f00eb2500c4c23306

Contents?: true

Size: 953 Bytes

Versions: 9

Compression:

Stored size: 953 Bytes

Contents

module MongoModel
  module AttributeMethods
    module Dirty
      extend ActiveSupport::Concern
      
      include ActiveModel::Dirty
      
      included do
        after_save { changed_attributes.clear }
      end
      
      # Returns the attributes as they were before any changes were made to the document.
      def original_attributes
        attributes.merge(changed_attributes)
      end
      
      # Wrap write_attribute to remember original attribute value.
      def write_attribute(attr, value)
        attr = attr.to_sym
          
        # The attribute already has an unsaved change.
        if changed_attributes.include?(attr)
          old = changed_attributes[attr]
          changed_attributes.delete(attr) if value == old
        else
          old = clone_attribute_value(attr)
          changed_attributes[attr] = old unless value == old
        end
        
        # Carry on.
        super
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mongomodel-0.2.2 lib/mongomodel/concerns/attribute_methods/dirty.rb
mongomodel-0.2.1 lib/mongomodel/concerns/attribute_methods/dirty.rb
mongomodel-0.2.0 lib/mongomodel/concerns/attribute_methods/dirty.rb
mongomodel-0.1.6 lib/mongomodel/concerns/attribute_methods/dirty.rb
mongomodel-0.1.5 lib/mongomodel/concerns/attribute_methods/dirty.rb
mongomodel-0.1.4 lib/mongomodel/concerns/attribute_methods/dirty.rb
mongomodel-0.1.3 lib/mongomodel/concerns/attribute_methods/dirty.rb
mongomodel-0.1.1 lib/mongomodel/concerns/attribute_methods/dirty.rb
mongomodel-0.1 lib/mongomodel/concerns/attribute_methods/dirty.rb