Skip to content

Commit

Permalink
Context should be created on save state-machines#22
Browse files Browse the repository at this point in the history
It seems that context is evaluated on `.new` instead of `.save`
  • Loading branch information
duleorlovic committed May 19, 2017
1 parent acad608 commit 2cda111
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
29 changes: 28 additions & 1 deletion spec/helpers/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class ARModelWithContextStateTransition < ActiveRecord::Base
belongs_to :ar_model_with_context
end

class ARModelWithContextBasedOnOtherFieldStateTransition < ActiveRecord::Base
belongs_to :ar_model_with_context_based_on_other_field
end

class ARModelWithMultipleContextStateTransition < ActiveRecord::Base
belongs_to :ar_model_with_multiple_context
end
Expand Down Expand Up @@ -124,7 +128,24 @@ def context_with_args(transition)
id = transition.args.last.delete(:id) if transition.args.present?
id
end
end

class ARModelWithContextBasedOnOtherField < ActiveRecord::Base
state_machine :state, initial: :waiting do
audit_trail context: :context

event :start do
transition [:waiting, :stopped] => :started
end

event :stop do
transition :started => :stopped
end
end

def context
other_field
end
end

class ARModelDescendant < ARModel
Expand Down Expand Up @@ -227,7 +248,7 @@ class ARModel < ActiveRecord::Base
#
# Generate tables
#
def create_model_table(owner_class, multiple_state_machines = false, state_column = nil)
def create_model_table(owner_class, multiple_state_machines = false, state_column = nil, additional_field = nil)
ActiveRecord::Base.connection.create_table(owner_class.name.tableize) do |t|
if state_column.presence
t.string state_column
Expand All @@ -242,6 +263,10 @@ def create_model_table(owner_class, multiple_state_machines = false, state_colum
t.string :third
end

if additional_field
t.string additional_field
end

t.timestamps null: false
end
end
Expand All @@ -253,6 +278,7 @@ def create_model_table(owner_class, multiple_state_machines = false, state_colum

create_model_table(ARModelWithNamespace, false, :foo_state)
create_model_table(ARModelWithMultipleStateMachines, true)
create_model_table(ARModelWithContextBasedOnOtherField, false, :state, :other_field)

def create_transition_table(owner_class_name, state, add_context: false, polymorphic: false)
class_name = "#{owner_class_name}#{state.to_s.camelize}Transition"
Expand All @@ -279,6 +305,7 @@ def create_transition_table(owner_class_name, state, add_context: false, polymor
create_transition_table("ARModelWithNamespace", :foo_state, add_context: false)
create_transition_table("ARModelWithContext", :state, add_context: true)
create_transition_table("ARModelWithMultipleContext", :state, add_context: true)
create_transition_table("ARModelWithContextBasedOnOtherField", :state, add_context: true)
create_transition_table("ARModelWithMultipleStateMachines", :first)
create_transition_table("ARModelWithMultipleStateMachines", :second)
create_transition_table("ARModelWithMultipleStateMachines", :third)
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/state_machines/audit_trail/backend/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@
end
end

context 'wants to log a real context on save, not on initialize' do
before(:each) do
StateMachines::AuditTrail::Backend.create_for(ARModelWithContextBasedOnOtherFieldStateTransition, ARModelWithContextBasedOnOtherField, context: :context)
end

let!(:target) { ARModelWithContextBasedOnOtherField.new other_field: "initial_other_field_value" }

it 'should populate with real_other_field_value' do
target.other_field = "real_other_field_value"
target.save
last_transition = ARModelWithContextBasedOnOtherFieldStateTransition.where(:ar_model_with_context_based_on_other_field_id => target.id).last
expect(last_transition.context).to eq target.other_field
expect(last_transition.context).not_to eq "initial_other_field_value"
end
end

end


Expand Down

0 comments on commit 2cda111

Please sign in to comment.