Class: Uchi::Action
- Inherits:
-
Object
- Object
- Uchi::Action
- Defined in:
- lib/uchi/action.rb
Overview
Base class for all Uchi actions.
Actions allow you to perform custom operations on one or more records from a repository. Examples include publishing posts, exporting data, or sending notifications.
To create an action, subclass this class and implement the perform method:
class PublishPost < Uchi::Action
def perform(records, input = {})
records.each { |record| record.update!(published: true) }
Uchi::ActionResponse.success("Published #{records.size} posts")
end
end
Actions are registered on repositories via the actions method:
class PostRepository < Uchi::Repository
def actions
[PublishPost.new]
end
end
Instance Method Summary collapse
-
#fields ⇒ Array<Uchi::Field>
Returns the list of fields to show in the action form.
-
#name ⇒ String
Returns the display name for this action.
-
#perform(records, input = {}) ⇒ Uchi::ActionResponse
Performs the action on the given records.
-
#requires_input? ⇒ Boolean
Returns true if this action requires input fields.
Instance Method Details
#fields ⇒ Array<Uchi::Field>
Returns the list of fields to show in the action form.
Fields are instances of Uchi::Field subclasses (e.g., Field::String, Field::Boolean).
44 45 46 |
# File 'lib/uchi/action.rb', line 44 def fields [] end |
#name ⇒ String
Returns the display name for this action.
By default, this looks up the translation key
uchi.action.[action_key].name and falls back to the humanized class
name.
34 35 36 |
# File 'lib/uchi/action.rb', line 34 def name translate(:name, default: self.class.name.demodulize.titleize) end |
#perform(records, input = {}) ⇒ Uchi::ActionResponse
Performs the action on the given records.
This method must be implemented in subclasses.
55 56 57 |
# File 'lib/uchi/action.rb', line 55 def perform(records, input = {}) raise NotImplementedError, "#{self.class}#perform must be implemented" end |
#requires_input? ⇒ Boolean
Returns true if this action requires input fields.
62 63 64 |
# File 'lib/uchi/action.rb', line 62 def requires_input? fields.any? end |