Do not mark attribute as changed if value is the same#2
Do not mark attribute as changed if value is the same#2
Conversation
f455079 to
11c2981
Compare
|
While working on this fix I noticed that there is a tricky relationship between dirty tracking and fallbacks. If I have a model class Article
include Mobility
translates :title, dirty: true, fallbacks: { en: 'ja' }
endand I set the value in Japanese, the value in English will fall through to this value: article = Article.new
article.title
#=> nil
Mobility.with_locale(:ja) { article.title = "タイトル" }
article.title
#=> "タイトル"In the dirty module, when we check the current value of the attribute in the current locale, we need to disable fallbacks, otherwise we will compare with the fallback value and mark a change when we set to the same blank value: article.title = nil
article.changed
#=> ["title_ja", "title_en"]We don't want this - i.e., while I may want to fall through to a different locale, I don't want to compare with the value in that locale when marking attributes in the current locale as having changed or not. If instead we disable fallbacks ( article.title = nil
article.changed
#=> ["title_ja"]Only the value in Japanese has changed (from However, when we do update the value, do we mark the change as a change from i.e. article.title = "title"
article.changes
#=> { "title_ja" => [nil, "タイトル"], "title_en" => ["タイトル", "title"] }Or should it be marked as a change from I have gone with the former (change from fallback value), becuase: a) to make it do otherwise would require hacking hte internals of Rails/Sequel, which I don't want to do, b) I think intuitively, this sort of makes sense. |
1f127a1 to
e8aed90
Compare
(Note: I'm starting to post bug fixes and important changes as pull requests to
make them more visible.)
Currently, for a model post with dirty tracking enabled, this is what happens:
So far so good, but if we set the value to
nil:This is a bug, which this PR fixes. After the change:
Note: the change applies to both ActiveModel and Sequel modules.