Sha256: 320d2551188f7b1b0e118d62837bffcee4540b25528f86662ec605afdc2406c2

Contents?: true

Size: 924 Bytes

Versions: 1

Compression:

Stored size: 924 Bytes

Contents

# -*- encoding : utf-8 -*-
module RedisModelExtension

  # == Dirty
  # module for easier detection of changed attributes
  #
  # if you want it in your model include it after RedisModelExtension, i.e.
  #
  #  class MyModel
  #    include RedisModelExtension
  #    include RedisModelExtension::Dirty
  #  end
  module Dirty
    extend ActiveSupport::Concern

    included do
      include ActiveModel::Dirty
    end

    def attribute=(name, value)
      attribute_will_change!(name) unless value == attribute(name)
      super
    end

    def save
      if result = super
        @previously_changed = changes
        @changed_attributes.clear
      end
      result
    end

    module ClassMethods
      # hook to reset changed attributes, when load by .find or .get
      def new_by_key(key)
        new_instance = super
        new_instance.changed_attributes.clear
        new_instance
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis-model-extension-0.4.2 lib/redis-model-extension/dirty.rb