Module: Tokamak::Hook::Rails::Helpers

Defined in:
lib/tokamak/hook/rails.rb

Instance Method Summary collapse

Instance Method Details

#partial(partial_path, caller_binding = nil) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tokamak/hook/rails.rb', line 43

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 @content_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