Sha256: 08a07a4f6f84f44fa3613d00927eed65035022038d81ed3c51c4a024ce7bb57c

Contents?: true

Size: 1.42 KB

Versions: 39

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

class Avo::ActionsComponent < ViewComponent::Base
  include Avo::ApplicationHelper
  attr_reader :label

  def initialize(actions: [], resource: nil, view: nil, exclude: [], style: :outline, color: :primary, label: nil)
    @actions = actions || []
    @resource = resource
    @view = view
    @exclude = exclude
    @color = color
    @style = style
    @label = label || t("avo.actions")
  end

  def render?
    actions.present?
  end

  def actions
    @actions.reject { |action| action.class.in?(@exclude) }
  end

  # When running an action for one record we should do it on a special path.
  # We do that so we get the `model` param inside the action so we can prefill fields.
  def action_path(id)
    return many_records_path(id) unless @resource.has_model_id?

    if on_record_page?
      single_record_path id
    else
      many_records_path id
    end
  end

  # How should the action be displayed by default
  def is_disabled?(action)
    return false if action.standalone

    on_index_page?
  end

  private

  def on_record_page?
    @view.in?([:show, :edit, :new])
  end

  def on_index_page?
    !on_record_page?
  end

  def single_record_path(id)
    Avo::Services::URIService.parse(@resource.record_path)
      .append_paths("actions", id)
      .to_s
  end

  def many_records_path(id)
    Avo::Services::URIService.parse(@resource.records_path)
      .append_paths("actions", id)
      .to_s
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
avo-2.30.2 app/components/avo/actions_component.rb
avo-2.30.1 app/components/avo/actions_component.rb
avo-2.30.1.pre4.pr1683 app/components/avo/actions_component.rb
avo-2.30.1.pre3.pr1683 app/components/avo/actions_component.rb
avo-2.30.1.pre2.pr1683 app/components/avo/actions_component.rb
avo-2.30.1.pre1.pr1683 app/components/avo/actions_component.rb
avo-2.30.0 app/components/avo/actions_component.rb
avo-2.29.1 app/components/avo/actions_component.rb
avo-2.29.1.pre.pr1652 app/components/avo/actions_component.rb
avo-2.29.0 app/components/avo/actions_component.rb
avo-2.28.3.pre.pr1646 app/components/avo/actions_component.rb
avo-2.28.2.pre.pr1642 app/components/avo/actions_component.rb
avo-2.28.1.pre.pr1642 app/components/avo/actions_component.rb
avo-2.28.0 app/components/avo/actions_component.rb
avo-2.27.2.pre.pr1606 app/components/avo/actions_component.rb
avo-2.27.1 app/components/avo/actions_component.rb
avo-2.27.0 app/components/avo/actions_component.rb
avo-2.26.3.pre.pr1601 app/components/avo/actions_component.rb
avo-2.26.2.pre.pr1579 app/components/avo/actions_component.rb
avo-2.26.1.pr1584.pre.1 app/components/avo/actions_component.rb