Module: Zena::Use::Rendering::ControllerMethods
- Defined in:
- lib/zena/use/rendering.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#admin_layout ⇒ Object
Find the proper layout to render ‘admin’ actions.
-
#cache_page(opts = {}) ⇒ Object
Cache page content into a static file in the current sites directory : SITES_ROOT/test.host/public.
-
#caching_allowed(opts = {}) ⇒ Object
Return true if we can cache the current page.
- #js_data ⇒ Object
-
#page_cache_file(url = nil) ⇒ Object
Cache file path that reflects the called url.
-
#popup_layout ⇒ Object
TODO: test.
-
#render_404(exception) ⇒ Object
TODO: test.
-
#render_500(exception) ⇒ Object
TODO: test.
- #render_and_cache(options = {}) ⇒ Object
- #rescue_action(exception) ⇒ Object
-
#rescue_action_in_public(exception) ⇒ Object
TODO: test Our own handling of exceptions.
- #set_caching(opts) ⇒ Object
-
#title_for_layout ⇒ Object
Return the window title to use.
-
#visitor_node ⇒ Object
Use the current visitor as master node.
- #zafu_headers ⇒ Object
Class Method Details
.included(base) ⇒ Object
36 37 38 39 40 |
# File 'lib/zena/use/rendering.rb', line 36 def self.included(base) base.send(:helper_attr, :js_data, :zafu_headers) base.send(:helper_method, :set_caching) base.send(:layout, false) end |
Instance Method Details
#admin_layout ⇒ Object
Find the proper layout to render ‘admin’ actions. The layout is searched into the visitor’s contact’s skin first and then into default. This action is also responsible for setting a default @title_for_layout.
312 313 314 315 |
# File 'lib/zena/use/rendering.rb', line 312 def admin_layout @title_for_layout ||= "#{params[:controller]}/#{params[:action]}" template_url(:mode => '+adminLayout') end |
#cache_page(opts = {}) ⇒ Object
Cache page content into a static file in the current sites directory : SITES_ROOT/test.host/public
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/zena/use/rendering.rb', line 255 def cache_page(opts={}) if cachestamp_format?(params['format']) headers['Expires'] = (Time.now + 365*24*3600).strftime("%a, %d %b %Y %H:%M:%S GMT") headers['Cache-Control'] = (!current_site.authentication? && @node.v_public?) ? 'public' : 'private' end if perform_caching && caching_allowed(:authenticated => opts.delete(:authenticated)) url = page_cache_file(opts.delete(:url)) opts = {:expire_after => nil, :path => (current_site.cache_path + url), :content_data => response.body, :node_id => @node[:id] }.merge(opts) if secure!(CachedPage) { CachedPage.create(opts) } true else false end else false end end |
#caching_allowed(opts = {}) ⇒ Object
Return true if we can cache the current page
281 282 283 284 285 286 |
# File 'lib/zena/use/rendering.rb', line 281 def caching_allowed(opts = {}) return false if current_site.authentication? || (query_params != {} && !@cache_query) # Cache even if authenticated (public content). # Content viewed by anonymous user should be cached anyway. opts[:authenticated] || visitor.is_anon? end |
#js_data ⇒ Object
42 43 44 |
# File 'lib/zena/use/rendering.rb', line 42 def js_data @js_data ||= [] end |
#page_cache_file(url = nil) ⇒ Object
Cache file path that reflects the called url
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/zena/use/rendering.rb', line 289 def page_cache_file(url = nil) path = url || url_for(:only_path => true, :skip_relative_url_root => true) path = ((path.empty? || path == "/") ? "/index" : URI.unescape(path)) ext = params[:format].blank? ? 'html' : params[:format] # FULL QUERY_STRING in cached page ? if @cache_query # This builds blog29.htmlp=2.html and it is OK (helps make rewrite rules simple) path << @cache_query << ".#{ext}" else path << ".#{ext}" unless path =~ /\.#{ext}(\?\d+|)$/ end if cachestamp_format?(params['format']) # Set expire response.headers['Expires'] = 1.year.from_now.httpdate end path end |
#popup_layout ⇒ Object
TODO: test
318 319 320 321 |
# File 'lib/zena/use/rendering.rb', line 318 def popup_layout js_data << "var is_editor = true;" template_url(:mode=>'+popupLayout') end |
#render_404(exception) ⇒ Object
TODO: test
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/zena/use/rendering.rb', line 77 def render_404(exception) if Thread.current[:visitor] # page not found @node = current_site.root_node zafu_node('@node', Project) respond_to do |format| format.all do if request.format == Mime::XML render :nothing => true, :status => "404 Not Found" else not_found = "#{SITES_ROOT}/#{current_site.host}/public/#{prefix}/404.html" if File.exists?(not_found) render :text => File.read(not_found), :status => '404 Not Found' else render_and_cache :mode => '+notFound', :format => 'html', :cache_url => "/#{prefix}/404.html", :status => '404 Not Found' end end end end else # site not found respond_to do |format| format.html { render :text => File.read("#{Zena::ROOT}/app/views/sites/404.html"), :status => '404 Not Found' } format.all { render :nothing => true, :status => "404 Not Found" } end end rescue ActiveRecord::RecordNotFound => err # this is bad render_500(err) end |
#render_500(exception) ⇒ Object
TODO: test
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/zena/use/rendering.rb', line 110 def render_500(exception) # TODO: send an email with the exception ? # msg =<<-END_MSG # Something bad happened to your zena installation: # -------------------------- # #{exception.message} # -------------------------- # #{exception.backtrace.join("\n")} # END_MSG respond_to do |format| format.html { render :text => File.read("#{Zena::ROOT}/app/views/nodes/500.html"), :status => '500 Error' } format.all { render :nothing => true, :status => "500 Error" } end end |
#render_and_cache(options = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/zena/use/rendering.rb', line 127 def render_and_cache(={}) # FIXME: maybe we can remove this. @title_for_layout = title_for_layout opts = {:skin => @node[:skin], :cache => true}.merge() opts[:mode ] ||= params[:mode] opts[:format] ||= params[:format].blank? ? 'html' : params[:format] # # cleanup before rendering # params.delete(:mode) if opts[:format] != 'html' method = "render_to_#{opts[:format]}" if params.keys.include?('debug') opts[:cache] = false opts[:debug] = true end if respond_to?(method) # Call custom rendering engine 'render_to_pdf' for example. result = send(method, opts) else template_path = template_url(opts) # FIXME: Use Mime types to resolve content type... content_type = (Zena::EXT_TO_TYPE[opts[:format]] || ['application/octet-stream'])[0] result = { :data => render_to_string(:file => template_path, :layout=>false), :disposition => 'inline', :type => content_type, :filename => @node.title } end if error = result[:error] # error reporting from rendering engine opts[:cache] = false @render_error = error render :file => 'nodes/render_error' elsif result[:type] == 'text/html' # debugging mode from rendering engine opts[:cache] = false render :inline => result[:data] else if disposition = zafu_headers.delete('Content-Disposition') result.delete(:filename) if disposition =~ /filename\s*=/ result[:disposition] = disposition end if type = zafu_headers.delete('Content-Type') result[:type] = type end if filename = zafu_headers.delete('filename') result[:filename] = filename end if status = zafu_headers.delete('Status') if (status.to_i / 100) == 3 redirect_to zafu_headers.delete('Location'), :status => status.to_i else render :status => status.to_i end headers.merge!(zafu_headers) return end if data = result.delete(:data) send_data(data , result) elsif file = result.delete(:file) send_file(file , result) else # Should never happen raise Exception.new("Render '#{params[:format]}' should return either :file or :data (none found).") end headers.merge!(zafu_headers) end cache_page(:content_data => result[:data], :content_path => result[:file]) if opts[:cache] else # html if opts.delete(:as_string) render_to_string :file => template_url(opts), :layout => false else render :file => template_url(opts), :layout => false, :status => opts[:status] if status = zafu_headers.delete('Status') # reset rendering response.content_type = nil erase_render_results reset_variables_added_to_assigns if (status.to_i / 100) == 3 redirect_to zafu_headers.delete('Location'), :status => status.to_i else render :status => status.to_i end headers.merge!(zafu_headers) return end headers.merge!(zafu_headers) cache_page(:url => opts[:cache_url]) if opts[:cache] end end # This does not work, Rendering::Redirect is wrapped in TemplateError # rescue Zena::Use::Rendering::Redirect # This does not work either: infinity loop, CPU hog on errors. # rescue ActionView::TemplateError => err # orig = err.original_exception # if orig.kind_of?(Zena::Use::Rendering::Redirect) # redirect_to orig.url # else # raise err # end rescue ActiveRecord::RecordNotFound => err return render_404(err) end |
#rescue_action(exception) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/zena/use/rendering.rb', line 67 def rescue_action(exception) case exception when ActiveRecord::RecordNotFound, ActionController::UnknownAction render_404(exception) else super end end |
#rescue_action_in_public(exception) ⇒ Object
TODO: test Our own handling of exceptions
58 59 60 61 62 63 64 65 |
# File 'lib/zena/use/rendering.rb', line 58 def rescue_action_in_public(exception) case exception when ActiveRecord::RecordNotFound, ActionController::UnknownAction render_404(exception) else render_500(exception) end end |
#set_caching(opts) ⇒ Object
50 51 52 53 54 |
# File 'lib/zena/use/rendering.rb', line 50 def set_caching(opts) if request.query_string =~ opts[:allow_query] @cache_query = request.query_string end end |
#title_for_layout ⇒ Object
Return the window title to use.
324 325 326 327 |
# File 'lib/zena/use/rendering.rb', line 324 def title_for_layout return '' unless @node @node.title + (@node.kind_of?(Document) ? ".#{@node.ext}" : '') end |
#visitor_node ⇒ Object
Use the current visitor as master node.
330 331 332 333 |
# File 'lib/zena/use/rendering.rb', line 330 def visitor_node @node = visitor.node zafu_node('@node', Node) end |
#zafu_headers ⇒ Object
46 47 48 |
# File 'lib/zena/use/rendering.rb', line 46 def zafu_headers @zafu_headers ||= {} end |