Class: CrudComponents::Presenters::Actions

Inherits:
Base
  • Object
show all
Defined in:
lib/crud_components/presenters/actions.rb

Overview

Resolves a list of actions for a subject (record or model class) into renderable items. Derived actions are self-disabling: no permission or no resolvable route → no button, never a broken link.

Defined Under Namespace

Classes: Item

Constant Summary

Constants inherited from Base

Base::GEM_VIEW_ROOT

Instance Attribute Summary

Attributes inherited from Base

#view

Instance Method Summary collapse

Methods inherited from Base

#ability, #config, #css, #permission_context, #render_cell, #render_filter_control

Constructor Details

#initialize(view:, subject:, structure: nil, actions: nil, owner: nil, suppress_show: false) ⇒ Actions

Returns a new instance of Actions.



9
10
11
12
13
14
15
16
17
# File 'lib/crud_components/presenters/actions.rb', line 9

def initialize(view:, subject:, structure: nil, actions: nil, owner: nil, suppress_show: false)
  super(view: view)
  @subject = subject
  @model = subject.is_a?(Class) ? subject : subject.class
  @structure = structure || Structure.for(@model)
  @list = actions
  @owner = owner
  @suppress_show = suppress_show
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


35
# File 'lib/crud_components/presenters/actions.rb', line 35

def any? = items.any?

#itemsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/crud_components/presenters/actions.rb', line 21

def items
  @items ||= list.filter_map do |action|
    next if action.name == :show && action.derived? && @suppress_show
    next unless action.permitted?(permission_context, @subject)

    path = RouteResolver.action_path(view, action,
                                     record: kind == :row ? @subject : nil,
                                     model: @model, owner: @owner)
    next unless path

    Item.new(action: action, path: path)
  end
end

#joinable?Boolean

Bootstrap btn-group only renders cleanly when every child is a direct ‘.btn`. A non-GET action is a button_to form, which breaks the group’s edge radii — so we only join when all actions are GET links.

Returns:

  • (Boolean)


40
41
42
# File 'lib/crud_components/presenters/actions.rb', line 40

def joinable?
  items.all? { |item| item.action.http_method == :get }
end

#kindObject



19
# File 'lib/crud_components/presenters/actions.rb', line 19

def kind = @subject.is_a?(Class) ? :collection : :row