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 duping for AR KeyValue backend #126

Merged
merged 1 commit into from
Dec 3, 2017
Merged

Fix duping for AR KeyValue backend #126

merged 1 commit into from
Dec 3, 2017

Conversation

shioyama
Copy link
Owner

@shioyama shioyama commented Dec 3, 2017

Fixes #123

super(source)
self.send("#{association_name}=", source.send(association_name).map(&:dup))
# Set inverse on associations
send(association_name).each { |translation| translation.translatable = self }
Copy link
Owner Author

@shioyama shioyama Dec 3, 2017

Choose a reason for hiding this comment

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

If I leave out the spec checking that changing the attribute on one instance does not affect the value on the other instance, then this can be simplified to just:

define_method :initialize_dup do |source|
  super(source)
  self.send("#{association_name}=", source.send(association_name))
end

i.e. you don't need to dup the association objects, nor set the inverse.

However, I personally think that it's better that the dup'ed instance does not affect the original and vice versa. However, Sequel (for example) does not "deep dup" for any pattern here used in the backends... I suppose because there's a performance cost, and Sequel always defaults to not incurring any performance costs.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Never mind, we definitely want duplicate models to have their own translations with AR at least. I think this is the correct way to fix this.

@shioyama shioyama merged commit 1b14fe6 into master Dec 3, 2017
@shioyama shioyama deleted the ar_key_value_dup branch December 4, 2017 13:48
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