Is there a way, using Squeel, to refer to already existing scopes?
Consider the following:
scope :continuous, where{ job_type_id == 1 }
scope :standard, where{ job_type_id == 2 }
scope :active, where{ (job_status_id == 2) & ((job_type_id == 1) | ((job_type_id == 2) & (date_start > Time.now) & (date_end < Time.now))) }
All three scopes work properly, but the logic from the first two (continuous
and standard
) are duplicated within the third, which is what I'd like to avoid, by doing something like:
scope :active, where{ (job_status_id == 2) & (continuous | (standard & (date_start > Time.now) & (date_end < Time.now))) }
... except that I can't find the correct syntax in the Squeel DSL for referring to named scopes.
Is there a way to do what I'd like, or do I just need to be explicit?
Squeel currently doesn't support referencing named scopes. The preferred method is using Squeel sifters and then using the sifters in your scopes:
sifter :continuous { where{ job_type_id == 1 }}
sifter :standard { where{ job_type_id == 2 }}
scope :continuous, -> { where{ sift(:continuous) }}
scope :standard, -> { where{ sift(:standard) }}
scope :active, -> { where{ (job_status_id == 2) & (sift(:continuous) | (sift(:standard) & (date_start > Time.now) & (date_end < Time.now)) }}
Clearly still some repetition, and probably not the best example or use, but just wanted to show how to implement your example with them.
Reference sifters: https://github.com/ernie/squeel#sifters