Class: Avo::ResourceComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/avo/resource_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

#has_with_trial

Instance Attribute Details

#fields_by_panelObject (readonly)

Returns the value of attribute fields_by_panel.



2
3
4
# File 'app/components/avo/resource_component.rb', line 2

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.



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

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.



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

def has_many_panels
  @has_many_panels
end

#has_one_panelsObject (readonly)

Returns the value of attribute has_one_panels.



3
4
5
# File 'app/components/avo/resource_component.rb', line 3

def has_one_panels
  @has_one_panels
end

#resource_toolsObject (readonly)

Returns the value of attribute resource_tools.



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

def resource_tools
  @resource_tools
end

#viewObject (readonly)

Returns the value of attribute view.



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

def view
  @view
end

Instance Method Details

#authorize_association_for(policy_method) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/components/avo/resource_component.rb', line 51

def authorize_association_for(policy_method)
  policy_result = true

  if @reflection.present?
    # Fetch the appropiate resource
    reflection_resource = ::Avo::App.get_resource_by_model_name(@reflection.active_record.name)
    # Fetch the model
    # Hydrate the resource with the model if we have one
    reflection_resource.hydrate(model: @parent_model) if @parent_model.present?
    # Use the related_name as the base of the association
    association_name = @reflection.name

    if association_name.present?
      method_name = "#{policy_method}_#{association_name}?".to_sym
      # Prepare the authorization service
      service = reflection_resource.authorization

      if service.has_method?(method_name, raise_exception: false)
        policy_result = service.authorize_action(method_name, raise_exception: false)
      end
    end
  end

  policy_result
end

#can_create?Boolean

Returns:

  • (Boolean)


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

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)


15
16
17
18
19
# File 'app/components/avo/resource_component.rb', line 15

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)


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

def can_detach?
  authorize_association_for("detach")
end

#can_see_the_actions_button?Boolean

Returns:

  • (Boolean)


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

def can_see_the_actions_button?
  return false if @actions.blank?

  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)


35
36
37
# File 'app/components/avo/resource_component.rb', line 35

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

#can_see_the_edit_button?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/components/avo/resource_component.rb', line 31

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

#destroy_pathObject



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

def destroy_path
  helpers.resource_path(model: @resource.model, resource: @resource)
end

#detach_pathObject



25
26
27
28
29
# File 'app/components/avo/resource_component.rb', line 25

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

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

#has_reflection_and_is_read_onlyObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/components/avo/resource_component.rb', line 83

def has_reflection_and_is_read_only
  if @reflection.present? && @reflection.active_record.name && @reflection.name
    fields = ::Avo::App.get_resource_by_model_name(@reflection.active_record.name).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 }.readonly
  else
    false
  end
end

#main_panelObject



77
78
79
80
81
# File 'app/components/avo/resource_component.rb', line 77

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