Sha256: db5bf5573f9a249aed6e3c6b104f8da11d27ed8940acb9ce121548dc20793a4e

Contents?: true

Size: 1.19 KB

Versions: 13

Compression:

Stored size: 1.19 KB

Contents

module Superstore
  module AttributeMethods
    module Dirty
      extend ActiveSupport::Concern
      include ActiveModel::Dirty

      # Attempts to +save+ the record and clears changed attributes if successful.
      def save(*) #:nodoc:
        if status = super
          @previously_changed = changes
          @changed_attributes = {}
        end
        status
      end

      # <tt>reload</tt> the record and clears changed attributes.
      def reload
        super.tap do
          @previously_changed.try :clear
          @changed_attributes.try :clear
        end
      end

      def unapplied_changes
        result = {}
        changed_attributes.each_key { |attr| result[attr] = read_attribute(attr) }
        result
      end

      def old_attribute_value(attr)
        if attribute_changed?(attr)
          changed_attributes[attr]
        else
          read_attribute attr
        end
      end

      def write_attribute(name, value)
        name = name.to_s
        old = old_attribute_value(name)

        super

        if old == read_attribute(name)
          changed_attributes.delete(name)
        else
          changed_attributes[name] = old
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
superstore-2.4.4 lib/superstore/attribute_methods/dirty.rb
superstore-2.4.3 lib/superstore/attribute_methods/dirty.rb
superstore-2.4.2 lib/superstore/attribute_methods/dirty.rb
superstore-2.4.1 lib/superstore/attribute_methods/dirty.rb
superstore-2.4.0 lib/superstore/attribute_methods/dirty.rb
superstore-2.3.0 lib/superstore/attribute_methods/dirty.rb
superstore-2.2.0 lib/superstore/attribute_methods/dirty.rb
superstore-2.1.3 lib/superstore/attribute_methods/dirty.rb
superstore-2.1.2 lib/superstore/attribute_methods/dirty.rb
superstore-2.1.1 lib/superstore/attribute_methods/dirty.rb
superstore-2.1.0 lib/superstore/attribute_methods/dirty.rb
superstore-2.0.1 lib/superstore/attribute_methods/dirty.rb
superstore-2.0.0 lib/superstore/attribute_methods/dirty.rb