Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
added workflow to pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Adams committed Nov 23, 2008
1 parent 0aec1f7 commit 7c6f342
Show file tree
Hide file tree
Showing 43 changed files with 1,318 additions and 77 deletions.
25 changes: 23 additions & 2 deletions app/controllers/admin/pages_controller.rb
Expand Up @@ -26,12 +26,19 @@ def edit

def update
if @page.update_attributes(params[:page])
handle_publishing_workflow
flash.now[:message] = 'Page Updated Successfully'
@preview_url = @page.ancestor_path + @page.name
@page_id = @page.id
render :action => 'preview'
render :update do |page|
page << "notify('Page has been updated');"
page[:sidebar].update (render :partial => 'admin/pages/edit_sidebar')
page.call "ToggleContentBox.handle_toggle_content_boxes"
end
else
render :action => 'edit'
render :update do |page|
page << "notify('There was a problem updating the page.');"
end
end
end

Expand Down Expand Up @@ -129,4 +136,18 @@ def attach_page_plugins
@page.save
end
end

def handle_publishing_workflow
logger.info "============="
case params["_commit"]
when /Unpublish|Save as Draft/
@page.status = 'draft'
logger.info 'saving draft'
when 'Publish'
@page.publish!
when 'Submit for Review'
@page.submit_for_review!
end
@page.save
end
end
6 changes: 5 additions & 1 deletion app/controllers/page_controller.rb
Expand Up @@ -7,7 +7,11 @@ def indexer
@breadcrumb = @page.ancestors.reverse.to_a if @page
@top_page = params[:path][0] || 'root'
render :template => "404", :status => 404, :layout => false and return unless @page
render :template => 'content/page' and return if @page.page_type == "page"
if @page.is_published? || (logged_in? && current_user.can_view_drafts?)
render :template => 'content/page' and return if @page.page_type == "page"
else
render :template => 'content/unpublished' and return if @page.page_type == "page"
end
render :template => 'content/multipage'
end
end
6 changes: 4 additions & 2 deletions app/helpers/forms_view_helper.rb
@@ -1,10 +1,12 @@
module FormsViewHelper
def form_row the_label, the_field, options={}
lbl = content_tag("th", the_label)
label_options = options.delete(:label_options)
field_options = options.delete(:field_options)
lbl = content_tag("th", the_label, label_options)
if options[:note]
the_field << content_tag("div", options[:note], :class => 'note')
end
fld = content_tag("td", the_field)
fld = content_tag("td", the_field, field_options)
content_tag("tr", lbl + fld)
end

Expand Down
2 changes: 2 additions & 0 deletions app/helpers/page_admin_helper.rb
Expand Up @@ -92,6 +92,7 @@ def tree_table(acts_as_tree_set, init=true, level=0, &block)
ret << ' <thead>'
ret << ' <tr>'
ret << ' <th>Title</th>'
ret << ' <th>Status</th>'
ret << ' <th class=\'actions\'>Actions</th>'
ret << ' </tr>'
ret << ' </thead>'
Expand All @@ -108,6 +109,7 @@ def tree_table(acts_as_tree_set, init=true, level=0, &block)
def tree_row item, level=0, &block
ret = "<tr class='level-#{level} #{cycle("odd", "even")}'>"
ret << ' <td class="item">' + yield(item) + '</td>'
ret << ' <td>' + item.status.to_s + '</td>'
if current_user.can_post?
controls = render(:partial => 'page_controls', :locals => { :page => item })
else
Expand Down
11 changes: 11 additions & 0 deletions app/models/ansuz_mailer.rb
@@ -0,0 +1,11 @@
class AnsuzMailer < ActionMailer::Base
def page_review_notifications(page, sent_at = Time.now)
subject 'Ansuz#page_review_notifications'
recipients 'knewter@gmail.com'
from 'knewter@gmail.com'
sent_on sent_at

body :greeting => 'Hi,'
end

end
32 changes: 31 additions & 1 deletion app/models/page.rb
Expand Up @@ -20,6 +20,22 @@
#

class Page < ActiveRecord::Base
include AASM

aasm_column :status
aasm_initial_state :draft
aasm_state :reviewing, :enter => :inform_reviewers
aasm_state :published
aasm_state :draft

aasm_event :submit_for_review do
transitions :to => :reviewing, :from => [:draft]
end

aasm_event :publish do
transitions :to => :published, :from => [:reviewing, :draft]
end

acts_as_tree :order => 'page_order'
attr_protected :page_number, :pages

Expand All @@ -32,6 +48,12 @@ class Page < ActiveRecord::Base
# authorization plugin
acts_as_authorizable

