Class: Avo::Views::ResourceIndexComponent

Inherits:
ResourceComponent
  • Object
show all
Includes:
ApplicationHelper, ResourcesHelper
Defined in:
app/components/avo/views/resource_index_component.rb

Instance Method Summary collapse

Methods included from ApplicationHelper

#a_button, #a_link, #appearance_neutral_labels, #avo_appearance_t, #body_classes, #button_classes, #chart_color, #container_classes, #custom_sidebar_width_default, #d, #decode_filter_params, #e, #editor_file_path, #editor_url, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #manual_frame_cookie_name, #manual_frame_remembered?, #mount_path, #number_to_social, #possibly_rails_authentication?, #render_header_menu_items, #render_license_warning, #root_path_without_url, #rtl?, #safe_blob_path, #safe_blob_representation_url, #safe_blob_url, #sidebar_width_default, #sidebar_width_max, #sidebar_width_min, #text_direction, #ui, #wrap_in_modal

Methods included from SummaryChartHelper

#summary_chart_params_for

Methods included from ResourcesHelper

#field_wrapper, #index_field_wrapper, #item_selector_data_attributes, #record_path, #record_title, #resource_for_record, #resource_grid, #resource_show_path, #resource_table

Instance Method Details

#attach_pathObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/components/avo/views/resource_index_component.rb', line 87

def attach_path
  current_path = CGI.unescape(request.env["PATH_INFO"]).split("/").select(&:present?)

  Avo.root_path(
    paths: [*current_path, "new"],
    query: {
      view: @parent_resource&.view&.to_s,
      turbo_frame: params[:turbo_frame],
      for_attribute: field&.try(:for_attribute)
    }.compact
  )
end

#authorized_to_search?Boolean

Returns:

  • (Boolean)


128
129
130
131
# File 'app/components/avo/views/resource_index_component.rb', line 128

def authorized_to_search?
  # Hide the search if the authorization prevents it
  @resource.authorization.authorize_action("search", raise_exception: false)
end

#back_pathObject



164
165
166
167
168
169
170
171
172
173
# File 'app/components/avo/views/resource_index_component.rb', line 164

def back_path
  # The `return_to` param takes precedence over anything else.
  return params[:return_to] if params[:return_to].present?

  # Show Go Back link only when association page is opened
  # as a standalone page
  if @reflection.present? && !helpers.turbo_frame_request?
    helpers.resource_path(record: @parent_record, resource: @parent_resource)
  end
end

#can_attach?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/components/avo/views/resource_index_component.rb', line 43

def can_attach?
  reflection_class = if @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)
    @reflection.through_reflection.class
  else
    @reflection.class
  end

  return false unless reflection_class.in? [
    ActiveRecord::Reflection::HasManyReflection,
    ActiveRecord::Reflection::HasAndBelongsToManyReflection
  ]

  authorize_association_for(:attach)
end

#can_render_scopes?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'app/components/avo/views/resource_index_component.rb', line 160

def can_render_scopes?
  Avo.plugin_manager.installed?("avo-scopes") && @scopes.present?
end

#can_see_the_create_button?Boolean

The Create button is dependent on the new? policy method. The create? should be called only when the user clicks the Save button so the developers gets access to the params from the form.

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'app/components/avo/views/resource_index_component.rb', line 34

def can_see_the_create_button?
  # Disable creation for ArrayResources
  return false if @resource.resource_type_array?

  return authorize_association_for(:create) if @reflection.present?

  @resource.authorization.authorize_action(:new, raise_exception: false)
end

#create_pathObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/components/avo/views/resource_index_component.rb', line 58

def create_path
  args = {}

  if @reflection.present?
    args = {
      via_resource_class: @parent_resource.class,
      via_relation_class: reflection_model_class,
      via_record_id: @parent_record.to_param
    }

    if @reflection.class.in? [
      ActiveRecord::Reflection::ThroughReflection,
      ActiveRecord::Reflection::HasAndBelongsToManyReflection
    ]
      args[:via_relation] = params[:resource_name]
    end

    if @reflection.is_a? ActiveRecord::Reflection::HasManyReflection
      args[:via_relation] = @reflection.name
    end

    if @reflection.inverse_of.present?
      args[:via_relation] = @reflection.inverse_of.name
    end
  end

  helpers.new_resource_path(resource: @resource, **args)
end

#descriptionObject



113
114
115
116
117
118
# File 'app/components/avo/views/resource_index_component.rb', line 113

def description
  # If this is a has many association, the user can pass a description to be shown just for this association.
  return field&.description(query: @query) if @reflection.present?

  @resource.description
end

#render_dynamic_filters_buttonObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/components/avo/views/resource_index_component.rb', line 133

def render_dynamic_filters_button
  return unless show_dynamic_filters_button?

  a_button size: :md,
    color: :primary,
    icon: "tabler/outline/filter",
    data: {
      controller: "avo-filters",
      action: "click->avo-filters#toggleFiltersArea",
      avo_filters_dynamic_filters_component_id_value: dynamic_filters_component_id
    } do
    Avo::DynamicFilters.configuration.button_label
  end
end

#scopes_listObject



148
149
150
151
152
153
154
155
156
157
158
# File 'app/components/avo/views/resource_index_component.rb', line 148

def scopes_list
  Avo::Scopes::ListComponent.new(
    scopes: @scopes,
    resource: @resource,
    turbo_frame: @turbo_frame,
    parent_record: @parent_record,
    parent_resource: @parent_resource,
    query: @query,
    loader: @resource.entity_loader(:scope)
  )
end

#show_search_inputObject



120
121
122
123
124
125
126
# File 'app/components/avo/views/resource_index_component.rb', line 120

def show_search_input
  return false unless authorized_to_search?
  return false unless @resource.class.search_query.present?
  return false if field&.hide_search_input

  true
end

#singular_resource_nameObject

Feeds the "%item" interpolation in the index create/attach buttons, so it returns the sentence form: a translated name stays verbatim, a generated one is lowercased. The buttons upcase_first the sentence.



103
104
105
106
107
108
109
110
111
# File 'app/components/avo/views/resource_index_component.rb', line 103

def singular_resource_name
  if @reflection.present?
    return field.sentence_name.singularize if field.present?

    reflection_resource.sentence_name
  else
    @resource.sentence_name.presence || @resource.model_class.model_name.name.downcase
  end
end

#titleObject



22
23
24
25
26
27
28
29
30
# File 'app/components/avo/views/resource_index_component.rb', line 22

def title
  if @reflection.present?
    return name if field.present?

    reflection_resource.plural_name
  else
    @resource.plural_name
  end
end