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

Custom QueryBuilder doesn't add methods to base class #170

Open
coreyward opened this issue Feb 27, 2016 · 1 comment
Open

Custom QueryBuilder doesn't add methods to base class #170

coreyward opened this issue Feb 27, 2016 · 1 comment

Comments

@coreyward
Copy link
Contributor

Because resource scopes (e.g. where) are delegated to the query builder during the definition of resource, there isn't a way for custom query builders to add methods on the base resource/class.

It would be preferential for there to be some degree of scope definition support (a la ActiveRecord), but maybe there's a reasonable workaround that doesn't require explicit delegation in the resource class?

Today:

class CustomQueryBuilder < JsonAPIClient::Query::Builder
  def recent
    where(created_at_gt: 3.days.ago)
  end
end

class User < API::Base
  self.query_builder = CustomQueryBuilder
  def self.recent
    _new_scope.recent
  end
end

Future?

class User < API::Base
   scope :recent, -> { where(created_at_gt: 3.days.ago) }
end
@JohnBDonner
Copy link

@coreyward I don't think it's necessary to go through the CustomQueryBuilder for that. I think you can just use the following:

class User < API::Base
  def self.recent
    where(created_at_gt: 3.days.ago)
  end
end

Scopes are really just class methods with a query defined in them: http://guides.rubyonrails.org/active_record_querying.html#scopes

I may be misunderstanding your question though 😄

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

2 participants