Class: Avo::Index::ResourceControlsComponent

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

Direct Known Subclasses

ResourceControlsDropdownComponent

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, #body_classes, #button_classes, #chart_color, #container_classes, #d, #decode_filter_params, #e, #editor_file_path, #editor_url, #empty_state, #encode_filter_params, #frame_id, #get_model_class, #input_classes, #mount_path, #number_to_social, #possibly_rails_authentication?, #render_license_warning, #root_path_without_url, #rtl?, #text_direction, #ui, #wrap_in_modal

Methods included from ResourcesHelper

#field_wrapper, #filter_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

#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
23
24
25
# File 'app/components/avo/index/resource_controls_component.rb', line 18

def can_edit?
  # Disable edit for ArrayResources
  return false if @resource.resource_type_array?

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

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

#edit_pathObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/components/avo/index/resource_controls_component.rb', line 31

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)


60
61
62
63
64
65
66
# File 'app/components/avo/index/resource_controls_component.rb', line 60

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

#parent_resourceObject



53
54
55
56
57
58
# File 'app/components/avo/index/resource_controls_component.rb', line 53

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



68
69
70
# File 'app/components/avo/index/resource_controls_component.rb', line 68

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



27
28
29
# File 'app/components/avo/index/resource_controls_component.rb', line 27

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



45
46
47
48
49
50
51
# File 'app/components/avo/index/resource_controls_component.rb', line 45

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