Skip to content

Fix duping for AR KeyValue backend - #126

Merged
shioyama merged 1 commit into
masterfrom
ar_key_value_dup
Dec 3, 2017
Merged

Fix duping for AR KeyValue backend#126
shioyama merged 1 commit into
masterfrom
ar_key_value_dup

Conversation

@shioyama

@shioyama shioyama commented Dec 3, 2017

Copy link
Copy Markdown
Owner

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 }

@shioyama shioyama Dec 3, 2017

Copy link
Copy Markdown
Owner Author

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
Copy Markdown
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