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 Method Summary collapse

Instance Method Details

#fieldsArray<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:



44
45
46
# File 'lib/uchi/action.rb', line 44

def fields
  []
end

#nameString

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)


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.

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)


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.

Returns:

  • (Boolean)


62
63
64
# File 'lib/uchi/action.rb', line 62

def requires_input?
  fields.any?
end