-
Notifications
You must be signed in to change notification settings - Fork 82
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
Disable fallbacks when using locale/fallthrough accessors #86
Conversation
@@ -284,7 +284,7 @@ def save | |||
expect(article.title).to eq("Title") | |||
expect(article.changed?).to eq(true) | |||
expect(article.changed).to match_array(["title_ja", "title_en"]) | |||
expect(article.changes).to eq({ "title_ja" => [nil, "ばばば"], "title_en" => ["ばばば", "Title"]}) | |||
expect(article.changes).to eq({ "title_ja" => [nil, "ばばば"], "title_en" => [nil, "Title"]}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a byproduct, this change also makes dirty/fallbacks compatibility a bit less weird.
@pwim see what you think. |
90ebe75
to
10466fd
Compare
The downside to this approach is that it creates a dependency between the locale_accessors and fallbacks plugins. locale_accessors needs to know about the fallbacks plugin because the fallbacks plugin removes the ability to ask for a fallback in a given language Mobility.locale = :ja
word = Word.create(name: "モビリティ", meaning: "(名詞):動きやすさ、可動性")
word.name(locale: :de)
#=> "モビリティ" Personally, I think the following behaviour would be cleaner: Mobility.locale = :ja
word = Word.create(name: "モビリティ", meaning: "(名詞):動きやすさ、可動性")
word.name(locale: :de)
#=> nil
Mobility.locale = :de
word.name
#=> "モビリティ" i.e., the fallbacks would only be used when the locale is implied by the global one, not explicit. If this was the case, then it would be possible to implement the accessors in a way that doesn't explicitly couple it to the fallback plugin. |
Ah, interesting. You're right that passing |
So this is the way I can envision implementing this. Currently, whether the locale is passed into a reader or writer with Something like this: define_method attribute do |**options|
return super() if options[:super]
locale = options[:locale] || Mobility.locale
Mobility.enforce_available_locales!(locale)
options[:locale] &&= !!locale
mobility_backend_for(attribute).read(locale.to_sym, options)
end So ignoring the The changes to locale/fallthrough accessors in this PR can then be reverted since we don't need them, and instead fallbacks has to look for I think using the |
This approach sounds good to me. I think setting |
13a3642
to
34a1b8b
Compare
014bcea
to
6ba83cf
Compare
@pwim I've merged this into master, give it a try and let me know what you think. I'm debating whether to release it as 0.2.3 or combine it with other changes in the next minor version 0.3... |
Awesome! After pointing my Gemfile to point to master, applying the workaround described in #84, and working around two differences with globalize, I've switched Doorkeeper to use mobility. It's live now, with no immediately obvious issues. Seeing as this is making an incompatible API change, semantic versioning suggests we should release a new minor version. By the way, I discovered two slight differences with globalize:
I think eventually you'll want to make a guide for migrating from Globalize to Mobility, so I wanted to report these as they might be worth noting. Though, they might be too specific to bother with, and won't make a difference for most people. |
@pwim Thanks for investigating! And glad to hear you're using Mobility in production! That's great news. About the two differences, the first one is a conscious decision, but the second one should not happen as long as you have the "presence" plugin enabled (it's enabled by default). Post.new(title_ja: "").title
=> nil This is what I get with a model like this: class Post < ApplicationRecord
extend Mobility
translates :title, backend: :table, locale_accessors: true
end Did you set the If you want to just override specific option values, rather than the entire hash, you should set each value on its own: Mobility.configure do |config|
# ...
config.default_options[:locale_accessors] = true
config.default_options[:type] = :string
end |
The other thing missing from Globalize compatibility (which is harder to implement) is #51. |
Fixes #85