Skip to content

Commit

Permalink
reset dirty attributes after save
Browse files Browse the repository at this point in the history
  • Loading branch information
langalex committed Apr 27, 2009
1 parent 3348bff commit 9a5ff75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/couch_potato/persistence.rb
Expand Up @@ -13,7 +13,8 @@ module CouchPotato
module Persistence

def self.included(base)
base.send :include, Properties, Callbacks, Validatable, Json, DirtyAttributes, CouchPotato::View::CustomViews
base.send :include, Properties, Callbacks, Validatable, Json, CouchPotato::View::CustomViews
base.send :include, DirtyAttributes
base.send :include, MagicTimestamps
base.class_eval do
attr_accessor :_id, :_rev, :_attachments, :_deleted
Expand Down
15 changes: 15 additions & 0 deletions lib/couch_potato/persistence/dirty_attributes.rb
@@ -1,11 +1,26 @@
module CouchPotato
module Persistence
module DirtyAttributes

def self.included(base)
base.class_eval do
after_save :reset_dirty_attributes
end
end

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
7 changes: 6 additions & 1 deletion spec/unit/dirty_attributes_spec.rb
Expand Up @@ -88,7 +88,12 @@ class Plate

describe "after save" do
it "should reset all attributes to not dirty" do
pending
couchrest_db = stub('database', :get => {'_id' => '1', '_rev' => '2', 'food' => 'sushi', 'ruby_class' => 'Plate'}, :save_doc => {})
db = CouchPotato::Database.new(couchrest_db)
@plate = db.load_document '1'
@plate.food = 'burger'
db.save! @plate
@plate.should_not be_food_changed
end
end

Expand Down

0 comments on commit 9a5ff75

Please sign in to comment.