Module: Sequel::Plugins::SubsetConditions
- Defined in:
- lib/sequel/plugins/subset_conditions.rb
Overview
The subset_conditions plugin creates an additional *_conditions method for every ‘subset`, `where`, and `exclude` method call in a dataset_module block. This method returns the filter conditions, which can be useful if you want to use the conditions for a separate filter or combine them with OR. It also supports where_all and where_any dataset_module methods for combining multiple dataset method filters with AND or OR.
Usage:
# Add subset_conditions in the Album class
Album.plugin :subset_conditions
Album.dataset_module do
# This will now create a published_conditions method
where :published, published: true
# This will now create a not_bad_conditions method
exclude :not_bad, :bad
# This will create good_and_available and
# good_and_available_conditions methods
where_all :good_and_available, :published, :not_bad
# This will create good_or_available and
# good_or_available_conditions methods
where_any :good_or_available, :published, :not_bad
end
Album.where(Album.published_conditions).sql
# SELECT * FROM albums WHERE (published IS TRUE)
Album.exclude(Album.published_conditions).sql
# SELECT * FROM albums WHERE (published IS NOT TRUE)
Album.where(Album.published_conditions | {ready: true}).sql
# SELECT * FROM albums WHERE ((published IS TRUE) OR (ready IS TRUE))
Album.good_and_available.sql
SELECT * FROM albums WHERE ((published IS TRUE) AND NOT bad)
Album.good_or_available.sql
SELECT * FROM albums WHERE ((published IS TRUE) OR NOT bad)
Defined Under Namespace
Modules: DatasetModuleMethods
Class Method Summary collapse
Class Method Details
.apply(model, &block) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/sequel/plugins/subset_conditions.rb', line 48 def self.apply(model, &block) model.instance_exec do @dataset_module_class = Class.new(@dataset_module_class) do include DatasetModuleMethods end end end |