Class: CrudComponents::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/crud_components/action.rb

Overview

A button, per row or per collection. Derived defaults (:new, :show, :edit, :destroy) are self-disabling: they render only when permitted and their conventional route resolves (see RouteResolver).

Constant Summary collapse

DERIVED =

Behavioral defaults for the derived actions. Icons live in config.action_icons (see #icon); titles come from i18n (see #title).

{
  new: { on: :collection },
  show: { on: :row },
  edit: { on: :row },
  destroy: { on: :row, method: :delete, confirm: true, danger: true }
}.freeze
KNOWN_OPTIONS =
%i[on icon title class confirm method if].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, derived: false, **options, &path_block) ⇒ Action

Returns a new instance of Action.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/crud_components/action.rb', line 19

def initialize(name, derived: false, **options, &path_block)
  unknown = options.keys - KNOWN_OPTIONS
  if unknown.any?
    raise DefinitionError, "action :#{name}: unknown option(s) #{unknown.map(&:inspect).join(', ')}" \
                           "known: #{KNOWN_OPTIONS.map(&:inspect).join(', ')}"
  end

  defaults = DERIVED[name.to_sym] || {}
  @name = name.to_sym
  @derived = derived
  @on = options[:on] || defaults[:on] || :row
  @icon_option = options[:icon]
  @icon_given = options.key?(:icon)
  @title_option = options[:title]
  @css_class = options[:class]
  @confirm = options.key?(:confirm) ? options[:confirm] : defaults[:confirm]
  @http_method = options[:method] || defaults[:method] || :get
  @condition = options[:if]
  @path_block = path_block
  @danger = defaults[:danger] || false
end

Instance Attribute Details

#http_methodObject (readonly)

Returns the value of attribute http_method.



17
18
19
# File 'lib/crud_components/action.rb', line 17

def http_method
  @http_method
end

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/crud_components/action.rb', line 17

def name
  @name
end

#onObject (readonly)

Returns the value of attribute on.



17
18
19
# File 'lib/crud_components/action.rb', line 17

def on
  @on
end

#path_blockObject (readonly)

Returns the value of attribute path_block.



17
18
19
# File 'lib/crud_components/action.rb', line 17

def path_block
  @path_block
end

Instance Method Details

#collection?Boolean

Returns:

  • (Boolean)


52
# File 'lib/crud_components/action.rb', line 52

def collection? = @on == :collection

#confirm_messageObject



56
57
58
59
60
# File 'lib/crud_components/action.rb', line 56

def confirm_message
  return nil unless @confirm

  @confirm == true ? I18n.t('crud_components.confirm', default: 'Are you sure?') : @confirm
end

#css_class(config = CrudComponents.config) ⇒ Object



66
67
68
# File 'lib/crud_components/action.rb', line 66

def css_class(config = CrudComponents.config)
  @css_class || (danger? ? config.css.button_danger : config.css.button)
end

#danger?Boolean

Returns:

  • (Boolean)


51
# File 'lib/crud_components/action.rb', line 51

def danger? = @danger

#derived?Boolean

Returns:

  • (Boolean)


50
# File 'lib/crud_components/action.rb', line 50

def derived? = @derived

#icon(config = CrudComponents.config) ⇒ Object

Icon name (no library prefix — pair with css.icon_prefix). An explicit ‘icon:` on the action wins (including `icon: nil` for none); otherwise it comes from config.action_icons, which is empty for non-derived actions.



44
45
46
47
48
# File 'lib/crud_components/action.rb', line 44

def icon(config = CrudComponents.config)
  return @icon_option if @icon_given

  config.action_icons[@name]
end

#permitted?(context, record_or_model) ⇒ Boolean

‘context` is the view (when CanCanCan’s ‘can?` is around) or anything can?-shaped. Without an explicit `if:` and without `can?`, the action is shown — permissions are opt-in, not a dependency.

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/crud_components/action.rb', line 73

def permitted?(context, record_or_model)
  if @condition
    model = record_or_model.is_a?(Class) ? record_or_model : record_or_model.class
    record = record_or_model.is_a?(Class) ? nil : record_or_model
    Permission.permitted?(@condition, model, context, record)
  elsif context.respond_to?(:can?)
    context.can?(name, record_or_model)
  else
    true
  end
end

#row?Boolean

Returns:

  • (Boolean)


54
# File 'lib/crud_components/action.rb', line 54

def row? = @on == :row

#selection?Boolean

Returns:

  • (Boolean)


53
# File 'lib/crud_components/action.rb', line 53

def selection? = @on == :selection

#titleObject



62
63
64
# File 'lib/crud_components/action.rb', line 62

def title
  @title_option || I18n.t("crud_components.actions.#{name}", default: name.to_s.humanize)
end