Class: Uchi::Action

Inherits:
Object
  • Object
show all
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 Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#context ⇒ Uchi::Context

Returns the context in which the action is performed.

Returns:

  • (Uchi::Context) —

    the context in which the action is performed



28
29
30
# File 'lib/uchi/action.rb', line 28

def context
  @context
end

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).

Returns:



47
48
49
# File 'lib/uchi/action.rb', line 47

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.

Returns:

  • (String)


37
38
39
# File 'lib/uchi/action.rb', line 37

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.

Parameters:

  • records (ActiveRecord::Relation, Array) —
    • The records to operate on
  • input (Hash) (defaults to: {}) —
    • Hash of field values from the action form

Returns:

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/uchi/action.rb', line 58

def perform(records, input = {})
  raise NotImplementedError, "#{self.class}#perform must be implemented"
end

#requires_input? ⇒ Boolean

Returns true if this action requires input fields.

Returns:

  • (Boolean)


65
66
67
# File 'lib/uchi/action.rb', line 65

def requires_input?
  fields.any?
end