Skip to content

Commit

Permalink
Changed versioning to store live version in main table
Browse files Browse the repository at this point in the history
  • Loading branch information
pjb3 committed May 11, 2009
1 parent 3a28da7 commit bbe6e4d
Show file tree
Hide file tree
Showing 33 changed files with 540 additions and 494 deletions.
14 changes: 10 additions & 4 deletions app/controllers/cms/content_block_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def index
end

def show
load_block
load_block_draft
render "#{template_directory}/show"
end

Expand All @@ -37,7 +37,7 @@ def create
end

def edit
load_block
load_block_draft
render "#{template_directory}/edit"
end

Expand Down Expand Up @@ -90,7 +90,7 @@ def versions
end

def usages
load_block
load_block_draft
@pages = @block.connected_pages.all(:order => 'name')
render "#{template_directory}/usages"
end
Expand Down Expand Up @@ -133,6 +133,11 @@ def load_block
@block = model_class.find(params[:id])
end

def load_block_draft
load_block
@block = @block.as_of_draft_version if model_class.versioned?
end

# path related methods - available in the view as helpers

def new_block_path(options={})
Expand Down Expand Up @@ -173,7 +178,8 @@ def create_block
end

def after_create_on_success
flash[:notice] = "#{content_type.display_name} '#{@block.name}' was created"
block = @block.class.versioned? ? @block.draft : @block
flash[:notice] = "#{content_type.display_name} '#{block.name}' was created"
if @block.class.connectable? && @block.connected_page
redirect_to @block.connected_page.path
else
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/cms/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def page_versions(page)
["v#{r.version}: #{r.version_comment} by #{r.updated_by.login} at #{time_on_date(r.updated_at)}", r.version]
}, page.version),
:onchange => 'this.form.submit(); return false')
text << javascript_tag("$('version').selectedIndex = 0") if page.current_version?
text << javascript_tag("$('version').selectedIndex = 0") if page.live?
text
end

Expand Down
14 changes: 7 additions & 7 deletions app/helpers/cms/menu_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,24 +140,24 @@ def render_menu(options={})
elsif i == nodes.size-1
classes << "last"
end
classes << "open" if ancestors.include?(sn.live_node)
classes << "on" if page == sn.live_node
classes << "open" if ancestors.include?(sn.node)
classes << "on" if page == sn.node
cls = classes.empty? ? nil : classes.join(" ")

