Class: Avo::Index::ResourceControlsComponent

Inherits:
ResourceComponent
  • Object
show all
Defined in:
app/components/avo/index/resource_controls_component.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ResourceControlsComponent.



4
5
6
7
8
9
10
# File 'app/components/avo/index/resource_controls_component.rb', line 4

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

Instance Method Details

#can_detach?Boolean

Returns:

  • (Boolean)


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

def can_detach?
  @reflection.present? &&
    @resource.model.present? &&
    is_has_many_association &&
    authorize_association_for(:detach)
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_reorder?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
# File 'app/components/avo/index/resource_controls_component.rb', line 34

def can_reorder?
  return authorize_association_for(:reorder) if @reflection.present?

  @resource.authorization.authorize_action(:reorder, 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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/components/avo/index/resource_controls_component.rb', line 53

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_model.present?
    args = {
      via_resource_class: parent_resource.class.to_s,
      via_resource_id: @parent_model.to_param
    }
  end

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

#is_has_many_associationObject



82
83
84
# File 'app/components/avo/index/resource_controls_component.rb', line 82

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

#parent_resourceObject



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

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

  ::Avo::App.get_resource_by_model_name @parent_model.class
end

#referrer_pathObject



86
87
88
# File 'app/components/avo/index/resource_controls_component.rb', line 86

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

#show_pathObject



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

def show_path
  args = {}

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

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

#singular_resource_nameObject



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

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