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.
233
234
235
236
237
|
# File 'lib/crud_components/builder.rb', line 233
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
286
287
288
|
# File 'lib/crud_components/builder.rb', line 286
def method_missing(name, *)
raise DefinitionError, "#{where}: unknown facet '#{name}' — facets are render, filter and sort"
end
|
Instance Method Details
#collect(&block) ⇒ Object
239
240
241
242
|
# File 'lib/crud_components/builder.rb', line 239
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).
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/crud_components/builder.rb', line 261
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
244
245
246
247
248
249
250
251
252
|
# File 'lib/crud_components/builder.rb', line 244
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
290
|
# File 'lib/crud_components/builder.rb', line 290
def respond_to_missing?(_name, _include_private = false) = true
|
#sort(arg = NONE, &block) ⇒ Object
275
276
277
278
279
280
281
282
283
284
|
# File 'lib/crud_components/builder.rb', line 275
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
|