Class: AhoyCaptain::Tables::DynamicTableComponent::TableDefinition::Row

Inherits:
Object
  • Object
show all
Defined in:
app/components/ahoy_captain/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/ahoy_captain/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



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

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/ahoy_captain/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/ahoy_captain/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/ahoy_captain/tables/dynamic_table_component.rb', line 7

def view_context
  @view_context
end

Instance Method Details

#build_option(item, value) ⇒ Object



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

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

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



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

def column(value = nil, &block)
  view_context.(:span, class: "w-8 ml-8 text-right") 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



43
44
45
# File 'app/components/ahoy_captain/tables/dynamic_table_component.rb', line 43

def h
  @view_context.helpers
end

#headerObject



31
32
33
# File 'app/components/ahoy_captain/tables/dynamic_table_component.rb', line 31

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

#klassObject



60
61
62
# File 'app/components/ahoy_captain/tables/dynamic_table_component.rb', line 60

def klass
  @options[:klass]
end


35
36
37
# File 'app/components/ahoy_captain/tables/dynamic_table_component.rb', line 35

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

#number_to_human(amount) ⇒ Object



114
115
116
117
118
# File 'app/components/ahoy_captain/tables/dynamic_table_component.rb', line 114

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



120
121
122
# File 'app/components/ahoy_captain/tables/dynamic_table_component.rb', line 120

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

#progress_bar(item) ⇒ Object



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

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-base-100 h-8 grow", value: value, max: max)
  items << view_context.(:span, class: "grow text-elipsis overflow-hidden absolute left-4 bottom-3 h-8 text-primary-content") do
    label
  end

  items.join.html_safe
end

#render(item, view_context) ⇒ Object



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

def render(item, view_context)
  @view_context = view_context
  @item = item
  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



39
40
41
# File 'app/components/ahoy_captain/tables/dynamic_table_component.rb', line 39

def request
  @view_context.request
end

#search_paramsObject



47
48
49
# File 'app/components/ahoy_captain/tables/dynamic_table_component.rb', line 47

def search_params
  @view_context.search_params
end

#tooltip(value) ⇒ Object



124
125
126
# File 'app/components/ahoy_captain/tables/dynamic_table_component.rb', line 124

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