Class: Avo::ResourceComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
Concerns::ChecksAssocAuthorization, Concerns::RequestMethods
Defined in:
app/components/avo/resource_component.rb

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::RequestMethods

#referrer_params

Methods included from Concerns::ChecksAssocAuthorization

#authorize_association_for

Methods inherited from BaseComponent

#has_with_trial

Methods included from Concerns::FindAssociationField

#find_association_field

Instance Attribute Details

#fields_by_panelObject (readonly)

Returns the value of attribute fields_by_panel.



5
6
7
# File 'app/components/avo/resource_component.rb', line 5

def fields_by_panel
  @fields_by_panel
end

#has_as_belongs_to_many_panelsObject (readonly)

Returns the value of attribute has_as_belongs_to_many_panels.



8
9
10
# File 'app/components/avo/resource_component.rb', line 8

def has_as_belongs_to_many_panels
  @has_as_belongs_to_many_panels
end

#has_many_panelsObject (readonly)

Returns the value of attribute has_many_panels.



7
8
9
# File 'app/components/avo/resource_component.rb', line 7

def has_many_panels
  @has_many_panels
end

#has_one_panelsObject (readonly)

Returns the value of attribute has_one_panels.



6
7
8
# File 'app/components/avo/resource_component.rb', line 6

def has_one_panels
  @has_one_panels
end

#resourceObject (readonly)

Returns the value of attribute resource.



10
11
12
# File 'app/components/avo/resource_component.rb', line 10

def resource
  @resource
end

#resource_toolsObject (readonly)

Returns the value of attribute resource_tools.



9
10
11
# File 'app/components/avo/resource_component.rb', line 9

def resource_tools
  @resource_tools
end

#viewObject (readonly)

Returns the value of attribute view.



11
12
13
# File 'app/components/avo/resource_component.rb', line 11

def view
  @view
end

Instance Method Details

#can_create?Boolean

Returns:

  • (Boolean)


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

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

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

#can_delete?Boolean

Returns:

  • (Boolean)


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

def can_delete?
  return authorize_association_for(:destroy) if @reflection.present?

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

#can_detach?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
# File 'app/components/avo/resource_component.rb', line 25

def can_detach?
  return false if @reflection.blank? || @resource.record.blank? || !authorize_association_for(:detach)

  # If the inverse_of is a belongs_to, we need to check if it's optional in order to know if we can detach it.
  if inverse_of.is_a?(ActiveRecord::Reflection::BelongsToReflection)
    inverse_of.options[:optional]
  else
    true
  end
end

#can_see_the_actions_button?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'app/components/avo/resource_component.rb', line 52

def can_see_the_actions_button?
  return authorize_association_for(:act_on) if @reflection.present?

  @resource.authorization.authorize_action(:act_on, raise_exception: false) && !has_reflection_and_is_read_only
end

#can_see_the_destroy_button?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/components/avo/resource_component.rb', line 48

def can_see_the_destroy_button?
  @resource.authorization.authorize_action(:destroy, raise_exception: false)
end

#can_see_the_edit_button?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'app/components/avo/resource_component.rb', line 42

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

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

#destroy_pathObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/components/avo/resource_component.rb', line 58

def destroy_path
  args = {record: @resource.record, resource: @resource}

  args[:referrer] = if params[:via_resource_class].present?
    back_path
  # If we're deleting a resource from a parent resource, we need to go back to the parent resource page after the deletion
  elsif @parent_resource.present?
    helpers.resource_path(record: @parent_record, resource: @parent_resource)
  end

  helpers.resource_path(**args)
end

#detach_pathObject



36
37
38
39
40
# File 'app/components/avo/resource_component.rb', line 36

def detach_path
  return "/" if @reflection.blank?

  helpers.resource_detach_path(params[:resource_name], params[:id], @reflection.name.to_s, @resource.record_param)
end

#has_reflection_and_is_read_onlyObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/components/avo/resource_component.rb', line 89

def has_reflection_and_is_read_only
  if @reflection.present? && @reflection.active_record.name && @reflection.name
    resource = Avo.resource_manager.get_resource_by_model_class(@reflection.active_record.name).new(params: helpers.params, view: view, user: helpers._current_user)
    fields = resource.get_field_definitions
    filtered_fields = fields.filter { |f| f.id == @reflection.name }
  else
    return false
  end

  if filtered_fields.present?
    filtered_fields.find { |f| f.id == @reflection.name }.is_disabled?
  else
    false
  end
end

#main_panelObject



71
72
73
74
75
# File 'app/components/avo/resource_component.rb', line 71

def main_panel
  @main_panel ||= @resource.get_items.find do |item|
    item.is_main_panel?
  end
end

#render_cards_componentObject



109
110
111
112
113
# File 'app/components/avo/resource_component.rb', line 109

def render_cards_component
  if Avo.plugin_manager.installed?(:avo_dashboards)
    render Avo::CardsComponent.new cards: @resource.detect_cards.visible_cards, classes: "pb-4 sm:grid-cols-3"
  end
end

#render_control(control) ⇒ Object



105
106
107
# File 'app/components/avo/resource_component.rb', line 105

def render_control(control)
  send :"render_#{control.type}", control
end


77
78
79
80
81
82
83
84
85
86
87
# File 'app/components/avo/resource_component.rb', line 77

def sidebars
  return [] if Avo.license.lacks_with_trial(:resource_sidebar)

  @sidebars ||= @item.items
    .select do |item|
      item.is_sidebar?
    end
    .map do |sidebar|
      sidebar.hydrate(view: view, resource: resource)
    end
end