Sha256: 3a1672fd3f7fc788440f6df3db5a15278907ae2c05ec77138e8b19d0a2ab3b57

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

module Merb
  module PartsMixin
    
    # Dispatches a PartController.
    # ==== Parameters
    # opts<Hash>:: A Hash of Options. (see below)
    #
    # ==== Options
    # An option hash has two parts.
    # 1. keys that are Merb::PartControllers with values that are action names (as symbols)
    # 2. key value pairs that will become available in the PartController as params merged
    #    with the web controllers params
    #
    # ==== Example
    #  Calling a part controller
    #  {{{
    #    part TodoPart => :list
    #   }}}
    #
    #  Calling a part with additional options
    #  {{{
    #    part TodoPart => :list, :limit => 20, :user => current_user 
    #  }}}
    #
    # ==== Returns
    #   Returns the result of the PartControllers action, which is a string.
    def part(opts = {})
      # Extract any params out that may have been specified
      klasses, opts = opts.partition do |k,v| 
        k.respond_to?(:ancestors) && k.ancestors.include?(Merb::PartController)
      end       

      opts = opts.empty? ? {} : Hash[*(opts.first)]
      
      res = klasses.inject([]) do |memo,(klass,action)|
        memo << klass.new(self, opts)._dispatch(action)
      end
      res.size == 1 ? res[0] : res
    end
    
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
merb-parts-0.9.8 lib/merb-parts/mixins/parts_mixin.rb
merb_parts-0.9.12 lib/merb_parts/mixins/parts_mixin.rb
merb_parts-0.9.9 lib/merb-parts/mixins/parts_mixin.rb
merb_parts-0.9.11 lib/merb_parts/mixins/parts_mixin.rb
merb_parts-0.9.10 lib/merb_parts/mixins/parts_mixin.rb
merb_parts-0.9.13 lib/merb_parts/mixins/parts_mixin.rb