Sha256: 68508fca42b904e5b7e9effc9d173dbf012573de02c481fab1c591d9de80b107

Contents?: true

Size: 1 KB

Versions: 8

Compression:

Stored size: 1 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

      # 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

      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

8 entries across 8 versions & 1 rubygems

Version Path
couch_potato-1.7.1 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-1.7.0 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-1.6.5 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-1.6.4 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-1.6.3 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-1.4.0 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-1.3.0 lib/couch_potato/persistence/dirty_attributes.rb
couch_potato-1.2.0 lib/couch_potato/persistence/dirty_attributes.rb