Skip to content

Commit

Permalink
[#566] Preview Page
Browse files Browse the repository at this point in the history
Add a 'Preview' page button that will open the window in a new tab with no frame.
  • Loading branch information
peakpg committed Mar 19, 2013
1 parent d0e9f35 commit ba68de1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 23 deletions.
29 changes: 16 additions & 13 deletions app/controllers/cms/content_controller.rb
Expand Up @@ -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


Expand All @@ -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
Expand All @@ -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"]
Expand Down Expand Up @@ -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})
Expand All @@ -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
13 changes: 8 additions & 5 deletions app/helpers/cms/page_helper.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
7 changes: 6 additions & 1 deletion app/helpers/cms/rendering_helper.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions app/views/cms/pages/_page_buttons.html.erb
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions 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 %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/cms/behaviors/rendering.rb
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion task_ckeditor.md
Expand Up @@ -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

Expand All @@ -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.
Expand Down

0 comments on commit ba68de1

Please sign in to comment.