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

Refactor fallbacks plugin #433

Merged
merged 3 commits into from
Aug 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
([#424](https://github.com/shioyama/mobility/pull/424))
- Pass `model_class` to `Mobility::Backend#configure` via class method rather
than on options hash ([#429](https://github.com/shioyama/mobility/pull/429))
- Remove `Mobility.new_fallbacks` and `Configuration#fallbacks_generator`
([#433](https://github.com/shioyama/mobility/pull/433))

## 0.8

Expand Down
6 changes: 0 additions & 6 deletions lib/mobility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class Error < StandardError
require "mobility/backend"
require "mobility/backends"
require "mobility/configuration"
require "mobility/fallbacks"
require "mobility/plugin"
require "mobility/plugins"
require "mobility/attributes"
Expand Down Expand Up @@ -113,11 +112,6 @@ def config
end
end

# (see Mobility::Configuration#new_fallbacks)
def new_fallbacks(*args)
config.public_send(:new_fallbacks, *args)
end

# Configure Mobility
# @yield [Mobility::Configuration] Mobility configuration
def configure
Expand Down
18 changes: 0 additions & 18 deletions lib/mobility/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,6 @@ def plugins(*args, &block)
attributes_class.plugins(&block)
end

# Generate new fallbacks instance
# @note This method will call the proc defined in the variable set by the
# +fallbacks_generator=+ setter, passing the first argument to its `call`
# method. By default the generator returns an instance of
# +I18n::Locale::Fallbacks+.
# @param fallbacks [Hash] Fallbacks hash passed to generator
# @return [I18n::Locale::Fallbacks]
def new_fallbacks(fallbacks = {})
@fallbacks_generator.call(fallbacks)
end

# Assign proc which, passed a set of fallbacks, returns a default fallbacks
# instance. By default this is a proc which takes fallbacks and returns an
# instance of +I18n::Locale::Fallbacks+.
# @param [Proc] fallbacks generator
attr_writer :fallbacks_generator
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm removing this, I doubt it's almost ever used and it's cluttering multiple unrelated parts of the codebase. Stuff like this will be configurable by overriding plugin methods on the Mobility::Attributes subclass. I've added a spec showing how to do this. Once I finish removing Configuration, this wil be easier to do directly from Mobility.configure.


# Default backend to use (can be symbol or actual backend class)
# @return [Symbol,Class]
def default_backend
Expand All @@ -48,7 +31,6 @@ def default_backend

def initialize
@accessor_method = :translates
@fallbacks_generator = lambda { |fallbacks| Mobility::Fallbacks.build(fallbacks) }
end

def attributes_class
Expand Down
28 changes: 0 additions & 28 deletions lib/mobility/fallbacks.rb

This file was deleted.

24 changes: 19 additions & 5 deletions lib/mobility/plugins/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,25 @@ module Fallbacks
# on model if option is +false+.
included_hook do |_, backend_class|
fallbacks = options[:fallbacks]
backend_class.include(Methods.new(fallbacks)) unless fallbacks == false
backend_class.include(BackendReader.new(fallbacks, method(:generate_fallbacks))) unless fallbacks == false
end

class Methods < Module
def initialize(fallbacks_option)
private

def generate_fallbacks(fallbacks)
fallbacks_class = I18n.respond_to?(:fallbacks) ? I18nFallbacks : I18n::Locale::Fallbacks
fallbacks_class.new(fallbacks)
end

class I18nFallbacks < ::I18n::Locale::Fallbacks
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking maybe this should be its own plugin....

def [](locale)
super | I18n.fallbacks[locale]
end
end

class BackendReader < Module
def initialize(fallbacks_option, fallbacks_generator)
@fallbacks_generator = fallbacks_generator
define_read(convert_option_to_fallbacks(fallbacks_option))
end

Expand All @@ -157,9 +171,9 @@ def define_read(fallbacks)

def convert_option_to_fallbacks(option)
if option.is_a?(::Hash)
Mobility.new_fallbacks(option)
@fallbacks_generator[option]
elsif option == true
Mobility.new_fallbacks
@fallbacks_generator[{}]
else
::Hash.new { [] }
end
Expand Down
4 changes: 0 additions & 4 deletions spec/mobility/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
describe Mobility::Configuration do
subject { Mobility::Configuration.new }

it "initializes new fallbacks instance to I18n::Locale::Fallbacks.new" do
expect(subject.new_fallbacks).to be_a(I18n::Locale::Fallbacks)
end

it "sets default_backend to nil" do
expect(subject.default_backend).to eq(nil)
end
Expand Down
24 changes: 24 additions & 0 deletions spec/mobility/plugins/fallbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,28 @@
expect(backend.read(:'en-US', fallback: true)).to eq(nil)
end
end

# We've taken away the ability to customize the fallbacks generator from
# configuration, but it is still possible by overriding a private method on
# the Attributes class.
describe "overriding fallbacks generator" do
plugin_setup do
fallbacks true
end

before do
attributes_class.class_eval do
private

def generate_fallbacks(_)
Hash.new([])
end
end
end

it "uses overridden fallbacks generator" do
expect(listener).to receive(:read).once.with(:'en-US', any_args).and_return(nil)
expect(backend.read(:'en-US', fallback: true)).to eq(nil)
end
end
end