Module: ActionViewTemplateInheritance::ActionView

Defined in:
lib/actionview_template_inheritance.rb

Instance Method Summary collapse

Instance Method Details

#block(name, value = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
# File 'lib/actionview_template_inheritance.rb', line 7

def block(name, value = nil, &block)
  raise ArgumentError, "Block has to have a name!" if name.nil?
  raise ArgumentError, "You have to provide value or block, not both of them!" if value && block
  self.blocks[name] ||= block ? capture(&block) : value
  if self.blocks[:appends][name]
    appends = self.blocks[:appends][name].join("\n").html_safe
  end
  return (self.blocks[name] || "") + (appends || "")
end

#blocksObject



3
4
5
# File 'lib/actionview_template_inheritance.rb', line 3

def blocks
  @inheritance_blocks ||= { :appends => {} }
end

#clear_block(name) ⇒ Object



24
25
26
# File 'lib/actionview_template_inheritance.rb', line 24

def clear_block(name)
  block(name, "")
end

#enhance_block(name, value = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
# File 'lib/actionview_template_inheritance.rb', line 17

def enhance_block(name, value = nil, &block)
  raise ArgumentError, "Block has to have a name!" if name.nil?
  raise ArgumentError, "You have to provide value or block, not both of them!" if value && block
  self.blocks[:appends][name] ||= []
  self.blocks[:appends][name].unshift(block ? capture(&block) : value)
end

#inherit(options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/actionview_template_inheritance.rb', line 28

def inherit(options = {}, &block)
  # We accept a shorthand syntax -- if options is a string, render as a file.
  return inherit({:file => options}, &block) if options.is_a?(String)

  bind = options[:binding]

  # Get our differences and additions to the view we're inheriting.
  if block_given?
    capture(&block)
    bind ||= block.binding
  end

  raise "Important: inherit() requires a block. Empty one ({}) is fine though." unless bind 

  # Render our parent view.
  render(options)
end