Class: Avo::Index::ResourceTableComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
ApplicationHelper
Defined in:
app/components/avo/index/resource_table_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

#a_button, #a_link, #button_classes, #card_classes, #chart_color, #decode_filter_params, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #number_to_social, #render_license_warning, #root_path_without_url, #svg, #white_panel_classes

Methods included from ResourcesHelper

#field_wrapper, #filter_wrapper, #index_field_wrapper, #item_select_all_input, #item_selector_init, #item_selector_input, #resource_grid, #resource_table

Methods inherited from BaseComponent

#has_with_trial

Constructor Details

#initialize(resources: nil, resource: nil, reflection: nil, parent_record: nil, parent_resource: nil, pagy: nil, query: nil, actions: nil) ⇒ ResourceTableComponent

Returns a new instance of ResourceTableComponent.



11
12
13
14
15
16
17
18
19
20
# File 'app/components/avo/index/resource_table_component.rb', line 11

def initialize(resources: nil, resource: nil, reflection: nil, parent_record: nil, parent_resource: nil, pagy: nil, query: nil, actions: nil)
  @resources = resources
  @resource = resource
  @reflection = reflection
  @parent_record = parent_record
  @parent_resource = parent_resource
  @pagy = pagy
  @query = query
  @actions = actions
end

Instance Attribute Details

#pagyObject (readonly)

Returns the value of attribute pagy.



5
6
7
# File 'app/components/avo/index/resource_table_component.rb', line 5

def pagy
  @pagy
end

#queryObject (readonly)

Returns the value of attribute query.



5
6
7
# File 'app/components/avo/index/resource_table_component.rb', line 5

def query
  @query
end

Instance Method Details

#before_renderObject



7
8
9
# File 'app/components/avo/index/resource_table_component.rb', line 7

def before_render
  @header_fields, @table_row_components = cache_table_rows
end

#cache_table_rowsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/components/avo/index/resource_table_component.rb', line 47

def cache_table_rows
  # Cache the execution of the following block if caching is enabled in Avo configuration
  cache_if Avo.configuration.cache_resources_on_index_view, @resource.cache_hash(@parent_record), expires_in: 1.day do
    header_fields, table_row_components = generate_table_row_components

    # Create an array of header field labels used for each row to render values on the right column
    header_fields_ids = header_fields.map(&:table_header_label)

    # Assign header field IDs to each TableRowComponent
    # We assign it here because only complete header fields array after last table row.
    table_row_components.map { |table_row_component| table_row_component.header_fields = header_fields_ids }

    # Return header fields and table row components
    return [header_fields, table_row_components]
  end
end

#encrypted_queryObject



22
23
24
25
26
27
28
29
# File 'app/components/avo/index/resource_table_component.rb', line 22

def encrypted_query
  # TODO: move this to the resource where we can apply the adapter pattern
  if Module.const_defined?("Ransack::Search") && query.instance_of?(Ransack::Search)
    @query = @query.result
  end

  Avo::Services::EncryptionService.encrypt(message: @query, purpose: :select_all, serializer: Marshal)
end

#generate_table_row_componentsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/components/avo/index/resource_table_component.rb', line 64

def generate_table_row_components
  # Initialize arrays to hold header fields and table row components
  header_fields = []
  table_row_components = []

  # Loop through each resource in @resources
  @resources.each do |resource|
    # Get fields for the current resource and concat them to the @header_fields
    row_fields = resource.get_fields(reflection: @reflection, only_root: true)
    header_fields.concat row_fields

    # Create a TableRowComponent instance for the resource and add it to @table_row_components
    table_row_components << Avo::Index::TableRowComponent.new(
      resource: resource,
      fields: row_fields,
      reflection: @reflection,
      parent_record: @parent_record,
      parent_resource: @parent_resource,
      actions: @actions
    )
  end

  # Remove duplicate header fields based on table_header_label
  header_fields.uniq!(&:table_header_label)

  [header_fields, table_row_components]
end

#selected_all_labelObject



39
40
41
42
43
44
45
# File 'app/components/avo/index/resource_table_component.rb', line 39

def selected_all_label
  if @resource.pagination_type.countless?
    t "avo.records_selected_from_all_pages_html"
  else
    t "avo.x_records_selected_from_all_pages_html", count: pagy.count
  end
end

#selected_page_labelObject



31
32
33
34
35
36
37
# File 'app/components/avo/index/resource_table_component.rb', line 31

def selected_page_label
  if @resource.pagination_type.countless?
    t "avo.x_records_selected_from_page_html", selected: pagy.in
  else
    t "avo.x_records_selected_from_a_total_of_x_html", selected: pagy.in, count: pagy.count
  end
end