Skip to content

Commit

Permalink
Ignore namespace column if not present
Browse files Browse the repository at this point in the history
Fixes state-machines#15.

Don't attempt to save to the namespace column on the transition table if
it doesn't exist.
  • Loading branch information
breckenedge committed Oct 24, 2019
1 parent 6da97cf commit 190eadf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/state_machines/audit_trail/backend/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def initialize(transition_class, owner_class, options = {})
end

def persist(object, fields)
fields.delete(:namespace) unless namespace_column_present?(object)

# Let ActiveRecord manage the timestamp for us so it does the right thing with regards to timezones.
if object.new_record?
object.send(@association).build(fields)
Expand All @@ -18,4 +20,11 @@ def persist(object, fields)

nil
end

private

# Does the namespace column exist on the transition table for the provided object?
def namespace_column_present?(object)
object.class.reflect_on_association(@association).klass.column_names.include?('namespace')
end
end
6 changes: 3 additions & 3 deletions spec/helpers/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ def create_model_table(owner_class, multiple_state_machines = false, state_colum
create_model_table(ARModelWithNamespace, false, :foo_state)
create_model_table(ARModelWithMultipleStateMachines, true)

def create_transition_table(owner_class_name, state, add_context: false, polymorphic: false)
def create_transition_table(owner_class_name, state, add_context: false, polymorphic: false, add_namespace: false)
class_name = "#{owner_class_name}#{state.to_s.camelize}Transition"
ActiveRecord::Base.connection.create_table(class_name.tableize) do |t|

t.references "#{owner_class_name.demodulize.underscore}", index: false, polymorphic: polymorphic
# t.integer owner_class_name.foreign_key
t.string :namespace
t.string :namespace if add_namespace
t.string :event
t.string :from
t.string :to
Expand All @@ -276,7 +276,7 @@ def create_transition_table(owner_class_name, state, add_context: false, polymor
create_transition_table(name, :state)
end

create_transition_table("ARModelWithNamespace", :foo_state, add_context: false)
create_transition_table("ARModelWithNamespace", :foo_state, add_namespace: true)
create_transition_table("ARModelWithContext", :state, add_context: true)
create_transition_table("ARModelWithMultipleContext", :state, add_context: true)
create_transition_table("ARModelWithMultipleStateMachines", :first)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@
expect(state_transition.event).to be_nil
end

it 'should not log namespace' do
it 'should not log namespace if namespace column doesn\'t exist' do
target = ARModel.create!
expect(target.new_record?).to be_falsey
expect(target.ar_model_state_transitions.count).to eq 1
state_transition = target.ar_model_state_transitions.first
expect(state_transition.namespace).to be_nil
expect(state_transition.from).to be_nil
expect(state_transition.to).to eq 'waiting'
expect(state_transition.event).to be_nil
Expand Down

0 comments on commit 190eadf

Please sign in to comment.