diff --git a/lib/aasm/persistence/active_record_persistence.rb b/lib/aasm/persistence/active_record_persistence.rb index 0193212..449a536 100644 --- a/lib/aasm/persistence/active_record_persistence.rb +++ b/lib/aasm/persistence/active_record_persistence.rb @@ -96,7 +96,10 @@ module AASM end def aasm_column_looks_like_enum(name=:default) - self.class.columns_hash[self.class.aasm(name).attribute_name.to_s].type == :integer + column_name = self.class.aasm(name).attribute_name.to_s + column = self.class.columns_hash[column_name] + raise NoMethodError.new("undefined method '#{column_name}' for #{self.class}") if column.nil? + column.type == :integer end def aasm_guess_enum_method(name=:default) diff --git a/spec/database.rb b/spec/database.rb index ca62989..c4aeeb7 100644 --- a/spec/database.rb +++ b/spec/database.rb @@ -1,5 +1,5 @@ ActiveRecord::Migration.suppress_messages do - %w{gates multiple_gates readers writers transients simples no_scopes multiple_no_scopes no_direct_assignments multiple_no_direct_assignments thieves multiple_thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums false_states multiple_with_enums multiple_with_true_enums multiple_with_false_enums multiple_false_states readme_jobs}.each do |table_name| + %w{gates multiple_gates readers writers transients simples no_scopes multiple_no_scopes no_direct_assignments multiple_no_direct_assignments thieves multiple_thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_enum_without_columns multiple_with_enum_without_columns with_true_enums with_false_enums false_states multiple_with_enums multiple_with_true_enums multiple_with_false_enums multiple_false_states readme_jobs}.each do |table_name| ActiveRecord::Migration.create_table table_name, :force => true do |t| t.string "aasm_state" end diff --git a/spec/models/active_record/with_enum_without_column.rb b/spec/models/active_record/with_enum_without_column.rb new file mode 100644 index 0000000..a227d30 --- /dev/null +++ b/spec/models/active_record/with_enum_without_column.rb @@ -0,0 +1,35 @@ +class WithEnumWithoutColumn < ActiveRecord::Base + include AASM + + enum status: { + opened: 0, + closed: 1 + } + + aasm :column => :status do + state :closed, initial: true + state :opened + + event :view do + transitions :to => :opened, :from => :closed + end + end +end + +class MultipleWithEnumWithoutColumn < ActiveRecord::Base + include AASM + + enum status: { + opened: 0, + closed: 1 + } + + aasm :left, :column => :status do + state :closed, initial: true + state :opened + + event :view do + transitions :to => :opened, :from => :closed + end + end +end diff --git a/spec/unit/persistence/active_record_persistence_multiple_spec.rb b/spec/unit/persistence/active_record_persistence_multiple_spec.rb index f76963d..64a521a 100644 --- a/spec/unit/persistence/active_record_persistence_multiple_spec.rb +++ b/spec/unit/persistence/active_record_persistence_multiple_spec.rb @@ -113,6 +113,17 @@ describe "instance methods" do end end end + + context "when AASM enum setting is not enabled and aasm column not present" do + before :each do + @multiple_with_enum_with_column = MultipleWithEnumWithoutColumn.new + end + + it "should raise NoMethodError for transitions" do + expect{@multiple_with_enum_with_column.send(:view, :left)}.to raise_error(NoMethodError, "undefined method 'status' for MultipleWithEnumWithoutColumn") + end + end + end context "when AASM is configured to use enum" do diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb index d480cb5..eee6b42 100644 --- a/spec/unit/persistence/active_record_persistence_spec.rb +++ b/spec/unit/persistence/active_record_persistence_spec.rb @@ -113,6 +113,17 @@ describe "instance methods" do end end end + + context "when AASM enum setting is not enabled and aasm column not present" do + before :each do + @with_enum_with_column = WithEnumWithoutColumn.new + end + + it "should raise NoMethodError for transitions" do + expect{@with_enum_with_column.send(:view)}.to raise_error(NoMethodError, "undefined method 'status' for WithEnumWithoutColumn") + end + end + end context "when AASM is configured to use enum" do