Sha256: 0b332addcd7c3a9b4ef0fe3bbae500c68625e52710ae5dccb8ddef0f4500c747

Contents?: true

Size: 1.7 KB

Versions: 15

Compression:

Stored size: 1.7 KB

Contents

require 'active_model/dirty'

# Overrides persistence methods, providing support for dirty tracking.
#
module ActiveRemote
  module Dirty
    extend ActiveSupport::Concern

    included do
      include ActiveModel::Dirty

      attr_accessor :_active_remote_track_changes
    end

    def disable_dirty_tracking
      @_active_remote_track_changes = false
    end

    def enable_dirty_tracking
      @_active_remote_track_changes = true
    end

    # Override #reload to provide dirty tracking.
    #
    def reload(*)
      super.tap do
        @previously_changed.try(:clear)
        changed_attributes.clear
      end
    end

    # Override #save to store changes as previous changes then clear them.
    #
    def save(*)
      if status = super
        @previously_changed = changes
        changed_attributes.clear
      end

      status
    end

    # Override #save to store changes as previous changes then clear them.
    #
    def save!(*)
      super.tap do
        @previously_changed = changes
        changed_attributes.clear
      end
    end

    def skip_dirty_tracking(&block)
      disable_dirty_tracking

      yield

      enable_dirty_tracking
    end

  private

    # Wether or not changes are currently being tracked for this class.
    #
    def _active_remote_track_changes?
      @_active_remote_track_changes != false
    end

    # Override ActiveAttr's attribute= method so we can provide support for
    # ActiveModel::Dirty.
    #
    def attribute=(name, value)
      __send__("#{name}_will_change!") if _active_remote_track_changes? && value != self[name]
      super
    end

    # Override #update to only send changed attributes.
    #
    def remote_update(*)
      super(changed)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
active_remote-3.3.3 lib/active_remote/dirty.rb
active_remote-3.3.2 lib/active_remote/dirty.rb
active_remote-3.3.1 lib/active_remote/dirty.rb
active_remote-3.3.0 lib/active_remote/dirty.rb
active_remote-3.2.2 lib/active_remote/dirty.rb
active_remote-3.2.1 lib/active_remote/dirty.rb
active_remote-3.2.0 lib/active_remote/dirty.rb
active_remote-3.2.0.pre lib/active_remote/dirty.rb
active_remote-3.1.3 lib/active_remote/dirty.rb
active_remote-3.1.2 lib/active_remote/dirty.rb
active_remote-3.1.2.pre lib/active_remote/dirty.rb
active_remote-3.1.1 lib/active_remote/dirty.rb
active_remote-3.1.0 lib/active_remote/dirty.rb
active_remote-3.0.0 lib/active_remote/dirty.rb
active_remote-3.0.0.pre1 lib/active_remote/dirty.rb