lib/spontaneous/rack/public.rb in spontaneous-0.2.0.alpha2 vs lib/spontaneous/rack/public.rb in spontaneous-0.2.0.alpha3

- old
+ new

@@ -11,10 +11,11 @@ attr_reader :env, :response, :request attr_accessor :page def render_path(path) + @template_params = {} find_page!(path) response = catch(:halt) do if @page case @request.request_method @@ -50,11 +51,11 @@ # stolen from Sinatra def parse_response(response) case - when response.respond_to?(:spontaneous_content?) + when response.is_a?(Spontaneous::Content) @page = response when response.respond_to?(:to_str) @response.body = [response] when response.respond_to?(:to_ary) response = response.to_ary @@ -86,10 +87,15 @@ page = Spontaneous::Site[page] if String === page @page = page status(status) end + def render(page, template_params = {}) + @template_params = template_params + show(page) + end + REDIRECTS = { :permanent => 301, :temporary => 302 } @@ -120,112 +126,96 @@ mime_type << ";#{params.map { |kv| kv.join('=') }.join(', ')}" unless params.empty? response['Content-Type'] = mime_type end def find_page!(path) - @path, @format, @action = parse_path(path) + @path, @output, @action = parse_path(path) @page = Site[@path] end - def format - @format + def output + @output end def action @action end def render_get - # return not_found! unless @page - - @format = (@format || @page.default_format).to_sym if @page - if @action call_action! else block = page.request_block(request) parse_response(instance_eval(&block)) if (block) - - @format = (@format || @page.default_format).to_sym if @page - render_page_with_format(@page, @format) + render_page_with_output end end # non-action urls shouldn't respond to post requests def render_post - # return not_found! unless @page - block = page.request_block(request) return not_found! unless (block or @action) if @action call_action! else parse_response(instance_eval(&block)) if (block) - - @format = (@format || @page.default_format).to_sym if @page - render_page_with_format(@page, @format) + render_page_with_output end end def call_action! - # get - status, headers, result = @page.process_action(action, request.env, format) - # our 404 page should come from the CMS + status, headers, result = @page.process_action(action, request.env, output) if status == 404 not_found! else - if result.respond_to?(:spontaneous_content?) - render_page_with_format(result, format) + if result.is_a?(Spontaneous::Content) + @page = result + render_page_with_output else [status, headers, result] end end end def parse_path(path) if path =~ %r(#{ACTION}) path, action = path.split(ACTION) + path = "/" if path.empty? action, format = action.split(DOT) else path, format = path.split(DOT) end - # format = (format || :html).to_sym [path, format, action] end - def render_page_with_format(page, format) - if page && page.provides_format?(format) - content_type(page.mime_type(format)) - render_page(page, format) + def render_page_with_output + return not_found! if @page.nil? + return not_found! unless @page.provides_output?(@output) + + output = @page.output(@output) + + if output.public? + content_type(output.mime_type) + render_page(@page, output, @template_params) else - # perhaps we should return the html version if the page exists but - # doesn't respond to the requested format? - # or even redirect to the html? not_found! end end - def render_page(page, format = :html, local_params = {}) - response.body = page.render(format, local_params.merge({ - :params => request.params, + def renderer + env[Rack::RENDERER] + end + + def render_page(page, output, local_params = {}) + response.body = output.render_using(renderer, local_params.merge({ + :params => params, # use sinatras indifferent params :request => request, :session => request.session })) end - # def redirect?(page) - # redirection, redirect_code = page.request_redirect(request.params, request, request.session) - # if redirection - # redirection = redirection.path if redirection.respond_to?(:path) - # redirect_code = REDIRECTS[redirect_code] if Symbol === redirect_code - # redirect_code ||= REDIRECTS[:temporary] - # [redirection.to_s, redirect_code] - # else - # nil - # end - # end - + # our 404 page should come from the CMS def not_found! 404 end end end