Skip to content

Commit

Permalink
partially implemented dirty checking
Browse files Browse the repository at this point in the history
  • Loading branch information
gdagley committed Aug 3, 2010
1 parent 7a8c97b commit 501b32c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
24 changes: 23 additions & 1 deletion lib/toy/dirty.rb
@@ -1,5 +1,27 @@
module Toy
module Dirty

extend ActiveSupport::Concern
include ActiveModel::Dirty

module InstanceMethods
def attributes_changed!(attributes)
attributes.each do |attr_name|
attribute_will_change!(attr_name)
end
end

def save
super.tap { changed_attributes.clear }
end

def write_attribute(name, value)
name = name.to_s
unless attribute_changed?(name)
old = read_attribute(name)
changed_attributes[name] = old if old != value
end
super
end
end
end
end
1 change: 1 addition & 0 deletions lib/toy/store.rb
Expand Up @@ -7,6 +7,7 @@ module Store
include ActiveModel::Conversion
include Attributes
include Persistence
include Dirty
include Querying

include Callbacks
Expand Down
8 changes: 4 additions & 4 deletions spec/toy/dirty_spec.rb
Expand Up @@ -3,20 +3,20 @@
describe Toy::Dirty do
before :each do
model = Model('Foo') do
store FileStore
attribute :name
store RedisStore
attribute :name, String
end
@foo = model.create(:name => 'Geoffrey')
end
let(:foo) { @foo }

xit "should identify when there are no changes" do
it "should identify when there are no changes" do
foo.changed?.should be_false
foo.changed.should be_empty
foo.changes.should be_empty
end

xit "should identify when there are changes" do
it "should identify when there are changes" do
foo.name = 'John'

foo.changed?.should be_true
Expand Down

0 comments on commit 501b32c

Please sign in to comment.