Class: CrudComponents::Action
- Inherits:
-
Object
- Object
- CrudComponents::Action
- 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
-
#http_method ⇒ Object
readonly
Returns the value of attribute http_method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#on ⇒ Object
readonly
Returns the value of attribute on.
-
#path_block ⇒ Object
readonly
Returns the value of attribute path_block.
Instance Method Summary collapse
- #collection? ⇒ Boolean
- #confirm_message ⇒ Object
- #css_class(config = CrudComponents.config) ⇒ Object
- #danger? ⇒ Boolean
- #derived? ⇒ Boolean
-
#icon(config = CrudComponents.config) ⇒ Object
Icon name (no library prefix — pair with css.icon_prefix).
-
#initialize(name, derived: false, **options, &path_block) ⇒ Action
constructor
A new instance of Action.
-
#permitted?(context, record_or_model) ⇒ Boolean
‘context` is the view (when CanCanCan’s ‘can?` is around) or anything can?-shaped.
- #row? ⇒ Boolean
- #selection? ⇒ Boolean
- #title ⇒ Object
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, **, &path_block) unknown = .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 = [:on] || defaults[:on] || :row @icon_option = [:icon] @icon_given = .key?(:icon) @title_option = [:title] @css_class = [:class] @confirm = .key?(:confirm) ? [:confirm] : defaults[:confirm] @http_method = [:method] || defaults[:method] || :get @condition = [:if] @path_block = path_block @danger = defaults[:danger] || false end |
Instance Attribute Details
#http_method ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/crud_components/action.rb', line 17 def name @name end |
#on ⇒ Object (readonly)
Returns the value of attribute on.
17 18 19 |
# File 'lib/crud_components/action.rb', line 17 def on @on end |
#path_block ⇒ Object (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
52 |
# File 'lib/crud_components/action.rb', line 52 def collection? = @on == :collection |
#confirm_message ⇒ Object
56 57 58 59 60 |
# File 'lib/crud_components/action.rb', line 56 def 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. : config.css.) end |
#danger? ⇒ Boolean
51 |
# File 'lib/crud_components/action.rb', line 51 def danger? = @danger |
#derived? ⇒ 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.
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
54 |
# File 'lib/crud_components/action.rb', line 54 def row? = @on == :row |
#selection? ⇒ Boolean
53 |
# File 'lib/crud_components/action.rb', line 53 def selection? = @on == :selection |
#title ⇒ Object
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 |