Module: MimeActor::Scene::ClassMethods

Defined in:
lib/mime_actor/scene.rb

Instance Method Summary collapse

Instance Method Details

#act_on_action(*actions, format:, with: nil, &block) ⇒ Object

Register action + format definitions.

For each unique action being registered, a corresponding action method will be defined.

Examples:

register a html format on action create

act_on_action :create, format: :html

# an action method will be defined in the class
def indexl; end

register html, json formats on actions index, show

act_on_action :index, :show, format: [:html, :json]

# these action methods will be defined in the class
def index; end
def show; end

register a html format on action index with respond handler method

act_on_action :index, format: :html, with: :render_html

# an action method will be defined in the class
# the handler method will be called by the action method.
def index; end
# the given method should be defined in the class
def render_html; end

register a html format on action index with respond handler Proc

act_on_action :index, format: :html, with: -> { render html: "<h1>my header</h1>" }

# an action method will be defined in the class,
# the handler Proc will be called by the action method.
def index; end

register a html format on action index with respond handler block

act_on_action :html, on: :index do
  render :index
end

# an action method will be defined in the class
def index; end

Parameters:

  • a collection of action

  • a single format or a collection of format

  • (defaults to: nil)

    the respond handler for the given action + format

  • the block to be yieled for the given action + format

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mime_actor/scene.rb', line 87

def act_on_action(*actions, format:, with: nil, &block)
  raise ArgumentError, "format is required" if format.nil?
  raise ArgumentError, "provide either with: or a block" if !with.nil? && block_given?

  validate!(:actions, actions)
  validate!(:format_or_formats, format)
  validate!(:callable, with) unless with.nil?
  with = block if block_given?

  actions.each do |action|
    acting_scenes[action] ||= {}
    Array(format).each do |action_format|
      acting_scenes[action][action_format] = with
    end
  end

  ActiveSupport.version >= "7.2" ? generate_action_methods(actions) : eval_action_methods(actions)
end