Sha256: 02ec464014fdd17d60b5bb8d229c66ff34e827b96eea637e8449eaa10ce78cd5

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

module Dynamoid
  module Dirty
    extend ActiveSupport::Concern
    include ActiveModel::Dirty

    module ClassMethods
      def from_database(*)
        super.tap { |d| d.changed_attributes.clear }
      end
    end

    def save(*)
      clear_changes { super }
    end

    def update!(*)
      ret = super
      clear_changes # update! completely reloads all fields on the class, so any extant changes are wiped out
      ret
    end

    def reload
      super.tap { clear_changes }
    end

    def clear_changes
      previous = changes
      (block_given? ? yield : true).tap do |result|
        unless result == false # failed validation; nil is OK.
          @previously_changed = previous
          changed_attributes.clear
        end
      end
    end

    def write_attribute(name, value)
      attribute_will_change!(name) unless self.read_attribute(name) == value
      super
    end

    protected

    def attribute_method?(attr)
      super || self.class.attributes.has_key?(attr.to_sym)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dynamoid-2.1.0 lib/dynamoid/dirty.rb
dynamoid-2.0.0 lib/dynamoid/dirty.rb