Sha256: 54682288cb488c92c721bf19004706702d63bc1df9dad4b65962b394fb328832

Contents?: true

Size: 1.37 KB

Versions: 11

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

class Avo::Fields::HasOneField::ShowComponent < Avo::Fields::ShowComponent
  include Avo::ApplicationHelper

  def can_attach?
    policy_result = true

    if @field.present?
      reflection_resource = @field.target_resource
      if reflection_resource.present? && @resource.present?
        method_name = "attach_#{@field.id}?".to_sym

        if @resource.authorization.has_method?(method_name, raise_exception: false)
          policy_result = @resource.authorization.authorize_action(method_name, raise_exception: false)
        end
      end
    end

    policy_result
  end

  def attach_path
    helpers.avo.resources_associations_new_path(@resource.singular_model_key, @resource.model.to_param, @field.id)
  end

  def can_see_the_create_button?
    create = "create_#{@field.id}?"

    authorization_service = @resource.authorization

    # By default return true if the create method is not defined for this field
    return true unless authorization_service.has_method?(create, raise_exception: false)

    authorization_service.authorize_action(create, raise_exception: false)
  end

  def create_path
    args = {
      via_relation: @resource.singular_model_key,
      via_relation_class: @resource.model_class.to_s,
      via_resource_id: @resource.model.to_param
    }
    helpers.new_resource_path(resource: @field.target_resource, **args)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
avo-2.29.0 app/components/avo/fields/has_one_field/show_component.rb
avo-2.28.3.pre.pr1646 app/components/avo/fields/has_one_field/show_component.rb
avo-2.28.2.pre.pr1642 app/components/avo/fields/has_one_field/show_component.rb
avo-2.28.1.pre.pr1642 app/components/avo/fields/has_one_field/show_component.rb
avo-2.28.0 app/components/avo/fields/has_one_field/show_component.rb
avo-2.27.2.pre.pr1606 app/components/avo/fields/has_one_field/show_component.rb
avo-2.27.1 app/components/avo/fields/has_one_field/show_component.rb
avo-2.27.0 app/components/avo/fields/has_one_field/show_component.rb
avo-2.26.3.pre.pr1601 app/components/avo/fields/has_one_field/show_component.rb
avo-2.26.2.pre.pr1579 app/components/avo/fields/has_one_field/show_component.rb
avo-2.25.1.pre.1.pr1579 app/components/avo/fields/has_one_field/show_component.rb