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

Using Query object as scope doesn't seem to allow composition #31

Open
perezperret opened this issue Jun 23, 2020 · 1 comment
Open

Comments

@perezperret
Copy link

To be clear, I'm using your implementation as a reference and not importing it directly, so it's likely I'm missing something!

If I use a query object like this:

module Transactions
  class AcceptedQuery < Patterns::Query
    queries Transaction

    private

    def query
      relation.active.where(accepted_at: 5.days.ago..DateTime.current)
    end
  end
end
class Transaction < ActiveRecord::Base
  scope :latest, -> { order(created_at: :desc) }
  scope :query_object_scope, Transactions::AcceptedQuery
end

If I try to compose the scopes:

Transaction.latest.accepted

Transactions::AcceptedQuery.call will not receive Transaction.latest as the scope argument, but rather it's base_relation ie: Transaction.all. Am I looking at this right? Is there a workaround for this other than using a class method?

I like the elegance of scope :accepted, Transactions::AcceptedQuery but I can't get it to work.

Thank you for your time and energy! I've learned a lot from this Repo already!

@perezperret
Copy link
Author

Hi! Sorry for the noise! After doing a bit more research I now understand that scopes are chained correctly with some Rails magic using scoping. This article (although somewhat old) helped me find this code. I leave those for reference. Feel free to close!

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

No branches or pull requests

1 participant