Class: Avo::Index::ResourceControlsComponent

Inherits:
ResourceComponent show all
Includes:
ApplicationHelper, Concerns::ChecksShowAuthorization
Defined in:
app/components/avo/index/resource_controls_component.rb

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Attribute Summary

Attributes inherited from ResourceComponent

#fields_by_panel, #has_as_belongs_to_many_panels, #has_many_panels, #has_one_panels, #resource, #resource_tools, #view

Instance Method Summary collapse

Methods included from Concerns::ChecksShowAuthorization

#can_view?

Methods included from Concerns::ChecksAssocAuthorization

#authorize_association_for

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_selector_data_attributes, #resource_grid, #resource_show_path, #resource_table

Methods inherited from ResourceComponent

#can_create?, #can_delete?, #can_see_the_actions_button?, #can_see_the_destroy_button?, #can_see_the_edit_button?, #destroy_path, #detach_path, #has_reflection_and_is_read_only, #main_panel, #render_cards_component, #render_control, #sidebars

Methods included from Concerns::RequestMethods

#referrer_params

Methods inherited from BaseComponent

#has_with_trial

Methods included from Concerns::FindAssociationField

#find_association_field

Instance Method Details

#can_detach?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'app/components/avo/index/resource_controls_component.rb', line 14

def can_detach?
  is_has_many_association? ? super : false
end

#can_edit?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'app/components/avo/index/resource_controls_component.rb', line 18

def can_edit?
  return authorize_association_for(:edit) if @reflection.present?

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

#edit_pathObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/components/avo/index/resource_controls_component.rb', line 28

def edit_path
  # Add the `view` param to let Avo know where to redirect back when the user clicks the `Cancel` button.
  args = {via_view: "index"}

  if @parent_record.present?
    args = {
      via_resource_class: parent_resource.class.to_s,
      via_record_id: @parent_record.to_param
    }
  end

  helpers.edit_resource_path(record: @resource.record, resource: parent_or_child_resource, **args)
end

#is_has_many_association?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'app/components/avo/index/resource_controls_component.rb', line 57

def is_has_many_association?
  @reflection.class.in? [
    ActiveRecord::Reflection::HasManyReflection,
    ActiveRecord::Reflection::HasAndBelongsToManyReflection,
    ActiveRecord::Reflection::ThroughReflection
  ]
end

#parent_resourceObject



50
51
52
53
54
55
# File 'app/components/avo/index/resource_controls_component.rb', line 50

def parent_resource
  return @parent_resource if @parent_resource.present?
  return nil if @parent_record.blank?

  Avo.resource_manager.get_resource_by_model_class @parent_record.class
end

#referrer_pathObject



65
66
67
# File 'app/components/avo/index/resource_controls_component.rb', line 65

def referrer_path
  Avo.root_path(paths: ["resources", params[:resource_name], params[:id], params[:related_name]], query: request.query_parameters.to_h)
end

#show_pathObject



24
25
26
# File 'app/components/avo/index/resource_controls_component.rb', line 24

def show_path
  helpers.resource_show_path(resource: @resource, parent_or_child_resource: parent_or_child_resource, parent_resource: parent_resource, parent_record: @parent_record)
end

#singular_resource_nameObject



42
43
44
45
46
47
48
# File 'app/components/avo/index/resource_controls_component.rb', line 42

def singular_resource_name
  if @reflection.present?
    field&.name&.singularize || reflection_resource.name
  else
    @resource.singular_name.present? ? @resource.singular_name : @resource.model_class.model_name.name.downcase
  end
end