Skip to content

Commit

Permalink
Merge pull request #507 from gocardless/reload-should-clear-the-cache
Browse files Browse the repository at this point in the history
ActiveModel.reload has to clear the internal cache
  • Loading branch information
zerc committed Apr 21, 2023
2 parents 2a9d8ac + 3ce4571 commit 455a21d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v10.2.2 21st April 2023

### Changed
- Calling `active_record.reload` resets the adapater's internal cache

## v10.2.1 3rd April 2023

### Changed
Expand Down
5 changes: 3 additions & 2 deletions lib/statesman/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ def last(force_reload: false)
end

def reset
remove_instance_variable(:@last_transition) \
if instance_variable_defined?(:@last_transition)
if instance_variable_defined?(:@last_transition)
remove_instance_variable(:@last_transition)
end
end

private
Expand Down
8 changes: 8 additions & 0 deletions lib/statesman/adapters/active_record_queries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def included(base)

define_in_state(base, query_builder)
define_not_in_state(base, query_builder)

define_method(:reload) do |*a|
instance = super(*a)
if instance.respond_to?(:state_machine, true)
instance.state_machine.reset
end
instance
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/statesman/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Statesman
VERSION = "10.2.1"
VERSION = "10.2.2"
end
12 changes: 6 additions & 6 deletions spec/statesman/machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -976,20 +976,20 @@ def after_initialize; end
end

context "with defined callbacks" do
let(:callback_1) { -> { "Hi" } }
let(:callback_2) { -> { "Bye" } }
let(:callback_one) { -> { "Hi" } }
let(:callback_two) { -> { "Bye" } }

before do
machine.send(definer, from: :x, to: :y, &callback_1)
machine.send(definer, from: :y, to: :z, &callback_2)
machine.send(definer, from: :x, to: :y, &callback_one)
machine.send(definer, from: :y, to: :z, &callback_two)
end

it "contains the relevant callback" do
expect(callbacks.map(&:callback)).to include(callback_1)
expect(callbacks.map(&:callback)).to include(callback_one)
end

it "does not contain the irrelevant callback" do
expect(callbacks.map(&:callback)).to_not include(callback_2)
expect(callbacks.map(&:callback)).to_not include(callback_two)
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions spec/support/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,22 @@ class MyStateMachine
transition from: :failed, to: :initial
end

class MyActiveRecordModelTransition < ActiveRecord::Base
include Statesman::Adapters::ActiveRecordTransition

belongs_to :my_active_record_model
serialize :metadata, JSON
end

class MyActiveRecordModel < ActiveRecord::Base
has_many :my_active_record_model_transitions, autosave: false
alias_method :transitions, :my_active_record_model_transitions

include Statesman::Adapters::ActiveRecordQueries[
transition_class: MyActiveRecordModelTransition,
initial_state: :initial
]

def state_machine
@state_machine ||= MyStateMachine.new(
self, transition_class: MyActiveRecordModelTransition
Expand All @@ -33,18 +45,6 @@ def state_machine
def metadata
super || {}
end

def reload(*)
state_machine.reset
super
end
end

class MyActiveRecordModelTransition < ActiveRecord::Base
include Statesman::Adapters::ActiveRecordTransition

belongs_to :my_active_record_model
serialize :metadata, JSON
end

class MyActiveRecordModelTransitionWithoutInclude < ActiveRecord::Base
Expand Down

0 comments on commit 455a21d

Please sign in to comment.