Class: Avo::Index::ResourceControlsComponent
- Inherits:
-
ResourceComponent
- Object
- ResourceComponent
- Avo::Index::ResourceControlsComponent
- Defined in:
- app/components/avo/index/resource_controls_component.rb
Instance Method Summary collapse
- #can_detach? ⇒ Boolean
- #can_edit? ⇒ Boolean
- #can_view? ⇒ Boolean
- #edit_path ⇒ Object
-
#initialize(resource: nil, reflection: nil, parent_model: nil, parent_resource: nil, view_type: :table) ⇒ ResourceControlsComponent
constructor
A new instance of ResourceControlsComponent.
- #is_has_many_association ⇒ Object
- #parent_resource ⇒ Object
- #referrer_path ⇒ Object
- #show_path ⇒ Object
- #singular_resource_name ⇒ Object
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
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 && (:detach) end |
#can_edit? ⇒ Boolean
19 20 21 22 23 |
# File 'app/components/avo/index/resource_controls_component.rb', line 19 def can_edit? return (:edit) if @reflection.present? @resource..(:edit, raise_exception: false) end |
#can_view? ⇒ 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 (:show) if @reflection.present? # Even if there's a @reflection object present, for show we're going to fallback to the original policy. @resource..(:show, raise_exception: false) end |
#edit_path ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/components/avo/index/resource_controls_component.rb', line 47 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_association ⇒ Object
76 77 78 |
# File 'app/components/avo/index/resource_controls_component.rb', line 76 def is_has_many_association @reflection.is_a?(::ActiveRecord::Reflection::HasManyReflection) || @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection) end |
#parent_resource ⇒ Object
69 70 71 72 73 74 |
# File 'app/components/avo/index/resource_controls_component.rb', line 69 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_path ⇒ Object
80 81 82 |
# File 'app/components/avo/index/resource_controls_component.rb', line 80 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_path ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/components/avo/index/resource_controls_component.rb', line 34 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_name ⇒ Object
61 62 63 64 65 66 67 |
# File 'app/components/avo/index/resource_controls_component.rb', line 61 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 |