Class: FComponents::Table::Component
- Defined in:
- app/components/f_components/table/component.rb
Class Method Summary collapse
Instance Method Summary collapse
- #action ⇒ Object
- #build(resources) ⇒ Object
- #check_box(name:, value:, checked: false, **options) ⇒ Object
- #column(name, id: nil, main: false) ⇒ Object
-
#initialize(options) ⇒ Component
constructor
A new instance of Component.
Methods inherited from Base
Methods included from ComponentsHelper
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() = @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, **, &block) table = new() table.build(resources, &block) table end |
Instance Method Details
#action ⇒ Object
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, **) value = value.(@current_resource) if value.respond_to? :call checked = checked.(@current_resource) if checked.respond_to? :call = .each do |k, v| [k] = v.(@current_resource) if v.respond_to? :call end [:id] ||= "#{sanitize_to_id(name)}#{value}" @check_boxes << check_box_tag(name, value, checked, **) 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 |