Sha256: 39d402c300d10a4c7fd112c2df3df2e18d039c74558c5ff60518b5149f1be86d

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

module Restfulie::Server::ActionView::Helpers
  # Load a partial template to execute in describe
  #
  # For example:
  #
  # Passing the current context to partial in template:
  #
  #  describe_member(@album) do |member, album|
  #    partial('member', binding)
  #  end
  #
  # in partial:
  #
  #  member.links << link(:rel => :artistis, :href => album_artists_url(album))
  #
  # Or passing local variables assing
  #  
  #  descirbe_collection(@albums) do |collection|
  #    collection.describe_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::Common::Builder::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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restfulie-0.7.1 lib/restfulie/server/action_view/helpers.rb
restfulie-0.7.0 lib/restfulie/server/action_view/helpers.rb