diff --git a/spec/helpers/active_record.rb b/spec/helpers/active_record.rb index eff8131..19dcec8 100644 --- a/spec/helpers/active_record.rb +++ b/spec/helpers/active_record.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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" @@ -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) diff --git a/spec/lib/state_machines/audit_trail/backend/active_record_spec.rb b/spec/lib/state_machines/audit_trail/backend/active_record_spec.rb index f52c0be..48b2d31 100644 --- a/spec/lib/state_machines/audit_trail/backend/active_record_spec.rb +++ b/spec/lib/state_machines/audit_trail/backend/active_record_spec.rb @@ -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