Class: AppKit::Action
- Inherits:
-
Object
- Object
- AppKit::Action
- Defined in:
- lib/app_kit/action.rb
Overview
The action class is used to generate custom actions that can be performed on a given record. These actions are displayed when viewing a specific record. Actions are defiend in the main Resource DSL.
DSL Examples
Inline example
action :deactivate, :if => :active
Block example
action :deactivate, :if => :active do |record|
record.update(active: false)
end
Instance Attribute Summary collapse
-
#block ⇒ Object
The block to be executed when the aciton is performed.
-
#if_method ⇒ Object
A method to call on the model to determin if this action should be displayed.
-
#name ⇒ Object
The name of the aciton.
-
#resource ⇒ Object
The resoruce this action belongs to.
Instance Method Summary collapse
-
#action_type ⇒ Symbol
Determines if the action should call to a method or execute a block.
-
#display_name ⇒ String
The display name to use when displaying a link to the action.
-
#enabled_for_record?(record) ⇒ Boolean
Determines if the action should be displayed.
-
#initialize(name, resource, options = {}, &block) ⇒ Action
constructor
This class should be created from the Resource DSL and should not be created manually.
-
#is_block_action? ⇒ Boolean
Convienence method that detmines if the action should call a block.
-
#is_method_action? ⇒ Boolean
Convienence method that detmines if the action should call a model method.
-
#method_name ⇒ Symbol
The method name to call on the model.
Constructor Details
#initialize(name, resource, options = {}, &block) ⇒ Action
This class should be created from the Resource DSL and should not be created manually.
45 46 47 48 49 50 |
# File 'lib/app_kit/action.rb', line 45 def initialize(name, resource, = {}, &block) @name = name @block = block if block_given? @resource = resource @if_method = [:if] end |
Instance Attribute Details
#block ⇒ Object
The block to be executed when the aciton is performed.
action :deactive do |record|
record.update(active: false)
end
31 32 33 |
# File 'lib/app_kit/action.rb', line 31 def block @block end |
#if_method ⇒ Object
A method to call on the model to determin if this action should be displayed.
action :deactivate, :if => :active
38 39 40 |
# File 'lib/app_kit/action.rb', line 38 def if_method @if_method end |
#name ⇒ Object
The name of the aciton. This will be used to display the link title of the action itself. It will also be used as a the name of the method on the model class to execute if a block is not given.
action :consolidate_items
This action would create a custom action visible when viewing a resource record. This action will also execute a method named consolidate_items on the model instance.
25 26 27 |
# File 'lib/app_kit/action.rb', line 25 def name @name end |
#resource ⇒ Object
The resoruce this action belongs to.
34 35 36 |
# File 'lib/app_kit/action.rb', line 34 def resource @resource end |
Instance Method Details
#action_type ⇒ Symbol
Determines if the action should call to a method or execute a block.
68 69 70 |
# File 'lib/app_kit/action.rb', line 68 def action_type (block.present? ? :block : :method_call) end |
#display_name ⇒ String
The display name to use when displaying a link to the action.
54 55 56 |
# File 'lib/app_kit/action.rb', line 54 def display_name name.humanize end |
#enabled_for_record?(record) ⇒ Boolean
Determines if the action should be displayed
61 62 63 64 |
# File 'lib/app_kit/action.rb', line 61 def enabled_for_record?(record) return true unless if_method true if record.send(if_method) end |
#is_block_action? ⇒ Boolean
Convienence method that detmines if the action should call a block.
78 79 80 |
# File 'lib/app_kit/action.rb', line 78 def is_block_action? action_type == :block end |
#is_method_action? ⇒ Boolean
Convienence method that detmines if the action should call a model method.
73 74 75 |
# File 'lib/app_kit/action.rb', line 73 def is_method_action? action_type == :method_call end |
#method_name ⇒ Symbol
The method name to call on the model.
84 85 86 |
# File 'lib/app_kit/action.rb', line 84 def method_name name end |