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) ⇒ ResourceControlsComponent

Returns a new instance of ResourceControlsComponent.



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

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

Instance Method Details

#can_detach?Boolean

Returns:

  • (Boolean)


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

def can_detach?
  @reflection.present? &&
    @resource.model.present? &&
    (@reflection.is_a?(::ActiveRecord::Reflection::HasManyReflection) || @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)) &&
    authorize_association_for("detach")
end

#can_edit?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/components/avo/index/resource_controls_component.rb', line 17

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

#can_view?Boolean

Returns:

  • (Boolean)


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

def can_view?
  @resource.authorization.authorize_action(:show, raise_exception: false)
end

#edit_pathObject



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

def edit_path
  args = {}

  if @parent_model.present?
    args = {
      via_resource_class: parent_resource.model_class,
      via_resource_id: @parent_model.id
    }
  end

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

#parent_resourceObject



59
60
61
62
63
# File 'app/components/avo/index/resource_controls_component.rb', line 59

def parent_resource
  return nil if @parent_model.blank?

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

#show_pathObject



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

def show_path
  args = {}

  if @parent_model.present?
    args = {
      via_resource_class: parent_resource.model_class,
      via_resource_id: @parent_model.id
    }
  end

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

#singular_resource_nameObject



51
52
53
54
55
56
57
# File 'app/components/avo/index/resource_controls_component.rb', line 51

def singular_resource_name
  if @reflection.present?
    ::Avo::App.get_resource_by_model_name(@reflection.class_name).name
  else
    @resource.singular_name.present? ? @resource.singular_name : @resource.model_class.model_name.name.downcase
  end
end