Sha256: cbea60009d1a492748a02167e57355af515b0e31e8d950ac601f74b01b822cf3

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module Recliner
  module AttributeMethods
    module Dirty
      extend ActiveSupport::Concern
      
      include ActiveModel::Dirty
      
      included do
        alias_method_chain :save,  :dirty
        alias_method_chain :save!, :dirty
      end
      
      # Attempts to +save+ the record and clears changed attributes if successful.
      def save_with_dirty(*args) #:nodoc:
        if status = save_without_dirty(*args)
          changed_attributes.clear
        end
        status
      end
    
      # Attempts to <tt>save!</tt> the record and clears changed attributes if successful.
      def save_with_dirty!(*args) #:nodoc:
        status = save_without_dirty!(*args)
        changed_attributes.clear
        status
      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_s
          
        # 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

1 entries across 1 versions & 1 rubygems

Version Path
recliner-0.0.1 lib/recliner/attribute_methods/dirty.rb