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

Scopes on associations #268

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

dturnerTS
Copy link
Contributor

Punch a hole in the association_proxy for scopes. Also inject the parents id if needed.

Addresses #215

@mathieujobin
Copy link

LGTM 👍

@kinopyo
Copy link

kinopyo commented Jul 15, 2014

good job 👍

@@ -228,6 +236,11 @@
params[:comments].length.should eq(2)
end

it 'supports scopes on assoications' do
locked_comments = @user_with_included_data.comments.locked_scope
Copy link

Choose a reason for hiding this comment

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

i found this call will append another user_id as parameter in the end..

/users/1/comments?locked=true&user_id=1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. I'm not sure there is currently a good way to prevent that from happening. Any suggestions?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is fixed now.

Copy link

Choose a reason for hiding this comment

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

thanks, confirmed the behavior 👍

…Take advantage of the fact when using scopes on associations to prevent the parent id from bing in the params.
@chewi
Copy link
Contributor

chewi commented Aug 25, 2014

I was hoping this would work for custom_get (which are kinda like server-side scopes) but it doesn't because of this bit of evilness in the scope method.

          # Add the scope method to the Relation class
          Relation.instance_eval do
            define_method(name) { |*args| instance_exec(*args, &code) }
          end

@remiprev, is this really necessary? Can't the relation fall back to the model's class methods in method_missing? I know ActiveRecord does this, though admittedly it is a bit messy when deciding whether methods like find should go to the collection or the model class.

@boie0025
Copy link

👍

@dturnerTS
Copy link
Contributor Author

@hubert Anything I can do to help get this merged? Also, thank you for stepping up to help maintain Her.

@hubert
Copy link
Contributor

hubert commented May 15, 2015

@dturnerTS i've blocked out some time on saturday (tomorrow) to work through pending pull requests.

thanks for the kind words. happy to contribute back to the gem.

@chewi
Copy link
Contributor

chewi commented Sep 30, 2016

@edtjones, if you're looking at this one, I'd really appreciate having a chat with you about it.

@edtjones
Copy link
Collaborator

@chewi definitely, and looks really useful. Need to absorb fully. Are there any downsides / expected failure modes which mean we should be careful about merging this in?

@chewi
Copy link
Contributor

chewi commented Sep 30, 2016

I'd like it to work more like it does in Active Record, where you can call any class method through an association. The code I quoted above highlights the difference between Her and Active Record. I'm a little hazy on the details after all this time but Active Record is very mature now and I think we should strive to use the same approaches wherever possible as they have clearly served it well.

@shashiprab
Copy link

Any plans on merging this PR?

@edtjones
Copy link
Collaborator

hi @shashiprab @chewi this has been hanging around for ages - sorry! Don't have the mental bandwidth to deal with the implications of merging this at the moment but I would be delighted if someone else could review.

@zacharywelch
Copy link
Collaborator

Remember happily applying this patch a few years back and eventually moving a few things around. Also have a rough patch that would let us remove collection_path as @kinopyo pointed out.

class User
  include Her::Model
  has_many :comments
end

class Comment
  include Her::Model
  belongs_to :user
  scope :approved, -> { where(approved: true) }
end

> user = User.find(1)
# GET "/users/1"
> user.comments.approved
# GET "/users/1/comments?approved=true"

I'll put a PR together soon.

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.

9 participants