Class: Avo::Index::ResourceControlsComponent

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

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 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 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 included from Concerns::ChecksAssocAuthorization

#authorize_association_for

Methods inherited from BaseComponent

#has_with_trial

Constructor Details

#initialize(resource: nil, reflection: nil, parent_record: nil, parent_resource: nil, view_type: :table, actions: nil) ⇒ ResourceControlsComponent

Returns a new instance of ResourceControlsComponent.



6
7
8
9
10
11
12
13
# File 'app/components/avo/index/resource_controls_component.rb', line 6

def initialize(resource: nil, reflection: nil, parent_record: nil, parent_resource: nil, view_type: :table, actions: nil)
  @resource = resource
  @reflection = reflection
  @parent_record = parent_record
  @parent_resource = parent_resource
  @view_type = view_type
  @actions = actions
end

Instance Method Details

#can_detach?Boolean

Returns:

  • (Boolean)


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

def can_detach?
  is_has_many_association? ? super : false
end

#can_edit?Boolean

Returns:

  • (Boolean)


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

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

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

#can_view?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'app/components/avo/index/resource_controls_component.rb', line 25

def can_view?
  return false if Avo.configuration.resource_default_view.edit?

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

  # Even if there's a @reflection object present, for show we're going to fallback to the original policy.
  @resource.authorization.authorize_action(:show, raise_exception: false)
end

#edit_pathObject



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

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)


76
77
78
# File 'app/components/avo/index/resource_controls_component.rb', line 76

def is_has_many_association?
  @reflection.is_a?(::ActiveRecord::Reflection::HasManyReflection) || @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)
end

#parent_resourceObject



69
70
71
72
73
74
# File 'app/components/avo/index/resource_controls_component.rb', line 69

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



80
81
82
# File 'app/components/avo/index/resource_controls_component.rb', line 80

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



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

def show_path
  args = {}

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

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

#singular_resource_nameObject



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

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