Sha256: 57a0a7a40d41e8a3aed2b2aa35958cbe5a3af39c38db7dd92d0c7fd08ae836e9

Contents?: true

Size: 1.37 KB

Versions: 20

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

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

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

  def render?
    actions.present?
  end

  def actions
    @actions.select do |action|
      !action.class.in?(@exclude)
    end
  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

20 entries across 20 versions & 1 rubygems

Version Path
avo-2.18.1 app/components/avo/actions_component.rb
avo-2.18.1.pre.1.eagerloaddirs app/components/avo/actions_component.rb
avo-2.18.0 app/components/avo/actions_component.rb
avo-2.17.1.pre.5.stackedlayout app/components/avo/actions_component.rb
avo-2.17.1.pre.4.issue.1342 app/components/avo/actions_component.rb
avo-2.17.1.pre.3 app/components/avo/actions_component.rb
avo-2.17.1.pre.2.customauthorizationclients app/components/avo/actions_component.rb
avo-2.17.1.pre.1.zeitwerk.eager.load.dir app/components/avo/actions_component.rb
avo-2.17.0 app/components/avo/actions_component.rb
avo-2.16.1.pre.1.nativefields app/components/avo/actions_component.rb
avo-2.16.0 app/components/avo/actions_component.rb
avo-2.15.3 app/components/avo/actions_component.rb
avo-2.15.3.pre.1.data.attrs.to.sidebar.items app/components/avo/actions_component.rb
avo-2.15.2 app/components/avo/actions_component.rb
avo-2.15.2.pre.1 app/components/avo/actions_component.rb
avo-2.15.1 app/components/avo/actions_component.rb
avo-2.15.0 app/components/avo/actions_component.rb
avo-2.14.3.pre.1.branding app/components/avo/actions_component.rb
avo-2.13.2.pre.2 app/components/avo/actions_component.rb
avo-2.13.2.pre.1 app/components/avo/actions_component.rb