Sha256: 519540f41bd6c2eced36a11f44b62935b8e37a8baee267819b522b170b6871e5

Contents?: true

Size: 1.87 KB

Versions: 66

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

class Avo::BaseComponent < ViewComponent::Base
  include Turbo::FramesHelper

  def has_with_trial(ability)
    ::Avo::App.license.has_with_trial(ability)
  end

  private

  # Use the @parent_resource to fetch the field using the @reflection name.
  def field
    @parent_resource.get_field_definitions.find { |f| f.id == @reflection.name }
  rescue
    nil
  end

  # Fetch the resource and hydrate it with the model
  def association_resource
    resource = ::Avo::App.get_resource(params[:via_resource_class])
    model_class_name = params[:via_relation_class] || resource.model_class

    model_klass = ::Avo::BaseResource.valid_model_class model_class_name

    resource = ::Avo::App.get_resource_by_model_name model_klass if resource.blank?

    model = resource.find_record params[:via_resource_id], query: model_klass, params: params

    resource.dup.hydrate model: model
  end

  # Get the resource for the resource using the klass attribute so we get the namespace too
  def reflection_resource
    ::Avo::App.get_resource_by_model_name(@reflection.klass.to_s)
  rescue
    nil
  end

  # Get the resource for the resource using the klass attribute so we get the namespace too
  def reflection_parent_resource
    ::Avo::App.get_resource_by_model_name(@reflection.active_record.to_s)
  rescue
    nil
  end

  def parent_or_child_resource
    return @resource unless link_to_child_resource_is_enabled?
    return @resource if @resource.model.class.base_class == @resource.model.class

    ::Avo::App.get_resource_by_model_name(@resource.model.class).dup || @resource
  end

  def link_to_child_resource_is_enabled?
    return field_linked_to_child_resource? if @parent_resource

    @resource.link_to_child_resource
  end

  def field_linked_to_child_resource?
    field.present? && field.respond_to?(:link_to_child_resource) && field.link_to_child_resource
  end
end

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
avo-2.53.0 app/components/avo/base_component.rb
avo-2.52.0 app/components/avo/base_component.rb
avo-2.49.0 app/components/avo/base_component.rb
avo-2.48.0 app/components/avo/base_component.rb
avo-2.47.0 app/components/avo/base_component.rb
avo-2.46.0 app/components/avo/base_component.rb
avo-2.45.0 app/components/avo/base_component.rb
avo-2.44.0 app/components/avo/base_component.rb
avo-2.43.0 app/components/avo/base_component.rb
avo-2.42.2 app/components/avo/base_component.rb
avo-2.42.1 app/components/avo/base_component.rb
avo-2.42.0 app/components/avo/base_component.rb
avo-2.41.0 app/components/avo/base_component.rb
avo-2.40.0 app/components/avo/base_component.rb
avo-2.39.0 app/components/avo/base_component.rb
avo-2.38.0 app/components/avo/base_component.rb
avo-2.37.2 app/components/avo/base_component.rb
avo-2.37.1 app/components/avo/base_component.rb
avo-2.37.0 app/components/avo/base_component.rb
avo-2.36.3 app/components/avo/base_component.rb