Class: Jav::ResourceComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/jav/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/jav/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.



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

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.



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

def has_many_panels
  @has_many_panels
end

#has_one_panelsObject (readonly)

Returns the value of attribute has_one_panels.



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

def has_one_panels
  @has_one_panels
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

#resource_toolsObject (readonly)

Returns the value of attribute resource_tools.



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

def resource_tools
  @resource_tools
end

#viewObject (readonly)

Returns the value of attribute view.



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

def view
  @view
end

Instance Method Details

#authorize_association_for(policy_method) ⇒ Object

Ex: A Post has many Comments



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/components/jav/resource_component.rb', line 56

def authorize_association_for(policy_method)
  policy_result = true

  if @reflection.present?
    # Fetch the appropiate resource
    reflection_resource = field.resource
    # 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}?"

      # Use the policy methods from the parent (Post)
      service = reflection_resource.authorization

      if service.has_method?(method_name, raise_exception: false)
        # Some policy methods should get the parent record in order to have the necessarry information to do the authorization
        # Example: Post->has_many->Comments
        # When you want to authorize the creation/attaching of a Comment, you don't have the Comment instance.
        # But you do have the Post instance and you can get that in your policy to authorize against.
        parent_policy_methods = %i[view create attach act_on]

        record = if parent_policy_methods.include?(policy_method)
                   # Use the parent record (Post)
                   reflection_resource.model
                 else
                   # Override the record with the child record (Comment)
                   resource.model
                 end
        policy_result = service.authorize_action(method_name, record: record, raise_exception: false)
      end
    end
  end

  policy_result
end

#can_act_on?Boolean

Returns:

  • (Boolean)


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

def can_act_on?
  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_create?Boolean

Returns:

  • (Boolean)


4
5
6
7
8
# File 'app/components/jav/resource_component.rb', line 4

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)


10
11
12
13
14
# File 'app/components/jav/resource_component.rb', line 10

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)


16
17
18
19
20
21
22
23
24
25
# File 'app/components/jav/resource_component.rb', line 16

def can_detach?
  return false if @reflection.blank? || @resource.model.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_destroy_button?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/components/jav/resource_component.rb', line 37

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

#can_see_the_edit_button?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/components/jav/resource_component.rb', line 33

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

#destroy_pathObject



47
48
49
50
51
52
53
# File 'app/components/jav/resource_component.rb', line 47

def destroy_path
  if params[:via_resource_class].present?
    helpers.resource_path(model: @resource.model, resource: @resource, referrer: back_path)
  else
    helpers.resource_path(model: @resource.model, resource: @resource)
  end
end

#detach_pathObject



27
28
29
30
31
# File 'app/components/jav/resource_component.rb', line 27

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

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

#has_reflection_and_is_read_onlyObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/components/jav/resource_component.rb', line 110

def has_reflection_and_is_read_only
  return false unless @reflection.present? && @reflection.active_record.name && @reflection.name

  fields = ::Jav::App.get_resource_by_model_name(@reflection.active_record.name).get_field_definitions
  filtered_fields = fields.filter { |f| f.id == @reflection.name }

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

#main_panelObject



96
97
98
99
100
# File 'app/components/jav/resource_component.rb', line 96

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


102
103
104
# File 'app/components/jav/resource_component.rb', line 102

def sidebar
  @sidebar ||= search_for_sidebar
end


106
107
108
# File 'app/components/jav/resource_component.rb', line 106

def sidebar_component(form: nil)
  Jav::ResourceSidebarComponent.new resource: @resource, fields: sidebar.visible_items, params: params, view: view, form: form
end