Module: Fortitude::Widget::Rendering

Extended by:
ActiveSupport::Concern
Included in:
Fortitude::Widget
Defined in:
lib/fortitude/widget/rendering.rb

Constant Summary collapse

POTENTIAL_NEW_BUFFER_CLASSES =
%w{ActionView::OutputBuffer ActiveSupport::SafeBuffer String}

Instance Method Summary collapse

Instance Method Details

#_fortitude_run_content_blockObject

INTERNAL USE ONLY



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fortitude/widget/rendering.rb', line 114

def _fortitude_run_content_block
  if @_fortitude_block_for_content_method
    @_fortitude_block_for_content_method
  elsif @_fortitude_constructor_block
    lambda { |*args| @_fortitude_constructor_block.call(self, *args) }
  elsif @_fortitude_rendering_context.effective_yield_block
    @_fortitude_rendering_context.effective_yield_block
  else
    nil
  end
end

#call_blockObject

PUBLIC API (Erector compatibility)



137
138
139
# File 'lib/fortitude/widget/rendering.rb', line 137

def call_block
  yield_from_widget
end

#initialize(assigns = { }, &block) ⇒ Object

PUBLIC API



83
84
85
86
# File 'lib/fortitude/widget/rendering.rb', line 83

def initialize(assigns = { }, &block)
  assign_locals_from(assigns)
  @_fortitude_constructor_block = block
end

#output_bufferObject

PUBLIC API



78
79
80
# File 'lib/fortitude/widget/rendering.rb', line 78

def output_buffer
  @_fortitude_output_buffer_holder.output_buffer
end

#render(*args, &block) ⇒ Object

PUBLIC API



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fortitude/widget/rendering.rb', line 12

def render(*args, &block)
  call_through = lambda do
    @_fortitude_rendering_context.record_render(args) do
      tag_rawtext(invoke_helper(:render, *args, &block))
    end
  end

  if self.class._fortitude_record_emitting_tag? && args[0].kind_of?(Hash) && args[0].has_key?(:partial)
    @_fortitude_rendering_context.emitting_tag!(self, Fortitude::Tags::PartialTagPlaceholder.instance, nil, nil, &call_through)
  else
    call_through.call
  end
end

#render_to(rendering_context, &block_for_content_method) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fortitude/widget/rendering.rb', line 26

def render_to(rendering_context, &block_for_content_method)
  @_fortitude_rendering_context = rendering_context
  @_fortitude_output_buffer_holder = rendering_context.output_buffer_holder
  @_fortitude_block_for_content_method = block_for_content_method

  rendering_context.record_widget(self) do
    begin
      run_content(&_fortitude_run_content_block)
    ensure
      @_fortitude_rendering_context = nil
      @_fortitude_block_for_content_method = nil
    end
  end
end

#rendering_contextObject

PUBLIC API



49
50
51
# File 'lib/fortitude/widget/rendering.rb', line 49

def rendering_context
  @_fortitude_rendering_context
end

#to_html(rendering_context = nil) ⇒ Object

PUBLIC API



42
43
44
45
46
# File 'lib/fortitude/widget/rendering.rb', line 42

def to_html(rendering_context = nil)
  rendering_context ||= ::Fortitude::RenderingContext.default_rendering_context
  render_to(rendering_context)
  rendering_context.output_buffer_holder.output_buffer
end

#widget(w, hash = nil, &block) ⇒ Object

PUBLIC API



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fortitude/widget/rendering.rb', line 54

def widget(w, hash = nil, &block)
  if w.kind_of?(Class) && ((w < ::Fortitude::Widget) || ::Fortitude::Erector.is_erector_widget_class?(w))
    hash ||= { }
    w = w.new(hash)
  end

  if w.kind_of?(::Fortitude::Widget)
    begin
      @_fortitude_in_block_for_sub_widget = w if block
      w.render_to(@_fortitude_rendering_context, &block)
    ensure
      @_fortitude_in_block_for_sub_widget = nil
    end
  elsif ::Fortitude::Erector.is_erector_widget?(w)
    w.send(:_emit,
      :parent => rendering_context.helpers_object,
      :helpers => rendering_context.helpers_object,
      :output => rendering_context.output_buffer_holder.output_buffer)
  else
    raise "You tried to render a widget, but this is not valid: #{w.inspect}(#{hash.inspect})"
  end
end

#yield_from_widget(*args) ⇒ Object

PUBLIC API



127
128
129
130
131
132
133
134
# File 'lib/fortitude/widget/rendering.rb', line 127

def yield_from_widget(*args)
  block = _fortitude_run_content_block
  if block
    block.call(*args)
  else
    raise Fortitude::Errors::NoBlockToYieldTo.new(self)
  end
end