Sha256: 2548deb61c500f7d5962ffcdefef6c631b2be4fec2b12e223f564699c536fc16

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

module NicePartials
  class Partial
    delegate_missing_to :@view_context

    def initialize(view_context)
      @view_context = view_context
      @key = SecureRandom.uuid
    end

    def yield(name = nil)
      raise "You can only use Nice Partial's yield method to retrieve the content of named content area blocks. If you're not trying to fetch the content of a named content area block, you don't need Nice Partials! You can just call the vanilla Rails `yield`." unless name
      content_for(name)
    end

    def helpers(&block)
      class_eval &block
    end

    # See the `ActionView::PartialRenderer` monkey patch in `lib/nice_partials/monkey_patch.rb` for something similar.
    def content_for(name, content = nil, options = {}, &block)
      if block_given?
        partial_prefix = nice_partials_locale_prefix_from_view_context_and_block(@view_context, block)
        @view_context.nice_partials_push_t_prefix(partial_prefix)
      end

      result = @view_context.content_for("#{name}_#{@key}".to_sym, content, options, &block)

      if block_given?
        @view_context.nice_partials_pop_t_prefix
      end

      return result
    end

    def content_for?(name)
      @view_context.content_for?("#{name}_#{@key}".to_sym)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nice_partials-0.1.6 lib/nice_partials/partial.rb
nice_partials-0.1.5 lib/nice_partials/partial.rb
nice_partials-0.1.4 lib/nice_partials/partial.rb
nice_partials-0.1.3 lib/nice_partials/partial.rb