Skip to content

Commit

Permalink
I18nCache: Fix overtaken class var RuntimeError in Ruby 3.0
Browse files Browse the repository at this point in the history
This fixes errors like the following in Ruby 3.0:

    RuntimeError: class variable @@translate_required_html of SimpleForm::Inputs::StringInput is overtaken by SimpleForm::Inputs::Base
      .../simple_form/lib/simple_form/i18n_cache.rb:13:in `class_variable_get'
      .../simple_form/lib/simple_form/i18n_cache.rb:13:in `get_i18n_cache'
      .../simple_form/lib/simple_form/i18n_cache.rb:8:in `i18n_cache'
      .../simple_form/lib/simple_form/components/labels.rb:9:in `translate_required_html'
      .../simple_form/lib/simple_form/components/labels.rb:71:in `required_label_text'
      .../simple_form/lib/simple_form/components/labels.rb:43:in `label_text'
      .../simple_form/lib/simple_form/components/labels.rb:35:in `label'
  • Loading branch information
mcls committed Jan 5, 2021
1 parent 3ee670b commit 168c249
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/simple_form/i18n_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@ module SimpleForm
# caching facility to speed up form construction.
module I18nCache
def i18n_cache(key)
get_i18n_cache(key)[I18n.locale] ||= yield.freeze
store = I18nCacheStore.instance
store[key] ||= {}
store[key][I18n.locale] ||= yield.freeze
end

def get_i18n_cache(key)
if class_variable_defined?(:"@@#{key}")
class_variable_get(:"@@#{key}")
else
reset_i18n_cache(key)
end
def reset_i18n_cache(key)
I18nCacheStore.instance[key] = {}
end
end

def reset_i18n_cache(key)
class_variable_set(:"@@#{key}", {})
class I18nCacheStore
include Singleton

def initialize
@store ||= {}
end

def [](key)
@store[key]
end

def []=(key, value)
@store[key] = value
end
end
end

0 comments on commit 168c249

Please sign in to comment.