Module | Cms::Behaviors::InstanceMethods |
In: |
lib/cms/behaviors/rendering.rb
|
# File lib/cms/behaviors/rendering.rb, line 99 99: def perform_render(controller) 100: return "Exception: #{@render_exception}" if @render_exception 101: unless @controller 102: # We haven't prepared to render. This should only happen when logged in, as we don't want 103: # errors to bubble up and prevent the page being edited in that case. 104: prepare_to_render(controller) 105: end 106: 107: # Create, Instantiate and Initialize the view 108: view_class = Class.new(ActionView::Base) 109: action_view = view_class.new(@controller.view_paths, {}, @controller) 110: 111: # Make helpers and instance vars available 112: view_class.send(:include, @controller.class.master_helper_module) 113: if $:.detect{|d| File.exists?(File.join(d, self.class.helper_path))} 114: view_class.send(:include, self.class.helper_class) 115: end 116: 117: # We want content_for to be called on the controller's view, not this inner view 118: def action_view.content_for(name, content=nil, &block) 119: @controller.instance_variable_get("@template").content_for(name, content, &block) 120: end 121: 122: # Copy instance variables from this renderable object to it's view 123: action_view.assigns = assigns_for_view 124: 125: if respond_to?(:inline_options) && self.inline_options && self.inline_options.has_key?(:inline) 126: options = {:locals => {}}.merge(self.inline_options) 127: ActionView::InlineTemplate.new(options[:inline], options[:type]).render(action_view, options[:locals]) 128: else 129: action_view.render(:file => self.class.template_path) 130: end 131: end
# File lib/cms/behaviors/rendering.rb, line 84 84: def prepare_to_render(controller) 85: # Give this renderable a reference to the controller 86: @controller = controller 87: 88: copy_instance_variables_from_controller! 89: 90: # This gives the view a reference to this object 91: instance_variable_set(self.class.instance_variable_name_for_view, self) 92: 93: # This is like a controller action 94: # We will call it if you have defined a render method 95: # but if you haven't we won't 96: render if respond_to?(:render) 97: end
# File lib/cms/behaviors/rendering.rb, line 133 133: def render_exception=render_exception=(exception) 134: @render_exception = exception 135: end
# File lib/cms/behaviors/rendering.rb, line 147 147: def assigns_for_view 148: (instance_variables - self.class.ivars_to_ignore).inject({}) do |h,k| 149: h[k[1..-1]] = instance_variable_get(k) 150: h 151: end 152: end
# File lib/cms/behaviors/rendering.rb, line 138 138: def copy_instance_variables_from_controller! 139: if @controller.respond_to?(:instance_variables_for_rendering) 140: @controller.instance_variables_for_rendering.each do |iv| 141: #logger.info "Copying #{iv} => #{@controller.instance_variable_get(iv).inspect}" 142: instance_variable_set(iv, @controller.instance_variable_get(iv)) 143: end 144: end 145: end