Sha256: bdd55445225abf08e66dd764d53288e38b6de6d7bb036e77c51dd9d6d336e447

Contents?: true

Size: 1013 Bytes

Versions: 15

Compression:

Stored size: 1013 Bytes

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 write_attribute(name, value)
        name = name.to_s
        old = read_attribute(name)

        super

        unless attribute_changed?(name) || old == read_attribute(name)
          changed_attributes[name] = old
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
superstore-1.2.0 lib/superstore/attribute_methods/dirty.rb
superstore-1.1.4 lib/superstore/attribute_methods/dirty.rb
superstore-1.1.3 lib/superstore/attribute_methods/dirty.rb
superstore-1.1.2 lib/superstore/attribute_methods/dirty.rb
superstore-1.1.1 lib/superstore/attribute_methods/dirty.rb
superstore-1.1.0 lib/superstore/attribute_methods/dirty.rb
superstore-1.0.12 lib/superstore/attribute_methods/dirty.rb
superstore-1.0.11 lib/superstore/attribute_methods/dirty.rb
superstore-1.0.10 lib/superstore/attribute_methods/dirty.rb
superstore-1.0.9 lib/superstore/attribute_methods/dirty.rb
superstore-1.0.8 lib/superstore/attribute_methods/dirty.rb
superstore-1.0.7 lib/superstore/attribute_methods/dirty.rb
superstore-1.0.6 lib/superstore/attribute_methods/dirty.rb
superstore-1.0.5 lib/superstore/attribute_methods/dirty.rb
superstore-1.0.4 lib/superstore/attribute_methods/dirty.rb