Sha256: 1275b21a238dfe1441fa1a92c7b318cc876291e88416bead8222b7c83e53630b

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

module StrongPresenter
  module ControllerAdditions
    extend ActiveSupport::Concern

    module ClassMethods
      # @overload presents(*variables, options = {})
      #   Defines a helper method to access instance variables wrapped in presenters.
      #
      #   @example
      #     # app/controllers/articles_controller.rb
      #     class ArticlesController < ApplicationController
      #       presents :article
      #       presents :comments, with: ArticleCommentsPresenter, only: :show
      #       presents :comments, with: CommentsPresenter, only: :index { |presenter| presenter.permit(:author, :text) }
      #
      #       def show
      #         @article = Article.find(params[:id])
      #       end
      #     end
      #
      #     # app/views/articles/show.html.erb
      #     <%= article.presented_title %>
      #
      #   @param [Symbols*] variables
      #     names of the instance variables to present (without the `@`).
      #   @param [Hash] options
      #   @option options [Presenter, CollectionPresenter] :with (nil)
      #     presenter class to use. If nil, it is inferred from the instance
      #     variable.
      #   @option options [Symbols*] :only (nil)
      #     apply presenter only on these controller actions.
      #   @option options [Symbols*] :except (nil)
      #     don't apply presenter on these controller actions.
      #   @yield [Presenter] code to execute when presenter is initialized
      def presents(*variables, &block)
        options = variables.extract_options!
        options.assert_valid_keys(:with, :only, :except)

        constructor = StrongPresenter::PresenterHelperConstructor.new(self, block, options)

        variables.each do |variable|
          constructor.call(variable)
          helper_method variable
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
strong_presenter-0.2.2 lib/strong_presenter/controller_additions.rb
strong_presenter-0.2.1 lib/strong_presenter/controller_additions.rb
strong_presenter-0.2.0 lib/strong_presenter/controller_additions.rb
strong_presenter-0.1.0 lib/strong_presenter/controller_additions.rb