Class: Admin::Base::ActionExecutor
- Inherits:
-
Object
- Object
- Admin::Base::ActionExecutor
- Defined in:
- lib/admin/base/action_executor.rb
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#action_name ⇒ Object
readonly
Returns the value of attribute action_name.
-
#actor ⇒ Object
(also: #current_user)
readonly
Returns the value of attribute actor.
-
#resource_class ⇒ Object
readonly
Returns the value of attribute resource_class.
Instance Method Summary collapse
- #action_definition ⇒ Object
- #execute_bulk(records, params = {}) ⇒ Object
- #execute_collection(scope, params = {}) ⇒ Object
- #execute_member(record, params = {}) ⇒ Object
-
#initialize(resource_class, action_name, actor) ⇒ ActionExecutor
constructor
A new instance of ActionExecutor.
Constructor Details
#initialize(resource_class, action_name, actor) ⇒ ActionExecutor
Returns a new instance of ActionExecutor.
15 16 17 18 19 |
# File 'lib/admin/base/action_executor.rb', line 15 def initialize(resource_class, action_name, actor) @resource_class = resource_class @action_name = action_name @actor = actor end |
Instance Attribute Details
#action_name ⇒ Object (readonly)
Returns the value of attribute action_name.
6 7 8 |
# File 'lib/admin/base/action_executor.rb', line 6 def action_name @action_name end |
#actor ⇒ Object (readonly) Also known as: current_user
Returns the value of attribute actor.
6 7 8 |
# File 'lib/admin/base/action_executor.rb', line 6 def actor @actor end |
#resource_class ⇒ Object (readonly)
Returns the value of attribute resource_class.
6 7 8 |
# File 'lib/admin/base/action_executor.rb', line 6 def resource_class @resource_class end |
Instance Method Details
#action_definition ⇒ Object
51 52 53 |
# File 'lib/admin/base/action_executor.rb', line 51 def action_definition find_member_action || find_bulk_action || find_collection_action end |
#execute_bulk(records, params = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/admin/base/action_executor.rb', line 28 def execute_bulk(records, params = {}) action = find_bulk_action return failure_result("Action not found") unless action results = records.map { |record| execute_action(action, record, params) } success_count = results.count(&:success?) failure_count = results.count(&:failure?) if failure_count.zero? success_result("Successfully processed #{success_count} records") elsif success_count.zero? failure_result("Failed to process all #{failure_count} records") else success_result("Processed #{success_count} records, #{failure_count} failed") end end |
#execute_collection(scope, params = {}) ⇒ Object
45 46 47 48 49 |
# File 'lib/admin/base/action_executor.rb', line 45 def execute_collection(scope, params = {}) action = find_collection_action return failure_result("Action not found") unless action execute_action(action, scope, params) end |
#execute_member(record, params = {}) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/admin/base/action_executor.rb', line 21 def execute_member(record, params = {}) action = find_member_action return failure_result("Action not found") unless action return failure_result("Condition not met") unless condition_met?(action, record) execute_action(action, record, params) end |