Sha256: c5f3e104a9d7543b546afc270bc2d0ede5d14556c1ddf1b8675151324b00628a

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

class Avo::ActionsComponent < ViewComponent::Base
  include Avo::ApplicationHelper
  attr_reader :label, :size, :as_row_control

  def initialize(actions: [], resource: nil, view: nil, exclude: [], style: :outline, color: :primary, label: nil, size: :md, as_row_control: false)
    @actions = actions || []
    @resource = resource
    @view = view
    @exclude = exclude
    @color = color
    @style = style
    @label = label || I18n.t("avo.actions")
    @size = size
    @as_row_control = as_row_control
  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 `record` param inside the action so we can prefill fields.
  def action_path(id)
    return single_record_path(id) if as_row_control
    return many_records_path(id) unless @resource.has_record_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 || as_row_control

    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

6 entries across 6 versions & 1 rubygems

Version Path
avo-3.0.0.pre8 app/components/avo/actions_component.rb
avo-3.0.0.pre9 app/components/avo/actions_component.rb
avo-3.0.0.pre7 app/components/avo/actions_component.rb
avo-3.0.0.pre5 app/components/avo/actions_component.rb
avo-3.0.0.pre6 app/components/avo/actions_component.rb
avo-3.0.0.pre4 app/components/avo/actions_component.rb