json_api_client icon indicating copy to clipboard operation
json_api_client copied to clipboard

Custom QueryBuilder doesn't add methods to base class

Open coreyward opened this issue 9 years ago • 1 comments

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

coreyward avatar Feb 27 '16 18:02 coreyward

@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 😄

JohnBDonner avatar Jul 26 '17 15:07 JohnBDonner