Sha256: c2da79f6cefeeffe606fcfb1bfdbec731902a1926dda282ba1438a6a5f95a007

Contents?: true

Size: 685 Bytes

Versions: 7

Compression:

Stored size: 685 Bytes

Contents

module CouchTomato
  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

7 entries across 7 versions & 1 rubygems

Version Path
couch_tomato-0.2.0 lib/couch_tomato/persistence/dirty_attributes.rb
couch_tomato-0.1.5 lib/couch_tomato/persistence/dirty_attributes.rb
couch_tomato-0.1.4 lib/couch_tomato/persistence/dirty_attributes.rb
couch_tomato-0.1.3 lib/couch_tomato/persistence/dirty_attributes.rb
couch_tomato-0.1.2 lib/couch_tomato/persistence/dirty_attributes.rb
couch_tomato-0.1.1 lib/couch_tomato/persistence/dirty_attributes.rb
couch_tomato-0.1.0 lib/couch_tomato/persistence/dirty_attributes.rb