protected
def inform_reviewers
AnsuzMailer.deliver_page_review_notifications(self)
end

public
def linked_children
children.select{|x| x.linked? }
end
Expand Down Expand Up @@ -74,7 +96,7 @@ def swap!(direction)
end

def publishable_children
self.children.find :all, :conditions => ["published = ? AND linked = ?", true, true]
self.children.find :all, :conditions => ["status=? AND linked = ?", 'published', true]
end

def ancestor_path
Expand Down Expand Up @@ -151,6 +173,14 @@ def to_s
name
end

def is_published?
status == 'published'
end

def is_draft?
status == 'draft'
end

protected
def check_page_type
self.page_type = "page" and return if self.body.blank? || (@split_pages = self.body.split(/\{pagebreak\}/i)).length == 1
Expand Down
2 changes: 2 additions & 0 deletions app/models/role.rb
Expand Up @@ -9,6 +9,8 @@ class Role < ActiveRecord::Base
validates_uniqueness_of :name, :scope => [:authorizable_id, :authorizable_type]

STATIC_ROLES = ["admin", "initial_reviewer", "final_reviewer", "author"]
PUBLISHING_ROLES = ["admin", "final_reviewer"]
DRAFT_VIEWING_ROLES = ["admin", "final_reviewer", "initial_reviewer", "author"]

named_scope :root, :conditions => "authorizable_type IS NULL and authorizable_id IS NULL"

Expand Down
30 changes: 19 additions & 11 deletions app/models/user.rb
Expand Up @@ -87,15 +87,23 @@ def to_s
display_name
end

protected
# before filter
def encrypt_password
return if password.blank?
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
self.crypted_password = encrypt(password)
end

def password_required?
crypted_password.blank? || !password.blank?
end
def can_publish?
Role::PUBLISHING_ROLES.detect{|r| has_role? r}
end

def can_view_drafts?
Role::DRAFT_VIEWING_ROLES.detect{|r| has_role? r}
end

protected
# before filter
def encrypt_password
return if password.blank?
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
self.crypted_password = encrypt(password)
end

def password_required?
crypted_password.blank? || !password.blank?
end
end
45 changes: 45 additions & 0 deletions app/views/admin/pages/_edit_sidebar.html.erb
@@ -0,0 +1,45 @@
<%= link_to "Manage Pages", admin_pages_path, :class => 'button icon back' %>
<%= link_to "Edit Metadata", edit_admin_page_page_metadata_path(@page), :class => 'button icon edit' %>
<br />
<% form_remote_tag :url => admin_page_path(@page),
:update => 'main',
:method => :put,
:complete => remote_function(
:update => "item#{@page.parent_id}",
:url => { :action => 'manage_tree', :parent_id => @page.parent_id, :tree_post_id => @post_id },
:complete => 'refresh_visibility()'),
:html => { 'name' => 'page_form' } do -%>
<%= render :partial => 'form' %>
<%
# TODO: Clean up this button spaghetti
%>
<% if @page.is_draft? %>
<%= submit_tag 'Save as Draft' %>
<% else %>
<%= submit_tag 'Update' %>
<% unless @page.is_published? %>
<%= submit_tag 'Save as Draft' %>
<% end %>
<% end %>
<% unless @page.is_published? %>
<%= submit_tag "Submit for Review" %>
<% end %>
<% if current_user.can_publish? %>
<%= submit_tag (@page.is_published? ? 'Unpublish' : 'Publish') %>
<% end %>
<% end -%>
<div class='note'>
None of the workflow buttons above do anything meaningful yet.
</div>

