Class: CafeCar::Table::LabelBuilder
Instance Attribute Summary
Attributes inherited from Builder
#ui
Instance Method Summary
collapse
#model, #policy
Methods inherited from Builder
#cell, #has?, #html_safe?, #logo, #model_name, #remaining, #remaining_attributes, #shown, #shown!, #timestamp_attribute, #timestamps, #to_s, #~@
#assign_option!, #assign_options!, #get_options
#call_procs!, #clone_or_call!
Constructor Details
Returns a new instance of LabelBuilder.
3
4
5
6
7
8
|
# File 'lib/cafe_car/table/label_builder.rb', line 3
def initialize(...)
super
@method = @options.delete(:method) { raise }
@key = @method.to_s
@key += ?. + title if belongs_to?
end
|
Instance Method Details
#association? ⇒ Boolean
15
|
# File 'lib/cafe_car/table/label_builder.rb', line 15
def association? = field.association?
|
#belongs_to? ⇒ Boolean
16
|
# File 'lib/cafe_car/table/label_builder.rb', line 16
def belongs_to? = reflection&.belongs_to?
|
#column? ⇒ Boolean
12
|
# File 'lib/cafe_car/table/label_builder.rb', line 12
def column? = @objects.columns_hash.key? @key
|
#content ⇒ Object
45
|
# File 'lib/cafe_car/table/label_builder.rb', line 45
def content = @template.safe_join([ label, label_sort ], " ".html_safe)
|
#existing ⇒ Object
18
|
# File 'lib/cafe_car/table/label_builder.rb', line 18
def existing = params.fetch(:sort) { order_value }
|
#field ⇒ Object
10
|
# File 'lib/cafe_car/table/label_builder.rb', line 10
def field = policy.info(@method)
|
#href ⇒ Object
28
29
30
|
# File 'lib/cafe_car/table/label_builder.rb', line 28
def href
@template.url_for(**request.params.merge(sort:)) if sortable?
end
|
#label ⇒ Object
44
|
# File 'lib/cafe_car/table/label_builder.rb', line 44
def label = @method&.then { present(@objects).human(_1) }
|
#label_sort ⇒ Object
32
33
34
|
# File 'lib/cafe_car/table/label_builder.rb', line 32
def label_sort
@template.tag.span(symbol) if sortable?
end
|
#order_value ⇒ Object
17
|
# File 'lib/cafe_car/table/label_builder.rb', line 17
def order_value = @objects.order_values.first&.then { _1.expr.name }
|
#reflection ⇒ Object
14
|
# File 'lib/cafe_car/table/label_builder.rb', line 14
def reflection = field.reflection
|
#sort ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/cafe_car/table/label_builder.rb', line 20
def sort
@sort ||=
case existing
when @key then "-#{existing}"
else @key
end
end
|
#sortable? ⇒ Boolean
13
|
# File 'lib/cafe_car/table/label_builder.rb', line 13
def sortable? = column? || belongs_to?
|
#symbol ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/cafe_car/table/label_builder.rb', line 36
def symbol
case existing
when @key then "↑" when "-#{@key}" then "↓" else "•"
end
end
|
#title ⇒ Object
11
|
# File 'lib/cafe_car/table/label_builder.rb', line 11
def title = reflection&.klass&.then { policy(_1).title_attribute&.to_s } rescue ""
|
#to_html ⇒ Object
46
|
# File 'lib/cafe_car/table/label_builder.rb', line 46
def to_html = @template.link_to_unless(!sortable?, content, href)
|