action_controller-expose

Declarative helper-accessible readers (with optional lazy memoization) for ActionController.

Usage

class CommentsController < ApplicationController
  # Multiple attr_reader + helper_method declarations.
  # Controller actions assign @foo / @bar; views read `foo` / `bar`.
  expose :foo, :bar

  # Lazy memoized reader. First call runs the block, later calls return cached
  # result. Memoization via `instance_variable_defined?`, so nil/false cache.
  expose :comment,  -> { Comment.find(params[:id]) }
  expose :audition, -> { Audition.medium_ids_including(comment.medium_id).first }
  expose :shoot,    -> { audition.shoots.first }

  # Or with a block:
  expose(:current_thing) { Thing.find(params[:thing_id]) }
end

All forms call helper_method, so the views can call the names without @.

Installation

gem "action_controller-expose"

License

MIT