Sha256: b8bbae7780ab2e1fe99fc0b839c05866d01a0e21cbab1564bc4a2eb3811fb6b9

Contents?: true

Size: 685 Bytes

Versions: 8

Compression:

Stored size: 685 Bytes

Contents

module CouchPotato
  module Persistence
    module DirtyAttributes
      
      def self.included(base)
        base.class_eval do
          after_save :reset_dirty_attributes
        end
      end
      
      # returns true if a model has dirty attributes, i.e. their value has changed since the last save
      def dirty? 
        new? || self.class.properties.inject(false) do |res, property|
          res || property.dirty?(self)
        end
      end
      
      private
      
      def reset_dirty_attributes
        self.class.properties.each do |property|
          instance_variable_set("@#{property.name}_was", send(property.name))
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
andrewtimberlake-couch_potato-0.2.8.1 lib/couch_potato/persistence/dirty_attributes.rb
langalex-couch_potato-0.2.3 lib/couch_potato/persistence/dirty_attributes.rb
langalex-couch_potato-0.2.4 lib/couch_potato/persistence/dirty_attributes.rb
langalex-couch_potato-0.2.5 lib/couch_potato/persistence/dirty_attributes.rb
langalex-couch_potato-0.2.6 lib/couch_potato/persistence/dirty_attributes.rb
langalex-couch_potato-0.2.7 lib/couch_potato/persistence/dirty_attributes.rb
langalex-couch_potato-0.2.8 lib/couch_potato/persistence/dirty_attributes.rb
thefool808-couch_potato-0.2.7 lib/couch_potato/persistence/dirty_attributes.rb