Class: FComponents::Table::Component

Inherits:
Base
  • Object
show all
Defined in:
app/components/f_components/table/component.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

call

Methods included from ComponentsHelper

#component, #fcomponent

Constructor Details

#initialize(options) ⇒ Component

Returns a new instance of Component.



6
7
8
9
10
11
12
13
14
# File 'app/components/f_components/table/component.rb', line 6

def initialize(options)
  @options = options
  @values = {}
  @actions = []
  @check_boxes = []
  @main_column = nil
  @current_resource = nil
  @current_resource_index = 0
end

Class Method Details

.for(resources, **options, &block) ⇒ Object



16
17
18
19
20
21
# File 'app/components/f_components/table/component.rb', line 16

def self.for(resources, **options, &block)
  table = new(options)
  table.build(resources, &block)

  table
end

Instance Method Details

#actionObject



50
51
52
53
54
55
# File 'app/components/f_components/table/component.rb', line 50

def action
  action = yield(@current_resource)

  @actions[@current_resource_index] ||= []
  @actions[@current_resource_index] << action
end

#build(resources) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'app/components/f_components/table/component.rb', line 57

def build(resources)
  @resources = resources.presence || [nil]
  @resources.each_with_index do |resource, i|
    @current_resource = resource
    @current_resource_index = i

    yield(self, @current_resource)
  end
end

#check_box(name:, value:, checked: false, **options) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/components/f_components/table/component.rb', line 35

def check_box(name:, value:, checked: false, **options)
  value = value.(@current_resource) if value.respond_to? :call
  checked = checked.(@current_resource) if checked.respond_to? :call

  parsed_options = options

  options.each do |k, v|
    parsed_options[k] = v.(@current_resource) if v.respond_to? :call
  end

  parsed_options[:id] ||= "#{sanitize_to_id(name)}#{value}"

  @check_boxes << check_box_tag(name, value, checked, **parsed_options)
end

#column(name, id: nil, main: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'app/components/f_components/table/component.rb', line 23

def column(name, id: nil, main: false)
  if main
    @main_column = main.is_a?(Proc) ? main : name
  end

  @values[name] ||= []

  return if @current_resource.blank?

  @values[name] << { id: id, column_index: @current_resource_index, cell_content: yield(@current_resource).to_s }
end