Module: RenderWithMissingTemplate::ActionView::InstanceMethods

Defined in:
lib/render_with_missing_template/action_view.rb

Constant Summary collapse

RENDER_KEYS =
[:partial, :text, :template, :file, :inline]

Instance Method Summary collapse

Instance Method Details

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



12
13
14
15
16
17
18
19
20
21
22
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
56
57
# File 'lib/render_with_missing_template/action_view.rb', line 12

def render_with_defaults(options = {}, locals = {}, &block)
  if options.is_a?(Hash)
    defaults = options.delete(:if_missing)
    nested = options.delete(:nested)          
  elsif locals.is_a?(Hash)
    defaults = locals.delete(:if_missing)
    nested = locals.delete(:nested)          
  end

  # Prevents replacing :if_missing => false with empty hash.
  unless defaults.nil?
    begin
      render_without_defaults(options, locals, &block)
    rescue ::ActionView::MissingTemplate
      unless defaults == false
        defaults ||= {}
        new_options = defaults
        if options.is_a?(Hash)
          new_options = defaults.reverse_merge(options)
          
          # If user does something like
          #  render :partial => "test", :locals => {:temp => 1}, :if_missing => {:template => "missing"}
          # We should replace :partial => "test" with :template => "missing" and keep :locals.

          # Determine that render options have two template reference keys.
          render_keys = RENDER_KEYS.map { |k| k if new_options.include?(k) }.compact                
          if render_keys.size > 1
            # And delete unneccessary keys from default options.
            new_options = new_options.delete_if { |k, v| render_keys.include?(k) && options.include?(k) }
          end
        end
        render_without_defaults(new_options, locals, &block)
      end
    end
  else
    begin
      render_without_defaults(options, locals, &block)        
    rescue ::ActionView::MissingTemplate => e            
      if nested
        raise MissingTemplateContainer.new(e)
      else
        raise e
      end
    end
  end
end