Sha256: 448ad52236c296a41268d241bf2c734f9a4dee364acf4bd578cfb00c6927a950

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module Restfulie
  module Server
    module ActionView
      module Helpers
        # Load a partial template to execute in describe
        #
        # For example:
        #
        # Passing the current context to partial in template:
        #
        #  member(@album) do |member, album|
        #    partial('member', binding)
        #  end
        #
        # in partial:
        #
        #  member.links << link(:rel => :artists, :href => album_artists_url(album))
        #
        # Or passing local variables assing
        #
        # collection(@albums) do |collection|
        #   collection.members do |member, album|
        #     partial("member", :locals => {:member => member, :album => album})
        #   end
        # end
        #
        def partial(partial_path, caller_binding = nil)
          template = _pick_partial_template(partial_path)
      
          # Create a context to assing variables
          if caller_binding.kind_of?(Hash)
            Proc.new do
              extend @restfulie_type_helpers
              context = eval("(class << self; self; end)", binding)
              
              unless caller_binding[:locals].nil?
                caller_binding[:locals].each do |k, v|
                  context.send(:define_method, k.to_sym) { v }
                end
              end
              
              partial(partial_path, binding)
            end.call
          else
            eval(template.source, caller_binding, template.path)
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restfulie-0.9.1 lib/restfulie/server/action_view/helpers.rb
restfulie-0.8.1 lib/restfulie/server/action_view/helpers.rb