Module: Fortitude::Rails::RenderingMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/fortitude/rails/rendering_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._fortitude_register_renderer!Object



57
58
59
60
61
# File 'lib/fortitude/rails/rendering_methods.rb', line 57

def self._fortitude_register_renderer!
  ::ActionController.add_renderer_without_fortitude(:widget) do |widget, options|
    ::Fortitude::Rails::RenderingMethods._fortitude_render_widget(self, widget, options)
  end
end

._fortitude_render_widget(controller, widget, options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fortitude/rails/rendering_methods.rb', line 23

def self._fortitude_render_widget(controller, widget, options)
  if ::Fortitude::Erector.is_erector_widget_class?(widget) || ::Fortitude::Erector.is_erector_widget_class?(widget.class)
    return ::Erector::Rails.render(widget, controller.view_context, { }, false, options)
  end

  view_context = controller.view_context

  if widget.kind_of?(Class)
    if widget < ::Fortitude::Widget
      widget = widget.new(widget.extract_needed_assigns_from(view_context.assigns))
    else
      raise "You tried to render something using 'render :widget' that is a class, but not a subclass of Fortitude::Widget: #{widget.inspect}"
    end
  end

  if (! widget.kind_of?(::Fortitude::Widget))
    raise "You tried to render something using 'render :widget' that is neither an instance of a subclass of Fortitude::Widget, nor a class that inherits from Fortitude::Widget: #{widget.inspect}"
  end

  rendering_context = controller.create_fortitude_rendering_context(
    :helpers_object => view_context, :output_buffer_holder => view_context)

  output_buffer = view_context.with_output_buffer do
    widget.render_to(rendering_context)
  end

  passed_options = options.dup
  passed_options.delete(:widget)
  passed_options[:text] = output_buffer.to_s
  passed_options[:layout] = true unless passed_options.has_key?(:layout)

  return controller.render_to_string(passed_options)
end

Instance Method Details

#create_fortitude_rendering_context(options) ⇒ Object



19
20
21
# File 'lib/fortitude/rails/rendering_methods.rb', line 19

def create_fortitude_rendering_context(options)
  ::Fortitude::RenderingContext.new(options)
end

#fortitude_rendering_context_for(delegate_object, yield_block) ⇒ Object



13
14
15
16
17
# File 'lib/fortitude/rails/rendering_methods.rb', line 13

def fortitude_rendering_context_for(delegate_object, yield_block)
  @_fortitude_rendering_contexts ||= { }
  @_fortitude_rendering_contexts[delegate_object.object_id] ||= create_fortitude_rendering_context(
    :delegate_object => delegate_object, :yield_block => yield_block)
end

#render_with_fortitude(*args, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fortitude/rails/rendering_methods.rb', line 63

def render_with_fortitude(*args, &block)
  if (options = args[0]).kind_of?(Hash) && (widget_block = options[:inline]) && (options[:type] == :fortitude)
    options.delete(:inline)

    rendering_context = fortitude_rendering_context_for(self, nil)
    widget_class = Class.new(Fortitude::Widgets::Html5)
    widget_class.use_instance_variables_for_assigns(true)
    widget_class.extra_assigns(:use)
    widget_class.send(:define_method, :content, &widget_block)

    assigns = { }
    instance_variables.each do |ivar_name|
      value = instance_variable_get(ivar_name)
      assigns[$1.to_sym] = value if ivar_name =~ /^@([^_].*)$/
    end
    assigns = assigns.merge(options[:locals] || { })

    widget = widget_class.new(assigns)
    new_args = [ options.merge(:widget => widget) ] + args[1..-1]
    return render_without_fortitude(*new_args, &block)
  end

  return render_without_fortitude(*args, &block)
end