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

Support for reifying belongs_to relationships #730

Merged
merged 1 commit into from Mar 29, 2016

Conversation

theRealNG
Copy link
Contributor

Support for reifying belongs_to relationships.
#503

So you can call @project.versions.last.reify( belongs_to: true) and get the client model at the time of last change of the project model.

@theRealNG
Copy link
Contributor Author

Need help. RuboCop tests are failing because of Cyclomatic and Perceived complexity for reify is too high. Don't know how to fix it.

belongs_to: false))
end

model.send("#{assoc.name}=".to_sym, collection)
Copy link
Member

Choose a reason for hiding this comment

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

to_sym isn't necessary here -- send automatically does that for you

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the "send" method in ruby v1.9.3 accepts only a symbol.

@jaredbeck
Copy link
Member

Need help. RuboCop tests are failing because of Cyclomatic and Perceived complexity for reify is too high. Don't know how to fix it.

I'm sure we can find a way to extract a private method. (http://refactoring.com/catalog/extractMethod.html)

@jaredbeck
Copy link
Member

I'm sure we can find a way to extract a private method. (http://refactoring.com/catalog/extractMethod.html)

This should help: #734

Oh!, I see you're also working on that?

@jaredbeck
Copy link
Member

Oh!, I see you're also working on that?

Haha, we came up with the same thing :D

@theRealNG theRealNG force-pushed the issue_503 branch 2 times, most recently from 7cf94a3 to 11c23f0 Compare March 14, 2016 00:13
@theRealNG
Copy link
Contributor Author

@jaredbeck Refactored the "reify_associations" method to return the model, so that in future we can use it in any other place if required.

@jaredbeck jaredbeck changed the title Fix for Issue 503 Support for reifying belongs_to relationships Mar 14, 2016
collection = if version.nil?
assoc.klass.where(assoc.klass.primary_key => collection_key).first
elsif version.event == "create"
options[:mark_for_destruction] ? collection.mark_for_destruction : nil
Copy link
Member

Choose a reason for hiding this comment

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

Is there a test that covers this line? It seems like the reference to collection here would raise a NameError. What am I missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jaredbeck
Nice catch. But I don't think this condition is ever met as the base model is always created before the associated model. Ex:- Widget instance is created before the wotsit instance.

There is a case when this condition is met, i.e when a new widget is created and the association is changed to point to the new widget.
In this case we should not destroy the widget instance as widget instance is not dependent on the wotsit instance.

I have written tests which covers the above case.

@jaredbeck
Copy link
Member

Please rebase on master, squash commits, and add a changelog entry. Thanks.

@theRealNG
Copy link
Contributor Author

@jaredbeck Done

@@ -55,20 +56,10 @@ def reify(version, options)
reify_attributes(model, version, attrs)
model.send "#{model.class.version_association_name}=", version
reify_associations(model, options, version)
model
Copy link
Member

Choose a reason for hiding this comment

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

I think I'd rather keep the explicit return value here, instead of moving it to reify_associations.

@jaredbeck
Copy link
Member

I've read through your tests and they look very thorough, thanks!

Other than my minor point about the return value of reify_associations, I think this is ready to merge.

@theRealNG
Copy link
Contributor Author

@jaredbeck Changes the the return value of reify_associations as suggested.

@jaredbeck jaredbeck merged commit 0fdee5c into paper-trail-gem:master Mar 29, 2016
@jaredbeck
Copy link
Member

Merged. Thanks NG! Please submit a changelog entry when you get a chance.

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.

None yet

3 participants