Sha256: d8540461818bcf1b7317b760bc7ab9fccda84e88ee5f3d6757bb4991d37b75d4

Contents?: true

Size: 1.86 KB

Versions: 33

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

class Avo::Index::ResourceControlsComponent < Avo::ResourceComponent
  def initialize(resource: nil, reflection: nil, parent_model: nil, view_type: :table)
    @resource = resource
    @reflection = reflection
    @parent_model = parent_model
    @view_type = view_type
  end

  def can_detach?
    @reflection.present? &&
      @resource.model.present? &&
      is_has_many_association &&
      authorize_association_for("detach")
  end

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

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

  def can_view?
    return authorize_association_for(:view) if @reflection.present?

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

  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

  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

  def singular_resource_name
    if @reflection.present?
      reflection_resource.name
    else
      @resource.singular_name.present? ? @resource.singular_name : @resource.model_class.model_name.name.downcase
    end
  end

  def parent_resource
    return nil if @parent_model.blank?

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

  def is_has_many_association
    @reflection.is_a?(::ActiveRecord::Reflection::HasManyReflection) || @reflection.is_a?(::ActiveRecord::Reflection::ThroughReflection)
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
avo-1.25.2 app/components/avo/index/resource_controls_component.rb
avo-2.6.1.pre.2 app/components/avo/index/resource_controls_component.rb
avo-2.6.1.pre.1 app/components/avo/index/resource_controls_component.rb
avo-2.6.0 app/components/avo/index/resource_controls_component.rb
avo-2.5.2.pre.7 app/components/avo/index/resource_controls_component.rb
avo-2.5.2.pre.6 app/components/avo/index/resource_controls_component.rb
avo-2.5.2.pre.5 app/components/avo/index/resource_controls_component.rb
avo-2.5.2.pre.4 app/components/avo/index/resource_controls_component.rb
avo-2.5.2.pre.3 app/components/avo/index/resource_controls_component.rb
avo-2.5.2.pre.2 app/components/avo/index/resource_controls_component.rb
avo-2.5.2.pre.1 app/components/avo/index/resource_controls_component.rb
avo-2.5.1 app/components/avo/index/resource_controls_component.rb
avo-2.5.0 app/components/avo/index/resource_controls_component.rb
avo-2.4.1 app/components/avo/index/resource_controls_component.rb
avo-2.4.0 app/components/avo/index/resource_controls_component.rb
avo-2.3.1.pre.6 app/components/avo/index/resource_controls_component.rb
avo-2.3.1.pre.5 app/components/avo/index/resource_controls_component.rb
avo-2.3.1.pre.4 app/components/avo/index/resource_controls_component.rb
avo-2.3.1.pre.3 app/components/avo/index/resource_controls_component.rb
avo-2.3.1.pre.2 app/components/avo/index/resource_controls_component.rb