has_scope
has_scope copied to clipboard
Feature/enhance readme to support standalone mode
This update enhances the docs a little bit and documents support for HasScope outside of Controllers. I've taken the liberty to rewrite some parts of the docs to make them a little bit clearer by expanding the examples.
@carlosantoniodasilva There was one thing I wasn't able to document entirely and it's support for the current_scopes
method. The only way I found to expose the method inside of a PORO is to call super because it's a protected method but this didn't feel very nice to me. Expanding on the example in this PR's readme:
class GraduationsSearchQuery
include HasScope
has_scope :featured, type: :boolean
has_scope :by_degree
has_scope :by_period, using: %i[started_at ended_at], type: :hash
def initialize; end
def perform(collection:, params: {})
apply_scopes(collection, params)
end
def current_scopes
super
end
end
This allows me to call current scopes on the query object (and maybe I can find a way to expose it as helper to the view):
class GraduationsController < ApplicationController
def index
graduations_query = GraduationsSearchQuery.new
@graduations = graduations_query.perform(collection: Graduation, params: params)
graduations_query.current_scopes
end
end
Do you know of another way to do it?
@pinzonjulian cool, thanks! I'll take a better look in the next few days.
As for your question, another way of doing it is to explicitly change the method visibility, making the method public:
public :current_scopes
Hey @carlosantoniodasilva ! Just a little bump to get this merged in.
Cheers!
@pinzonjulian oops, my bad, this one slipped through the cracks. I've added it to my to-do here to circle back and review it. Thanks a bunch!
Friendly bump again :)
Hi, just passing here to say that this PR helped me to setup standalone scopes, thanks!
Hey! Circling back to ask if you've had time to have a look @carlosantoniodasilva
Is there anything I can do to help you out? I'd be thrilled to become a maintainer if you need a hand on this one!
One last effort to get this looked at!
All done @carlosantoniodasilva
Bumping! All the changes you requested have been added.
@pinzonjulian thanks so much!