Skip to content

Commit

Permalink
Properly get the foreign key for STI models
Browse files Browse the repository at this point in the history
  • Loading branch information
jstorimer committed Jun 3, 2011
1 parent 73d5bf5 commit d0600a0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/state_machine/audit_trail/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ def log(object, event, from, to, timestamp = Time.now)
end

def foreign_key_field(object)
object.class.name.foreign_key.to_sym
object.class.base_class.name.foreign_key.to_sym
end
end
6 changes: 4 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

ActiveRecord::Base.connection.create_table(:test_models) do |t|
t.string :state
t.string :type
t.timestamps
end

Expand All @@ -22,7 +23,6 @@
end



def create_transition_table(owner_class, state)
class_name = "#{owner_class.name}#{state.to_s.camelize}Transition"

Expand All @@ -49,7 +49,6 @@ class TestModelWithMultipleStateMachinesSecondTransition < ActiveRecord::Base
belongs_to :test_model
end


class TestModel < ActiveRecord::Base

state_machine :state, :initial => :waiting do # log initial state?
Expand All @@ -65,6 +64,9 @@ class TestModel < ActiveRecord::Base
end
end

class TestModelDescendant < TestModel
end

class TestModelWithMultipleStateMachines < ActiveRecord::Base

state_machine :first, :initial => :beginning do
Expand Down
5 changes: 5 additions & 0 deletions spec/state_machine/audit_trail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@
first_transition.created_at.should be_within(10.seconds).of(Time.now.utc)
end
end

it "should properly grab the class name from STI models" do
m = TestModelDescendant.create!
lambda { m.start! }.should_not raise_error
end
end

0 comments on commit d0600a0

Please sign in to comment.