Class: CafeCar::Table::LabelBuilder

Inherits:
ObjectsBuilder show all
Defined in:
lib/cafe_car/table/label_builder.rb

Instance Attribute Summary

Attributes inherited from Builder

#ui

Instance Method Summary collapse

Methods inherited from ObjectsBuilder

#model, #policy

Methods inherited from Builder

#cell, #has?, #html_safe?, #logo, #model_name, #remaining, #remaining_attributes, #shown, #shown!, #timestamp_attribute, #timestamps, #to_s, #~@

Methods included from OptionHelpers

#assign_option!, #assign_options!, #get_options

Methods included from ProcHelpers

#call_procs!, #clone_or_call!

Constructor Details

#initializeLabelBuilder

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

Returns:

  • (Boolean)


15
# File 'lib/cafe_car/table/label_builder.rb', line 15

def association? = field.association?

#belongs_to?Boolean

Returns:

  • (Boolean)


16
# File 'lib/cafe_car/table/label_builder.rb', line 16

def belongs_to?  = reflection&.belongs_to?

#column?Boolean

Returns:

  • (Boolean)


12
# File 'lib/cafe_car/table/label_builder.rb', line 12

def column?      = @objects.columns_hash.key? @key

#contentObject



45
# File 'lib/cafe_car/table/label_builder.rb', line 45

def content = @template.safe_join([ label, label_sort ], " ".html_safe)

#existingObject



18
# File 'lib/cafe_car/table/label_builder.rb', line 18

def existing     = params.fetch(:sort) { order_value }

#fieldObject



10
# File 'lib/cafe_car/table/label_builder.rb', line 10

def field        = policy.info(@method)

#hrefObject



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

#labelObject



44
# File 'lib/cafe_car/table/label_builder.rb', line 44

def label   = @method&.then { present(@objects).human(_1) }

#label_sortObject



32
33
34
# File 'lib/cafe_car/table/label_builder.rb', line 32

def label_sort
  @template.tag.span(symbol) if sortable?
end

#order_valueObject



17
# File 'lib/cafe_car/table/label_builder.rb', line 17

def order_value  = @objects.order_values.first&.then { _1.expr.name }

#reflectionObject



14
# File 'lib/cafe_car/table/label_builder.rb', line 14

def reflection   = field.reflection

#sortObject



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

Returns:

  • (Boolean)


13
# File 'lib/cafe_car/table/label_builder.rb', line 13

def sortable?    = column? || belongs_to?

#symbolObject



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

#titleObject



11
# File 'lib/cafe_car/table/label_builder.rb', line 11

def title        = reflection&.klass&.then { policy(_1).title_attribute&.to_s } rescue ""

#to_htmlObject



46
# File 'lib/cafe_car/table/label_builder.rb', line 46

def to_html = @template.link_to_unless(!sortable?, content, href)