<h2>Plugins on this page</h2>
<ul id="page_plugins" class='page_plugins'>
<% @page.page_plugins.each do |plugin| -%>
<li>
<%= link_to(plugin.module_type, edit_admin_page_plugin_path(plugin), :class => 'edit_page_plugin') -%>
<%= link_to("[up]", move_up_admin_page_plugin_path(plugin)) %>
<%= link_to("[down]", move_down_admin_page_plugin_path(plugin)) %>
<%= link_to("[x]", admin_page_plugin_path(plugin), :confirm => 'Are you sure you want to remove this plugin from the page?', :method => :delete) -%>
</li>
<% end -%>
</ul>
4 changes: 1 addition & 3 deletions app/views/admin/pages/_form.html.erb
Expand Up @@ -5,9 +5,7 @@
<%= form_row "Name (URL)", text_field('page', 'name', 'onKeyUp' => "document.getElementById('name_preview').innerHTML = this.value.replace(/ /g, '_')") + "<span class='path'><b>#{@page.ancestor_path}<span id=name_preview>#{@page.name}</span></b></span><br/>" %>
<%= form_row "Title", text_field('page', 'title') %>
<%= form_row "Full Title", text_field('page', 'full_title') %>
<% if current_user.can_publish? %>
<%= form_row 'Published', '<select id="page_published" name="page[published]">' + options_for_select([true, false], @page.published) + '</select>' %>
<% end %>
<%= form_row "Status", @page.status, :field_options => { :id => 'status' } %>
</table>
<div class="advanced_options">
<% toggle_content_box "Advanced Options" do %>
Expand Down
27 changes: 1 addition & 26 deletions app/views/admin/pages/edit.html.erb
@@ -1,32 +1,7 @@
<%= title "Edit Page: #{@page}" -%>
<% content_for :sidebar do -%>
<%= link_to "Manage Pages", admin_pages_path, :class => 'button icon back' %>
<%= link_to "Edit Metadata", edit_admin_page_page_metadata_path(@page), :class => 'button icon edit' %>
<br />
<% form_remote_tag :url => admin_page_path(@page),
:update => 'main',
:method => :put,
:complete => remote_function(
:update => "item#{@page.parent_id}",
:url => { :action => 'manage_tree', :parent_id => @page.parent_id, :tree_post_id => @post_id },
:complete => 'refresh_visibility()'),
:html => { 'name' => 'page_form' } do -%>
<%= render :partial => 'form' %>
<%= submit_tag 'Update' %>
<% end -%>

<h2>Plugins on this page</h2>
<ul id="page_plugins" class='page_plugins'>
<% @page.page_plugins.each do |plugin| -%>
<li>
<%= link_to(plugin.module_type, edit_admin_page_plugin_path(plugin), :class => 'edit_page_plugin') -%>
<%= link_to("[up]", move_up_admin_page_plugin_path(plugin)) %>
<%= link_to("[down]", move_down_admin_page_plugin_path(plugin)) %>
<%= link_to("[x]", admin_page_plugin_path(plugin), :confirm => 'Are you sure you want to remove this plugin from the page?', :method => :delete) -%>
</li>
<% end -%>
</ul>
<%= render :partial => 'admin/pages/edit_sidebar' %>
<% end -%>
<% if @page.display_title %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/ansuz_mailer/page_review_notifications.erb
@@ -0,0 +1,3 @@
Ansuz#page_review_notifications

Find me in app/views/ansuz/page_review_notifications.erb
3 changes: 3 additions & 0 deletions app/views/content/unpublished.html.erb
@@ -0,0 +1,3 @@
<h2>This page is not currently published</h2>

There's a page here by that name, but it's not currently published. Sorry.
14 changes: 0 additions & 14 deletions app/views/page/_top_nav.html.erb
Expand Up @@ -3,19 +3,5 @@
<ul id='top-nav'>
<%= render :partial => 'shared/menu' -%>
</ul>
<% if false -%>
<!--
<ul id='top-nav2'>
<% @topNav.each do |page|
isActive = @breadcrumb && (page == @breadcrumb[1] || page == @page)
url = "/pages/" + page.name + "/" %>
<li <%= "class=\"active\"" if isActive -%>>
<a href='<%= url %>'><%= page.title -%></a>
</li>
<% end %>
</ul>
-->
<% end -%>
<%= render :partial => "/admin/page_plugins/plugins_nav" -%>
<div class='clear'></div>
</div>
1 change: 1 addition & 0 deletions config/environment.rb
Expand Up @@ -45,6 +45,7 @@
:source => 'http://gems.github.com'

config.gem "RedCloth", :lib => "redcloth", :source => "http://code.whytheluckystiff.net"
config.gem 'rubyist-aasm', :version => '~> 2.0.2', :lib => 'aasm', :source => "http://gems.github.com"

# Only load the plugins named here, in the order given. By default, all plugins
# in vendor/plugins are loaded in alphabetical order.
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/dependencies.rb
Expand Up @@ -2,3 +2,6 @@

# various custom extensions we've grown to use
require 'isotope11'

# fix multiple submit buttons in ajax calls
require 'tufty/button_disambiguation'
11 changes: 11 additions & 0 deletions db/migrate/20081123161548_add_status_to_pages.rb
@@ -0,0 +1,11 @@
class AddStatusToPages < ActiveRecord::Migration
def self.up
add_column :pages, :status, :string
add_index :pages, :status
end

def self.down
remove_index :pages, :column => :status
remove_column :pages, :status
end
end

0 comments on commit 7c6f342

Please sign in to comment.