html << %Q{<li id="#{sn.node_type.underscore}_#{sn.live_node.id}"#{cls ? " class=\"#{cls}\"" : ''}>\n}.indent(indent+4)
html << %Q{<li id="#{sn.node_type.underscore}_#{sn.node.id}"#{cls ? " class=\"#{cls}\"" : ''}>\n}.indent(indent+4)

#Figure out what this link for this node should be
#If it is a page, then the page will simply be used
#But if is a page, we call the first_page_or_link method
p = sn.node_type == "Section" ? sn.live_node.first_page_or_link : sn.live_node
html << %Q{<a href="#{p ? p.path : '#'}"#{(p.respond_to?(:new_window) && p.new_window?) ? ' target="_blank"' : ''}>#{sn.live_node.name}</a>\n}.indent(indent+6)
p = sn.node_type == "Section" ? sn.node.first_page_or_link : sn.node
html << %Q{<a href="#{p ? p.path : '#'}"#{(p.respond_to?(:new_window) && p.new_window?) ? ' target="_blank"' : ''}>#{sn.node.name}</a>\n}.indent(indent+6)

#Now if this is a section, we do the child nodes,
#but only if the show_all_siblings parameter is true,
#or if this section is one of the current page's ancestors
#and also if the current depth is less than the target depth
if sn.node_type == "Section" && (show_all_siblings || ancestors.include?(sn.live_node)) && d < depth
fn.call(sn.live_node.visible_child_nodes, d+1)
if sn.node_type == "Section" && (show_all_siblings || ancestors.include?(sn.node)) && d < depth
fn.call(sn.node.visible_child_nodes, d+1)
end

html << %Q{</li>\n}.indent(indent+4)
Expand Down
30 changes: 8 additions & 22 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ class Attachment < ActiveRecord::Base

#----- Macros ----------------------------------------------------------------

is_archivable
is_publishable
uses_soft_delete
is_userstamped
is_versioned
attr_accessor :temp_file

#----- Callbacks -------------------------------------------------------------

before_validation :make_dirty_if_temp_file
before_validation :prepend_file_path_with_slash
before_validation :extract_file_extension_from_file_name
before_validation :extract_file_type_from_temp_file
Expand Down Expand Up @@ -61,6 +64,10 @@ def section=(section)

#----- Callbacks Methods -----------------------------------------------------

def make_dirty_if_temp_file
dirty! if temp_file
end

def prepend_file_path_with_slash
unless file_path.blank?
self.file_path = "/#{file_path}" unless file_path =~ /^\//
Expand Down Expand Up @@ -137,13 +144,7 @@ def self.storage_location=(storage_location)
end

def self.find_live_by_file_path(file_path)
if attachment_version = Attachment::Version.first(:conditions => {
:file_path => file_path,
:published => true},
:order => "version desc")
attachment = attachment_version.attachment.as_of_version(attachment_version.version)
(attachment.live_version? && !attachment.archived?) ? attachment : nil
end
Attachment.published.not_archived.first(:conditions => {:file_path => file_path})
end

#----- Instance Methods ------------------------------------------------------
Expand All @@ -156,21 +157,6 @@ def name
file_name
end

def live?
versionable? ? versions.count(:conditions => ['version > ? AND published = ?', version, true]) == 0 && published? : true
end

def live_version
if archived? || deleted?
nil
elsif published?
self
else
live_version = versions.first(:conditions => {:published => true}, :order => "version desc, id desc")
live_version ? as_of_version(live_version.version) : nil
end
end

def icon
{
:doc => %w[doc],
Expand Down
117 changes: 49 additions & 68 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,68 +50,71 @@ class Page < ActiveRecord::Base
before_destroy :delete_connectors

validates_presence_of :name, :path
validate :path_unique?
validates_uniqueness_of :path
validate :path_not_reserved

def after_build_new_version(new_version)
copy_connectors
copy_connectors(
:from_version_number => @copy_connectors_from_version || (new_version.version - 1),
:to_version_number => new_version.version
)
@copy_connectors_from_version = nil
true
end
end

def copy_connectors
copy_from_version = @copy_connectors_from_version ? @copy_connectors_from_version : version - 1

connectors.for_page_version(copy_from_version).all(:order => "connectors.container, connectors.position").each do |c|
if c.connectable #The connector won't have a connectable if it has been deleted
# Publish all
def after_publish
self.reload # Get's the correct version number loaded
self.connectors.for_page_version(self.version).all(:order => "position").each do |c|
if c.connectable_type.constantize.publishable? && con = c.connectable
con.publish
end
end
end

def copy_connectors(options={})
connectors.for_page_version(options[:from_version_number]).all(:order => "connectors.container, connectors.position").each do |c|
# The connector won't have a connectable if it has been deleted
# Also need to see if the draft has been deleted,
# in which case we are in the process of deleting it
if c.connectable && !c.connectable.draft.deleted?
connectable = c.connectable_type.constantize.versioned? ? c.connectable.as_of_version(c.connectable_version) : c.connectable

#If are publishing this page, We need to publish the other page if it is not already published
if published? && c.connectable_type.constantize.publishable? && !c.connectable.published?
connectable.publish_by_page!(self)
end

#If we are copying connectors from a previous version, that means we are reverting this page,
#in which case we should create a new version of the block, and connect this page to that block
if @copy_connectors_from_version && connectable.class.versioned? && !connectable.current_version?
if @copy_connectors_from_version && connectable.class.versioned? && (connectable.version != connectable.draft.version)
connectable = connectable.class.find(connectable.id)
connectable.published_by_page = self if connectable.class.publishable?
connectable.revert_to(c.connectable_version)
end
end

connectors.build(
:page_version => version,
new_connector = connectors.build(
:page_version => options[:to_version_number],
:connectable => connectable,
:connectable_version => connectable.class.versioned? ? connectable.version : nil,
:container => c.container,
:position => c.position
)
end
end

@copy_connectors_from_version = nil
true
end

def create_connector(connectable, container)
transaction do
raise "Connectable is nil" unless connectable
raise "Container is required" if container.blank?
update_attributes(:version_comment => "#{connectable} was added to the '#{container}' container",
:publish_on_save => (published? && connectable.connected_page &&
update_attributes(
:version_comment => "#{connectable} was added to the '#{container}' container",
:publish_on_save => (
published? &&
connectable.connected_page &&
(connectable.class.publishable? ? connectable.published? : true)))
Connector.create(
:page => self,
:page_version => version,
connectors.create(
:page_version => draft.version,
:connectable => connectable,
:connectable_version => connectable.class.versioned? ? connectable.version : nil,
:container => container)
end
end

%w(up down to_top to_bottom).each do |d|
define_method("move_connector_#{d}") do |connector|
move_connector(connector, d)
end
end

def move_connector(connector, direction)
Expand All @@ -120,17 +123,23 @@ def move_connector(connector, direction)
raise "Direction is nil" unless direction
orientation = direction[/_/] ? "#{direction.sub('_', ' the ')} of" : "#{direction} within"
update_attributes(:version_comment => "#{connector.connectable} was moved #{orientation} the '#{connector.container}' container")
connectors.for_page_version(version).like(connector).first.send("move_#{direction}")
connectors.for_page_version(draft.version).like(connector).first.send("move_#{direction}")
end
end
end

%w(up down to_top to_bottom).each do |d|
define_method("move_connector_#{d}") do |connector|
move_connector(connector, d)
end
end

def remove_connector(connector)
transaction do
raise "Connector is nil" unless connector
update_attributes(:version_comment => "#{connector.connectable} was removed from the '#{connector.container}' container")

#The logic of this is to go ahead and let the container get copied forward, then delete the new connector
if new_connector = connectors.for_page_version(version).like(connector).first
if new_connector = connectors.for_page_version(draft.version).like(connector).first
new_connector.destroy
else
raise "Error occurred while trying to remove connector #{connector.id}"
Expand All @@ -144,10 +153,9 @@ def delete_connectors

#This is done to let copy_connectors know which version to pull from
#copy_connectors will get called later as an after_update callback
alias_method :original_revert_to, :revert_to
def revert_to(version)
@copy_connectors_from_version = version
original_revert_to(version)
super(version)
end

def file_size
Expand Down Expand Up @@ -181,13 +189,7 @@ def public?
def page_title
title.blank? ? name : title
end

%w(up down to_top to_bottom).each do |d|
define_method("move_connector_#{d}") do |connector|
move_connector(connector, d)
end
end


def append_leading_slash_to_path
if path.blank?
self.path = "/"
Expand All @@ -196,17 +198,6 @@ def append_leading_slash_to_path
end
end

def path_unique?
conditions = ["path = ?", path]
unless new_record?
conditions.first << " and id != ?"
conditions << id
end
if self.class.count(:conditions => conditions) > 0
errors.add(:path, "must be unique")
end
end

def path_not_reserved
if Cms.reserved_paths.include?(path)
errors.add(:path, "is invalid, '#{path}' a reserved path")
Expand Down Expand Up @@ -240,8 +231,8 @@ def in_section?(section_or_section_name)

#Returns true if the block attached to each connector in the given container are published
def container_published?(container)
connectors.for_page_version(version).in_container(container.to_s).all? do |c|
c.connectable_type.constantize.publishable? ? c.connectable.published? : true
connectors.for_page_version(draft.version).in_container(container.to_s).all? do |c|
c.connectable_type.constantize.publishable? ? c.connectable.live? : true
end
end

Expand All @@ -251,17 +242,7 @@ def connectable_count_for_container(container)
end

def self.find_live_by_path(path)
page_versions = Page::Version.all(:conditions => {
:path => path,
:archived => false,
:published => true},
:order => "version desc")
for page_version in page_versions
next if page_version.page.nil? #skip versions that belong to deleted pages
page = page_version.page.as_of_version(page_version.version)
return page.live_version? ? page : nil
end
nil
published.not_archived.first(:conditions => {:path => path})
end

def name_with_section_path
Expand Down
16 changes: 4 additions & 12 deletions app/models/section_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@ class SectionNode < ActiveRecord::Base
named_scope :of_type, lambda{|types| {:conditions => ["section_nodes.node_type IN (?)", types]}}

def visible?
return false unless live_node
return false if(live_node.respond_to?(:hidden?) && live_node.hidden?)
return false if(live_node.respond_to?(:archived?) && live_node.archived?)
return false if(live_node.respond_to?(:published?) && !live_node.published?)
return false unless node
return false if(node.respond_to?(:hidden?) && node.hidden?)
return false if(node.respond_to?(:archived?) && node.archived?)
return false if(node.respond_to?(:published?) && !node.published?)
true
end

def live_node
@live_node ||= if node
node.class.versioned? ? node.live_version : node
else
nil
end
end

def orphaned?
!node || (node.class.uses_soft_delete? && node.deleted?)
end
Expand Down
3 changes: 2 additions & 1 deletion app/views/cms/blocks/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
col_ct += 1 if content_type.model_class.respond_to?(:updated_at)
col_ct += 1 if content_type.model_class.connectable?
col_ct += 1 if content_type.model_class.publishable? %>
<% @blocks.each do |block| %>
<% @blocks.each do |b| %>
<% block = b.as_of_draft_version %>
<tr id="<%= block.class.name.underscore %>_<%= block.id %>" class="<%= block.class.name.underscore %> <%= block.class.publishable? && !block.published? ? 'draft' : 'published' %>">
<td class="first"></td>
<% content_type.columns_for_index.each_with_index do |column, i| %>
Expand Down
Loading

0 comments on commit bbe6e4d

Please sign in to comment.