Skip to content

Commit

Permalink
Fields need to be flagged as attribute methods:
Browse files Browse the repository at this point in the history
- In order for dynamic dirty tracking methods to work, each field much
  be registered with Active Model for the method missing matchers. This
  was previously only working if the field had a value set.

- This will need a rework of how it is implemented once the next release
  of Active Model comes out. This is because until the next release all
  fields have to be marked at one time, which causes Mongoid to undef
  the entire list and reset the list with every field definition. AM
  master currently allows attributes to be added one at a time.

- Fixes #1036.
  • Loading branch information
durran committed Jul 1, 2011
1 parent ea71fd6 commit e39e8dd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/mongoid/fields.rb
Expand Up @@ -190,6 +190,13 @@ def add_field(name, options = {})
fields[name] = field
create_accessors(name, meth, options)
process_options(field)

# @todo Durran: Refactor this once we can depend on at least
# ActiveModel greater than 3.0.9. They finally have the ability then
# to add attribute methods one at a time. This code will make class
# load times extremely slow.
undefine_attribute_methods
define_attribute_methods(fields.keys)
end
end

Expand Down
19 changes: 19 additions & 0 deletions spec/unit/mongoid/dirty_spec.rb
Expand Up @@ -2,6 +2,25 @@

describe Mongoid::Dirty do

context "when fields have been defined pre-dirty inclusion" do

let(:person) do
Person.new
end

it "defines a _change method" do
person.updated_at_change.should be_nil
end

it "defines a _changed? method" do
person.updated_at_changed?.should eq(false)
end

it "defines a _changes method" do
person.updated_at_was.should be_nil
end
end

describe "#attribute_change" do

context "when the attribute has changed" do
Expand Down

0 comments on commit e39e8dd

Please sign in to comment.