From 86cdbe5ccb55caf5d3f033a80a82eeb80dd222fd Mon Sep 17 00:00:00 2001 From: Brian Durand Date: Mon, 21 Nov 2011 17:23:27 -0600 Subject: [PATCH] fix undefined method from class_attribute not being defined on instances --- lib/acts_as_revisionable.rb | 10 +++++----- spec/acts_as_revisionable_spec.rb | 32 +++++++++++++++---------------- spec/spec_helper.rb | 2 +- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/acts_as_revisionable.rb b/lib/acts_as_revisionable.rb index 3651f04..bb3b20f 100644 --- a/lib/acts_as_revisionable.rb +++ b/lib/acts_as_revisionable.rb @@ -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 @@ -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) @@ -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 diff --git a/spec/acts_as_revisionable_spec.rb b/spec/acts_as_revisionable_spec.rb index dd7ae4c..f5b8559 100644 --- a/spec/acts_as_revisionable_spec.rb +++ b/spec/acts_as_revisionable_spec.rb @@ -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 @@ -334,7 +334,7 @@ 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 @@ -342,7 +342,7 @@ class RevisionableSubclassModel < RevisionableNamespaceModel end model.reload ActsAsRevisionable::RevisionRecord.count.should == 0 - + model.name = 'new_name' model.store_revision do ActsAsRevisionable::RevisionRecord.count.should == 1 @@ -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! @@ -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') @@ -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 @@ -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 @@ -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') @@ -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'} @@ -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 @@ -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' @@ -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 @@ -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') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 151ec8a..0756008 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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"