Class: CrudComponents::Builder::FacetCollector
- Inherits:
-
Object
- Object
- CrudComponents::Builder::FacetCollector
show all
- Defined in:
- lib/crud_components/builder.rb
Constant Summary
collapse
- NONE =
Object.new
Instance Method Summary
collapse
-
#collect(&block) ⇒ Object
-
#filter(*spec, **assoc, &block) ⇒ Object
A like-spec passed positionally (same mini-language as ‘search_in`): filter :title own column filter :title, :subtitle several columns, OR-combined filter authors: %i[name email] join, explicit columns filter :publisher join, delegate to the target’s search_in filter :title, { authors: :name } mixed plus ‘filter false` (off) and `filter { |scope, value| … }` (block).
-
#initialize(model, name) ⇒ FacetCollector
constructor
A new instance of FacetCollector.
-
#method_missing(name) ⇒ Object
-
#render(*args, &block) ⇒ Object
-
#respond_to_missing?(_name, _include_private = false) ⇒ Boolean
-
#sort(arg = NONE, &block) ⇒ Object
Constructor Details
Returns a new instance of FacetCollector.
178
179
180
181
182
|
# File 'lib/crud_components/builder.rb', line 178
def initialize(model, name)
@model = model
@name = name
@facets = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
231
232
233
|
# File 'lib/crud_components/builder.rb', line 231
def method_missing(name, *)
raise DefinitionError, "#{where}: unknown facet '#{name}' — facets are render, filter and sort"
end
|
Instance Method Details
#collect(&block) ⇒ Object
184
185
186
187
|
# File 'lib/crud_components/builder.rb', line 184
def collect(&block)
instance_exec(&block)
@facets
end
|
#filter(*spec, **assoc, &block) ⇒ Object
A like-spec passed positionally (same mini-language as ‘search_in`):
filter :title own column
filter :title, :subtitle several columns, OR-combined
filter authors: %i[name email] join, explicit columns
filter :publisher join, delegate to the target's search_in
filter :title, { authors: :name } mixed
plus ‘filter false` (off) and `filter { |scope, value| … }` (block).
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/crud_components/builder.rb', line 206
def filter(*spec, **assoc, &block)
once!(:filter)
spec << assoc unless assoc.empty?
case
when spec == [false] && !block then @facets[:filter] = false
when block && spec.empty? then @facets[:filter] = block
when !spec.empty? && !block then @facets[:filter] = spec.size == 1 ? spec.first : spec
else
raise DefinitionError, "#{where}: filter takes `false`, a column/association spec " \
'(e.g. `filter :title` or `filter authors: %i[name email]`), or a block'
end
end
|
#render(*args, &block) ⇒ Object
189
190
191
192
193
194
195
196
197
|
# File 'lib/crud_components/builder.rb', line 189
def render(*args, &block)
once!(:render)
unless block && args.empty?
raise DefinitionError, "#{where}: the render facet takes only a block — " \
'named renderers are picked with the as: keyword on attribute'
end
@facets[:render] = block
end
|
#respond_to_missing?(_name, _include_private = false) ⇒ Boolean
235
|
# File 'lib/crud_components/builder.rb', line 235
def respond_to_missing?(_name, _include_private = false) = true
|
#sort(arg = NONE, &block) ⇒ Object
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/crud_components/builder.rb', line 220
def sort(arg = NONE, &block)
once!(:sort)
case
when arg == false && !block then @facets[:sort] = false
when block && arg.equal?(NONE) then @facets[:sort] = block
when arg.is_a?(Symbol) && !block then @facets[:sort] = arg
else
raise DefinitionError, "#{where}: sort takes `false`, an own-column symbol, or a block"
end
end
|