Skip to content

Commit

Permalink
fix undefined method from class_attribute not being defined on instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Durand committed Nov 21, 2011
1 parent 9904352 commit 86cdbe5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
10 changes: 5 additions & 5 deletions lib/acts_as_revisionable.rb
Expand Up @@ -57,7 +57,6 @@ def acts_as_revisionable(options = {})
acts_as_revisionable_options[:class_name] = acts_as_revisionable_options[:class_name].name if acts_as_revisionable_options[:class_name].is_a?(Class)
extend ClassMethods
include InstanceMethods
class_name =
class_name = acts_as_revisionable_options[:class_name].to_s if acts_as_revisionable_options[:class_name]
has_many_options = {:as => :revisionable, :order => 'revision DESC', :class_name => class_name}
has_many_options[:dependent] = :destroy unless options[:dependent] == :keep
Expand Down Expand Up @@ -245,9 +244,10 @@ def store_revision

# Create a revision record based on this record and save it to the database.
def create_revision!
revision = revision_record_class.new(self, acts_as_revisionable_options[:encoding])
if self.acts_as_revisionable_options[:meta].is_a?(Hash)
self.acts_as_revisionable_options[:meta].each do |attribute, value|
revision_options = self.class.acts_as_revisionable_options
revision = revision_record_class.new(self, revision_options[:encoding])
if revision_options[:meta].is_a?(Hash)
revision_options[:meta].each do |attribute, value|
case value
when Symbol
value = self.send(value)
Expand All @@ -263,7 +263,7 @@ def create_revision!

# Truncate the number of revisions kept for this record. Available options are :limit and :minimum_age.
def truncate_revisions!(options = nil)
options = {:limit => acts_as_revisionable_options[:limit], :minimum_age => acts_as_revisionable_options[:minimum_age]} unless options
options = {:limit => self.class.acts_as_revisionable_options[:limit], :minimum_age => self.class.acts_as_revisionable_options[:minimum_age]} unless options
revision_record_class.truncate_revisions(self.class, self.id, options)
end

Expand Down
32 changes: 16 additions & 16 deletions spec/acts_as_revisionable_spec.rb
Expand Up @@ -322,7 +322,7 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
end
model.reload
ActsAsRevisionable::RevisionRecord.count.should == 0

model.should_receive(:update).and_raise("update failed")
model.name = 'new_name'
begin
Expand All @@ -334,15 +334,15 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
end
ActsAsRevisionable::RevisionRecord.count.should == 0
end

it "should not save a revision if an update fails with errors" do
model = RevisionableTestModel.new(:name => 'test')
model.store_revision do
model.save!
end
model.reload
ActsAsRevisionable::RevisionRecord.count.should == 0

model.name = 'new_name'
model.store_revision do
ActsAsRevisionable::RevisionRecord.count.should == 1
Expand All @@ -351,7 +351,7 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
end
ActsAsRevisionable::RevisionRecord.count.should == 0
end

it "should mark the last revision for a deleted record as being trash" do
model = ActsAsRevisionable::RevisionableNamespaceModel.new(:name => 'test')
model.save!
Expand All @@ -364,7 +364,7 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
ActsAsRevisionable::RevisionRecord.last_revision(ActsAsRevisionable::RevisionableNamespaceModel, model.id).should be_trash
end
end

context "restoring revisions" do
it "should restore a record without associations" do
model = RevisionableTestModel.new(:name => 'test')
Expand All @@ -374,7 +374,7 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
end
model.reload
ActsAsRevisionable::RevisionRecord.count.should == 0

model.name = 'new_name'
model.set_secret(5678)
model.store_revision do
Expand All @@ -384,12 +384,12 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
ActsAsRevisionable::RevisionRecord.count.should == 1
model.name.should == 'new_name'
model.secret.should == 5678

restored = model.restore_revision(1)
restored.name.should == 'test'
restored.secret.should == 1234
restored.id.should == model.id

restored.store_revision do
restored.save!
end
Expand All @@ -399,12 +399,12 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
restored_model.name.should == restored.name
restored_model.secret.should == restored.secret
end

it "should restore a record with has_many associations" do
many_thing_1 = RevisionableTestManyThing.new(:name => 'many_thing_1')
many_thing_1.sub_things.build(:name => 'sub_thing_1')
many_thing_1.sub_things.build(:name => 'sub_thing_2')

model = RevisionableTestModel.new(:name => 'test')
model.many_things << many_thing_1
model.many_things.build(:name => 'many_thing_2')
Expand All @@ -416,7 +416,7 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
RevisionableTestSubThing.count.should == 2
RevisionableTestManyOtherThing.count.should == 2
ActsAsRevisionable::RevisionRecord.count.should == 0

model.store_revision do
model.name = 'new_name'
many_thing_1 = model.many_things.detect{|t| t.name == 'many_thing_1'}
Expand All @@ -439,7 +439,7 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
sub_thing_1.save!
many_other_thing_1.save!
end

model.reload
ActsAsRevisionable::RevisionRecord.count.should == 1
RevisionableTestManyThing.count.should == 2
Expand All @@ -449,7 +449,7 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
model.many_things.collect{|t| t.name}.sort.should == ['many_thing_3', 'new_many_thing_1']
model.many_things.detect{|t| t.name == 'new_many_thing_1'}.sub_things.collect{|t| t.name}.sort.should == ['new_sub_thing_1', 'sub_thing_3']
model.many_other_things.collect{|t| t.name}.sort.should == ['many_other_thing_3', 'new_many_other_thing_1']

# restore to memory
restored = model.restore_revision(1)
restored.name.should == 'test'
Expand All @@ -458,14 +458,14 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
restored.many_things.detect{|t| t.name == 'many_thing_1'}.sub_things.collect{|t| t.name}.sort.should == ['sub_thing_1', 'sub_thing_2']
restored.many_other_things.collect{|t| t.name}.sort.should == ['many_other_thing_3', 'new_many_other_thing_1']
restored.valid?.should == true

# make the restore to memory didn't affect the database
model.reload
model.name.should == 'new_name'
model.many_things(true).collect{|t| t.name}.sort.should == ['many_thing_3', 'new_many_thing_1']
model.many_things.detect{|t| t.name == 'new_many_thing_1'}.sub_things.collect{|t| t.name}.sort.should == ['new_sub_thing_1', 'sub_thing_3']
model.many_other_things.collect{|t| t.name}.sort.should == ['many_other_thing_3', 'new_many_other_thing_1']

model.restore_revision!(1)
RevisionableTestModel.count.should == 1
RevisionableTestManyThing.count.should == 2
Expand All @@ -479,7 +479,7 @@ class RevisionableSubclassModel < RevisionableNamespaceModel
restored.many_things.detect{|t| t.name == 'many_thing_2'}.sub_things.collect{|t| t.name}.sort.should == []
restored.many_other_things.collect{|t| t.name}.sort.should == ['many_other_thing_3', 'new_many_other_thing_1']
end

it "should restore a record with has_one associations" do
model = RevisionableTestModel.new(:name => 'test')
model.build_one_thing(:name => 'other')
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -19,7 +19,7 @@
if ActiveRecord::VERSION::MINOR == 0
composite_primary_key_version = "~>3.1.0"
else
composite_primary_key_version = "~>4.0.0.a"
composite_primary_key_version = ">=4.0.0"
end
else
composite_primary_key_version = "~>2.3.5"
Expand Down

0 comments on commit 86cdbe5

Please sign in to comment.