Sha256: 386715fa31e696f7c4f89222abc6837f85ae4a8211aab1a9a99de7deeaaf10e2

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

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

    module ClassMethods
      def from_database(*)
        super.tap { |d| d.send(:clear_changes_information) }
      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
          clear_changes_information
        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

    if ActiveModel::VERSION::STRING >= '5.2.0'
      # The ActiveModel::Dirty API was changed
      # https://github.com/rails/rails/commit/c3675f50d2e59b7fc173d7b332860c4b1a24a726#diff-aaddd42c7feb0834b1b5c66af69814d3
      # So we just try to disable new functionality

      def mutations_from_database
        @mutations_from_database ||= ActiveModel::NullMutationTracker.instance
      end

      def forget_attribute_assignments
      end
    end

    if ActiveModel::VERSION::STRING < '4.2.0'
      def clear_changes_information
        changed_attributes.clear
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dynamoid-2.2.0 lib/dynamoid/dirty.rb