Sha256: 3075ead2c5208e0ba7d6ed30ca94423dc4efe7d49c9c72b3db467b5436d1c373

Contents?: true

Size: 1.55 KB

Versions: 17

Compression:

Stored size: 1.55 KB

Contents

module Draper
  module DecoratesAssigned
    # @overload decorates_assigned(*variables, options = {})
    #   Defines a helper method to access decorated instance variables.
    #
    #   @example
    #     # app/controllers/articles_controller.rb
    #     class ArticlesController < ApplicationController
    #       decorates_assigned :article
    #
    #       def show
    #         @article = Article.find(params[:id])
    #       end
    #     end
    #
    #     # app/views/articles/show.html.erb
    #     <%= article.decorated_title %>
    #
    #   @param [Symbols*] variables
    #     names of the instance variables to decorate (without the `@`).
    #   @param [Hash] options
    #   @option options [Decorator, CollectionDecorator] :with (nil)
    #     decorator class to use. If nil, it is inferred from the instance
    #     variable.
    #   @option options [Hash, #call] :context
    #     extra data to be stored in the decorator. If a Proc is given, it will
    #     be passed the controller and should return a new context hash.
    def decorates_assigned(*variables)
      factory = Draper::Factory.new(variables.extract_options!)

      variables.each do |variable|
        undecorated = "@#{variable}"
        decorated = "@decorated_#{variable}"

        define_method variable do
          return instance_variable_get(decorated) if instance_variable_defined?(decorated)
          instance_variable_set decorated, factory.decorate(instance_variable_get(undecorated), context_args: self)
        end

        helper_method variable
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
draper-4.0.2 lib/draper/decorates_assigned.rb
draper-4.0.1 lib/draper/decorates_assigned.rb
draper-4.0.0 lib/draper/decorates_assigned.rb
draper-3.1.0 lib/draper/decorates_assigned.rb
draper-3.0.1 lib/draper/decorates_assigned.rb
draper-3.0.0 lib/draper/decorates_assigned.rb
draper-3.0.0.pre1 lib/draper/decorates_assigned.rb
draper_new-3.0.0 lib/draper/decorates_assigned.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/draper-2.1.0/lib/draper/decorates_assigned.rb
draper-2.1.0 lib/draper/decorates_assigned.rb
draper-2.0.0 lib/draper/decorates_assigned.rb
draper-1.4.0 lib/draper/decorates_assigned.rb
draper-1.3.1 lib/draper/decorates_assigned.rb
draper-1.3.0 lib/draper/decorates_assigned.rb
draper-1.2.1 lib/draper/decorates_assigned.rb
draper-1.2.0 lib/draper/decorates_assigned.rb
jamesgolick-draper-1.1.1a lib/draper/decorates_assigned.rb