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

Fix issues with dirty plugin by dynamically defining dirty methods #348

Merged
merged 2 commits into from
Oct 24, 2019

Conversation

shioyama
Copy link
Owner

@shioyama shioyama commented Oct 21, 2019

Resolving issues raised in #347, for master branch. For a summary see here, and 011575d.

@shioyama shioyama changed the title Ensure saved_changes includes both translated and untranslated attributes Fix issues with dirty plugin by dynamically defining dirty methods Oct 23, 2019
@shioyama
Copy link
Owner Author

@pwim This should roughly do it, minus a few more clean up changes. This is still on master, I'll update the other PR once this is passing and working.

As described earlier, naming methods on the tracker carefully pays off because we can dynamically define all the attribute methods using introspection and just delegate to the tracker now. Seems to work ok in all versions, and should quietly support future changes as well.

@shioyama shioyama force-pushed the fix_saved_changes branch 2 times, most recently from 321f6ce to db48ec7 Compare October 23, 2019 11:28
Previous to the dirty module refactor, attribute handlers like
+attribute_changed?+ were supported, but with that change they became
unsupported. We need to support the public ones at least.

First, let's lay down good test coverage before making more changes.
Also, ensure that methods which include both translated and untranslated
attributes are tested against untranslated attribute changes as well, to
ensure we haven't broken any basic functionality.
…odules

The dirty tracking methods defined by ActiveModel and ActiveRecord
change from version to version, which makes it tricky to correctly
support all of them for translated attributes. Rather than adding many
version conditionals to Mobility code, we can instead dynamically
determine the dirty method patterns by including the AR/AM dirty module
into a dummy class, and extracting the attribute methods from that
class. We can then use those patterns to define Mobility dirty methods.

However, to support both methods like +title_changed?+ and *also*
+attribute_changed?(:title)+, we need to streamline things a bit. We can
do this by changing the naming of methods on the Mobility mutation
tracker such that its methods correspond exactly to the attribute
handler methods. Doing this allows us to dynamically define both handler
method overrides and dirty methods on translated attributes with some
simple metaprogramming, in a way that will be mostly robust to changes
in Rails (just need to add new methods to the dirty tracker to support
new dirty methods on AM/AR).

The result is that the Mobility ActiveRecord plugin becomes much
simpler, whereas the ActiveModel one becomes slightly more complex,
although most of that added complexity is in the mutation tracker, which
is easy to understand. Tests are now very important since we are
defining many more overrides than before (attribute handlers were not
previously being overridden, but now we have no choice but to do so.)
@shioyama
Copy link
Owner Author

Ok, squashed commits and added more test coverage. Once specs pass, I'll merge and update (or maybe replace) #347, then we can finally release this as 0.8.9.

@shioyama shioyama merged commit 011575d into master Oct 24, 2019
@github-pages github-pages bot temporarily deployed to github-pages October 24, 2019 05:05 Inactive
@shioyama
Copy link
Owner Author

Merged! Now just need to rebase 0-8-stable...

@shioyama shioyama deleted the fix_saved_changes branch October 24, 2019 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant