Sha256: 04acfc02ddd1e0344d0f66f18b46abc12517233f8d5eaa9490d955f8e39e8813

Contents?: true

Size: 1.17 KB

Versions: 18

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

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

  def initialize(actions: [], resource: nil, view: nil)
    @actions = actions
    @resource = resource
    @view = view
  end

  def render?
    @actions.present?
  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

18 entries across 18 versions & 1 rubygems

Version Path
avo-2.12.1.pre.1 app/components/avo/actions_component.rb
avo-2.12.0 app/components/avo/actions_component.rb
avo-2.11.3.pre.3 app/components/avo/actions_component.rb
avo-2.11.3.pre.2 app/components/avo/actions_component.rb
avo-2.11.2.pre.3 app/components/avo/actions_component.rb
avo-2.11.2.pre.2 app/components/avo/actions_component.rb
avo-2.11.2.pre.1 app/components/avo/actions_component.rb
avo-2.11.1.pre.3 app/components/avo/actions_component.rb
avo-2.11.1 app/components/avo/actions_component.rb
avo-2.11.1.pre.2 app/components/avo/actions_component.rb
avo-2.11.1.pre.1 app/components/avo/actions_component.rb
avo-2.11.0 app/components/avo/actions_component.rb
avo-2.10.3.pre.1 app/components/avo/actions_component.rb
avo-2.10.2 app/components/avo/actions_component.rb
avo-2.10.0 app/components/avo/actions_component.rb
avo-2.9.1.pre7 app/components/avo/actions_component.rb
avo-2.9.1.pre6 app/components/avo/actions_component.rb
avo-2.9.1.pre4 app/components/avo/actions_component.rb