Browse files

[#566] Preview Page

Add a 'Preview' page button that will open the window in a new tab with no frame.
  • Loading branch information...
1 parent d0e9f35 commit ba68de1fd444b006fdb903004af5c90e466e6619 @peakpg peakpg committed Mar 15, 2013
View
29 app/controllers/cms/content_controller.rb
@@ -12,7 +12,7 @@ class ContentController < Cms::ApplicationController
before_filter :construct_path_from_route, :only => [:show_page_route]
before_filter :try_to_redirect, :only => [:show]
before_filter :try_to_stream_file, :only => [:show]
- before_filter :check_access_to_page, :only => [:show, :show_page_route]
+ before_filter :check_access_to_page, :except => [:edit, :preview]
before_filter :select_cache_directory
@@ -35,6 +35,7 @@ def show_page_route
# Used in the iframe to display a page that's being editted.
def edit
@show_toolbar = true
+ @mode = "edit"
if page = Page.where(:id => params[:id]).first
@page = page.as_of_draft_version
render_page
@@ -46,6 +47,13 @@ def edit
end
+ def preview
+ @mode = "view"
+ @page = Page.find_draft(params[:id])
+ ensure_current_user_can_edit(@page)
+ render_page
+ end
+
# Used by the rendering behavior
def instance_variables_for_rendering
instance_variables - (@initial_ivars || []) - ["@initial_ivars"]
@@ -150,7 +158,6 @@ def try_to_stream_file
end
def check_access_to_page
- set_page_mode
if current_user.able_to?(:edit_content, :publish_content, :administrate)
logger.debug "Displaying draft version of page"
if page = Page.first(:conditions => {:path => @path})
@@ -166,24 +173,20 @@ def check_access_to_page
page_not_found unless (@page && !@page.archived?)
end
- unless current_user.able_to_view?(@page)
- store_location
- raise Cms::Errors::AccessDenied
- end
+ ensure_current_user_can_edit(@page)
end
# ----- Other Methods --------------------------------------------------------
+ def ensure_current_user_can_edit(page)
+ unless current_user.able_to_view?(page)
+ store_location
+ raise Cms::Errors::AccessDenied
+ end
+ end
def page_not_found
raise ActiveRecord::RecordNotFound.new("No page at '#{@path}'")
end
-
- def set_page_mode
- @mode = @show_toolbar && current_user.able_to?(:edit_content) ? (params[:mode] || session[:page_mode] || "edit") : "view"
- session[:page_mode] = @mode
- end
-
-
end
end
View
13 app/helpers/cms/page_helper.rb
@@ -78,8 +78,7 @@ def current_page
# @return [String] The HTML content for the container.
def container(name)
content = content_for(name)
-
- if logged_in? && @page && current_user.able_to_edit?(@page)
+ if is_current_user_able_to_edit_this_content?(@page)
render :partial => 'cms/pages/simple_container', :locals => {:name => name, :content => content}
else
content
@@ -99,7 +98,7 @@ def container(name)
# @param [Proc] block
# @return [Boolean] True if the container has one or more blocks, or if we are in edit mode. False otherwise.
def container_has_block?(name, &block)
- has_block = (@mode == "edit") || current_page.connectable_count_for_container(name) > 0
+ has_block = (edit_mode?) || current_page.connectable_count_for_container(name) > 0
logger.info "mode = #{@mode}, has_block = #{has_block}"
if block_given?
concat(capture(&block)) if has_block
@@ -146,9 +145,9 @@ def render_breadcrumbs(options={})
def render_portlet(name)
portlets = Portlet.all(:conditions => ["name = ?", name.to_s])
if portlets.size > 1
- @mode == "edit" ? "ERROR: Multiple Portlets with name '#{name}'" : nil
+ edit_mode? ? "ERROR: Multiple Portlets with name '#{name}'" : nil
elsif portlets.empty?
- @mode == "edit" ? "ERROR: No Portlet with name '#{name}'" : nil
+ edit_mode? ? "ERROR: No Portlet with name '#{name}'" : nil
else
render_connectable(portlets.first)
end
@@ -160,5 +159,9 @@ def able_to?(*perms, &block)
return ''
end
+ # Determines whether this page is in edit mode or not.
+ def edit_mode?()
+ @mode == "edit"
+ end
end
end
View
7 app/helpers/cms/rendering_helper.rb
@@ -19,7 +19,7 @@ module RenderingHelper
# @param [Symbol] method
# @param [Hash] options
def show(method, options={})
- if (!logged_in?) # Need to check the current user can edit the page attached to this block too
+ if (!is_current_user_able_to_edit_this_content?(@content_block)) # Need to check the current user can edit the page attached to this block too
value = @content_block.send(method)
value.respond_to?(:html_safe) ? value.html_safe : value
else
@@ -54,6 +54,11 @@ def is_editing_page?(page)
logged_in? && current_user.able_to_edit?(page)
end
+ # Determines if the current user can edit and is currently editing this content.
+ def is_current_user_able_to_edit_this_content?(content)
+ content && logged_in? && edit_mode? && current_user.able_to_edit?(content)
+ end
+
def render_connector_and_connectable(connector, connectable)
logger.debug "Rendering #{connectable} "
if is_editing_page?(connector.page)
View
1 app/views/cms/pages/_page_buttons.html.erb
@@ -12,6 +12,7 @@
:method => :put,
:target => "_top",
:enabled => !(!current_user.able_to?(:publish_content) || !current_user.able_to_edit?(@page) || @page.version != @page.draft.version || @page.live?) %>
+<%= menu_button "Preview", cms.preview_page_path(@page), id: "preview_button", pull: "left", target: "_blank" %>
<div class="btn-group pull-left">
<%= menu_button "Assign",
cms.new_page_task_path(@page),
View
4 app/views/cms/toolbar/_editor_toolbar.html.erb
@@ -1,6 +1,6 @@
<%
- message = @mode == "edit" ? "On" : "Off"
- button_style = @mode == "edit" ? "btn-success" : ""
+ message = edit_mode? ? "On" : "Off"
+ button_style = edit_mode? ? "btn-success" : ""
%>
<%= content_for :button_bar do %>
<% if @page.version == @page.draft.version %>
View
1 config/routes.rb
@@ -39,6 +39,7 @@
end
resources :tasks
end
+ get '/pages/:id/preview', to: 'content#preview', as: 'preview_page'
get '/pages/:id/version/:version', :to=>'pages#version', :as=>'version_cms_page'
put '/pages/:id/revert_to/:version', :to=>'pages#revert_to', :as=>'revert_page'
resources :tasks do
View
2 lib/cms/behaviors/rendering.rb
@@ -136,7 +136,7 @@ def perform_render(controller)
if self.respond_to?(:deleted) && self.deleted
logger.error "Attempting to render deleted object: #{self.inspect}"
- msg = (@mode == 'edit' ? %Q[<div class="error">This #{self.class.name} has been deleted. Please remove this container from the page</div>] : '')
+ msg = (edit_mode? ? %Q[<div class="error">This #{self.class.name} has been deleted. Please remove this container from the page</div>] : '')
return msg
end
View
3 task_ckeditor.md
@@ -12,6 +12,7 @@
* Reorder content - Can move content blocks up or down within a page. Page will refresh after moving.
* Edit Page titles - Page title can be edited directly from the header.
* New Template API Method: page_header(). Used for h1/h2 etc, this will output an editable page title element (for logging in users).
+* Preview Page - Editors can now preview the page without a toolbar or editing controls.
### Deprecations
@@ -20,7 +21,7 @@
### ToDo
* Handle editing other blocks (i.e. Products)
-* Add a 'Preview' button - Open window in new tab, no UI.
+-- [BUG] Inline (i.e. product.name) fields display have a <p> tag added by ckeditor, so they display as block elements.
* Link to files
* Link to images
* [BUG] Updating the page_header does not immediately update the <title> element.

0 comments on commit ba68de1

Please sign in to comment.