Sha256: 2e493715f100f14bbf98740334f6863a2b6e8a987a1d964904c8bff1215979de

Contents?: true

Size: 1.34 KB

Versions: 14

Compression:

Stored size: 1.34 KB

Contents

require 'bigdecimal'
module CouchPotato
  module Persistence
    module DirtyAttributes
      
      def self.included(base) #:nodoc:
        base.send :include, ActiveModel::Dirty
        base.class_eval do
          after_save :reset_dirty_attributes
        end
      end
      
      def initialize(attributes = {})
        super
      end
    
      # returns true if a model has dirty attributes, i.e. their value has changed since the last save
      def dirty? 
        changed? || @forced_dirty
      end
      
      # marks a model as dirty
      def is_dirty
        @forced_dirty = true
      end
      
      def method_missing(name, *args)
        if(name.to_s.include?('_will_change!'))
          self.class.define_attribute_methods self.class.property_names
          send(name, *args)
        else
          super
        end
      end
      
      private
      
      def reset_dirty_attributes
        @previously_changed = changes
        @changed_attributes.clear
        @forced_dirty = nil
      end
      
      def clone_attribute(value)
        if [Fixnum, Symbol, TrueClass, FalseClass, NilClass, Float, BigDecimal].include?(value.class)
          value
        elsif [Hash, Array].include?(value.class)
          #Deep clone
          Marshal::load(Marshal::dump(value))
        else
          value.clone
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
couch_potato-0.6.0 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-0.5.7 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-rails2-0.5.10 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-rails2-0.5.9 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-rails2-0.5.8 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-rails2-0.5.7 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-rails2-0.5.6 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-0.5.6 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-0.5.5 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-0.5.4 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-0.5.3 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-0.5.2 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-0.5.1 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-0.5.0 lib/couch_potato/persistence/dirty_attributes.rb