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

Some refactor for association. #5722

Merged
merged 1 commit into from
Apr 12, 2012

Conversation

kennyj
Copy link
Contributor

@kennyj kennyj commented Apr 3, 2012

I'm tackling #5663.
And I think some coding problems are existed on associations (redundant / class hierarchy of methods...).
This PR fix some problems.
Please review it :)

@kevmoo
Copy link
Contributor

kevmoo commented Apr 3, 2012

@kennyj: I noticed you redefined reset to take in an optional target, but I don't see where this method is used with a non-nil paramater

@kennyj
Copy link
Contributor Author

kennyj commented Apr 4, 2012

@kevmoo kennyj@37cc758#L1R74
I wanted to protect the DRY principle and standardize the reset process :)

@kevmoo
Copy link
Contributor

kevmoo commented Apr 4, 2012

@kennyj DRY'ing and standardizing are great, but where is this method called with a non-nil param?

The other code path that sets @target is target= which also calls loaded!

I'm trying to understand why we'd want a reset code path that sets @target to anything but nil

@carlosantoniodasilva
Copy link
Member

@kevmoo The super [] call on collection association resets the target to an empty array.

@kennyj Do you think other reset overriding should stick with the same method signature?

@kennyj
Copy link
Contributor Author

kennyj commented Apr 4, 2012

@carlosantoniodasilva I think we should use the same method signature possibly (especially public api).

I have another 2 plan.

plan1: we add default_target method.

def reset
  @loaded = false
  @target = default_target
  @stale_state = nil
end

private
  def default_target # override on derived class!
    nil
  end

plan2: without considering the inheritance, we implement reset method separately.

What do you think ?

@kevmoo
Copy link
Contributor

kevmoo commented Apr 4, 2012

@carlosantoniodasilva : I'm w/ you on the call to super it association_collection. I'm trying to understand the new target param on reset in assocition.rb

@carlosantoniodasilva
Copy link
Member

@kenny plan 1 with default_target seems good to me. Another possibility I can think about, in collection association just do:

def reset
  super
  @target = []
end

@kevmoo the new param is required so you can achieve the same result as my example above with super [], which is basically calling reset [] on the parent.

@kennyj
Copy link
Contributor Author

kennyj commented Apr 4, 2012

@carlosantoniodasilva sounds good :-) more simply. I'll update commits. Thank you for reviewing

@kennyj
Copy link
Contributor Author

kennyj commented Apr 4, 2012

done :-)

@kevmoo
Copy link
Contributor

kevmoo commented Apr 4, 2012

Love it. Is there an associated test case?

@kennyj
Copy link
Contributor Author

kennyj commented Apr 4, 2012

cetrainly, we needed to add some testcases.
... and done :-)

@jonleighton
Copy link
Member

This looks fine to me, but I'm not keen on the test. Does this fix any bug? If not, let's just not bother with the test; it's just refactoring.

@kennyj
Copy link
Contributor Author

kennyj commented Apr 12, 2012

@jonleighton Thank you for reviewing.
I agree. I didn't fix any bug.
I rebased and updated this PR.

@jonleighton
Copy link
Member

Ok, please squash the commits :) then I'll merge.

* Remove unused association_class method.
* Remove a unnecessary assignment.
* Move @Updated to BelongsToAssociation that only reference this instance variable.
* Reset @stale_state at the reset method. I think this place is right place.
@kennyj
Copy link
Contributor Author

kennyj commented Apr 12, 2012

@jonleighton
Done !
Please merge it :-)

jonleighton added a commit that referenced this pull request Apr 12, 2012
@jonleighton jonleighton merged commit 65be11f into rails:master Apr 12, 2012
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

4 participants