Class: Lookout::Tables::DynamicTableComponent::TableDefinition::Row

Inherits:
Object
  • Object
show all
Defined in:
app/components/lookout/tables/dynamic_table_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, options = {}, &block) ⇒ Row

Returns a new instance of Row.



9
10
11
12
13
14
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 9

def initialize(attribute, options = {}, &block)
  @attribute = attribute
  @options = options
  @block = block
  @view_context = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

needed for tricky instance_eval with blocks and view_context



53
54
55
56
57
58
59
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 53

def method_missing(method, *args, &block)
  if klass.respond_to?(method)
    klass.send(method, *args, &block)
  else
    @item.send method, *args, &block
  end
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



7
8
9
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 7

def attribute
  @attribute
end

#itemObject (readonly)

Returns the value of attribute item.



8
9
10
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 8

def item
  @item
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



7
8
9
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 7

def view_context
  @view_context
end

Instance Method Details

#build_option(item, value) ⇒ Object



89
90
91
92
93
94
95
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 89

def build_option(item, value)
  if value.is_a?(Symbol)
    item.public_send(value)
  else
    value
  end
end

#column(value = nil, &block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 97

def column(value = nil, &block)
  padding_class = @is_last ? 'pl-4' : 'pl-6'
  view_context.(:span, class: "w-16 text-right border-l-gray border-base-300 #{padding_class} pr-2") do
    if value
      if @options[:formatter]
        if respond_to?(@options[:formatter])
          public_send(@options[:formatter], value)
        else
          view_context.public_send(@options[:formatter], value)
        end
      else
        value.to_s
      end
    else
      view_context.capture(&block).to_s
    end
  end
end

#hObject



44
45
46
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 44

def h
  @view_context.helpers
end

#headerObject



32
33
34
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 32

def header
  @options[:title] || @attribute.to_s.titleize
end

#klassObject



61
62
63
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 61

def klass
  @options[:klass]
end


36
37
38
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 36

def link_to(*args)
  @view_context.link_to(*args)
end

#number_to_human(amount) ⇒ Object



116
117
118
119
120
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 116

def number_to_human(amount)
  tooltip(amount) do
    view_context.number_to_human(amount, format: '%n%u', precision: 2, units: { thousand: 'k', million: 'm', billion: 'b' })
  end
end

#percent_total(item) ⇒ Object



122
123
124
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 122

def percent_total(item)
  '%.1f' % ((item.unit_amount.to_i * 1.0 / total)*100.0)
end

#progress_bar(item) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 65

def progress_bar(item)
  @item = item
  value = if @options[:value]
            build_option(item, @options[:value]).to_s
          else
            item.public_send(@attribute)
          end

  max = build_option(item, @options[:max]).to_s
  label = if @block
            instance_eval(&@block)
          else
            item.public_send(@attribute)
          end.to_s

  items = []
  items << view_context.(:progress, "", class: "progress-primary bg-gray-800 h-8 grow max-w-[calc(100%-2rem)] opacity-60", value: value, max: max)
  items << view_context.(:span, class: "grow text-elipsis overflow-hidden absolute left-4 flex items-center h-8 text-primary-content") do
    label
  end

  items.join.html_safe
end

#render(item, view_context, is_last = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 16

def render(item, view_context, is_last = false)
  @view_context = view_context
  @item = item
  @is_last = is_last
  if @options[:type] == :progress_bar
    progress_bar(item)
  else
    if @block
      column(@block.call(item))
    else
      column(item.public_send(attribute).presence)
    end
  end

end

#requestObject



40
41
42
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 40

def request
  @view_context.request
end

#search_paramsObject



48
49
50
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 48

def search_params
  @view_context.search_params
end

#tooltip(value) ⇒ Object



126
127
128
# File 'app/components/lookout/tables/dynamic_table_component.rb', line 126

def tooltip(value)
  Lookout::TooltipComponent.new(amount: value).render_in(view_context)
end