Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #709 : Remove memoization to avoid stale localized state name #716

Merged
merged 5 commits into from Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/aasm/core/event.rb
Expand Up @@ -4,7 +4,7 @@ module AASM::Core
class Event
include AASM::DslHelper

attr_reader :name, :state_machine, :options
attr_reader :name, :state_machine, :options, :default_display_name

def initialize(name, state_machine, options = {}, &block)
@name = name
Expand All @@ -13,6 +13,7 @@ def initialize(name, state_machine, options = {}, &block)
@valid_transitions = {}
@guards = Array(options[:guard] || options[:guards] || options[:if])
@unless = Array(options[:unless]) #TODO: This could use a better name
@default_display_name = name.to_s.gsub(/_/, ' ').capitalize

# from aasm4
@options = options # QUESTION: .dup ?
Expand Down
11 changes: 6 additions & 5 deletions lib/aasm/core/state.rb
Expand Up @@ -2,12 +2,13 @@

module AASM::Core
class State
attr_reader :name, :state_machine, :options
attr_reader :name, :state_machine, :options, :default_display_name

def initialize(name, klass, state_machine, options={})
@name = name
@klass = klass
@state_machine = state_machine
@default_display_name = name.to_s.gsub(/_/, ' ').capitalize
update(options)
end

Expand Down Expand Up @@ -54,11 +55,11 @@ def fire_callbacks(action, record, *args)
end

def display_name
@display_name ||= begin
@display_name = begin
if Module.const_defined?(:I18n)
localized_name
else
name.to_s.gsub(/_/, ' ').capitalize
@default_display_name
end
end
end
Expand All @@ -75,8 +76,8 @@ def for_select
private

def update(options = {})
if options.key?(:display) then
@display_name = options.delete(:display)
if options.key?(:display)
@default_display_name = options.delete(:display)
end
@options = options
self
Expand Down
12 changes: 10 additions & 2 deletions lib/aasm/localizer.rb
Expand Up @@ -5,7 +5,7 @@ def human_event_name(klass, event)
list << :"#{i18n_scope(klass)}.events.#{i18n_klass(ancestor)}.#{event}"
list
end
translate_queue(checklist) || I18n.translate(checklist.shift, :default => event.to_s.humanize)
translate_queue(checklist) || I18n.translate(checklist.shift, :default => default_display_name(event))
end

def human_state_name(klass, state)
Expand All @@ -14,7 +14,7 @@ def human_state_name(klass, state)
list << item_for(klass, state, ancestor, :old_style => true)
list
end
translate_queue(checklist) || I18n.translate(checklist.shift, :default => state.to_s.humanize)
translate_queue(checklist) || I18n.translate(checklist.shift, :default => default_display_name(state))
end

private
Expand Down Expand Up @@ -52,5 +52,13 @@ def ancestors_list(klass)
ancestor.respond_to?(:model_name) && not_active_record_base
end
end

def default_display_name(object) # Can use better arguement name
if object.respond_to?(:default_display_name)
object.default_display_name
else
object.to_s.gsub(/_/, ' ').capitalize
end
end
end
end # AASM
7 changes: 7 additions & 0 deletions spec/localizer_test_model_deprecated_style.yml
Expand Up @@ -4,3 +4,10 @@ en:
localizer_test_model:
aasm_state:
opened: "It's open now!"

fr:
activerecord:
attributes:
localizer_test_model:
aasm_state:
opened: "C'est ouvert maintenant!"
6 changes: 6 additions & 0 deletions spec/localizer_test_model_new_style.yml
Expand Up @@ -3,3 +3,9 @@ en:
attributes:
localizer_test_model:
aasm_state/opened: "It's open now!"

fr:
activerecord:
attributes:
localizer_test_model:
aasm_state/opened: "C'est ouvert maintenant!"
8 changes: 8 additions & 0 deletions spec/models/active_record/localizer_test_model.rb
Expand Up @@ -24,11 +24,19 @@ class LocalizerTestModel < ActiveRecord::Base
state = LocalizerTestModel.aasm.states.detect {|s| s == :opened}
expect(state.localized_name).to eq("It's open now!")
expect(state.human_name).to eq("It's open now!")
expect(state.display_name).to eq("It's open now!")

I18n.with_locale(:fr) do
expect(state.localized_name).to eq("C'est ouvert maintenant!")
expect(state.human_name).to eq("C'est ouvert maintenant!")
expect(state.display_name).to eq("C'est ouvert maintenant!")
end
end

it 'should use fallback' do
state = LocalizerTestModel.aasm.states.detect {|s| s == :closed}
expect(state.localized_name).to eq('Closed')
expect(state.human_name).to eq('Closed')
expect(state.display_name).to eq('Closed')
end
end