lib/spontaneous/plugins/publishing.rb in spontaneous-0.1.0.alpha1 vs lib/spontaneous/plugins/publishing.rb in spontaneous-0.2.0.alpha1

- old
+ new

@@ -1,25 +1,11 @@ # encoding: UTF-8 -# is this unforgivable? -# i think it's kinda neat, if a tad fragile (to columns named 'content'...) -module Sequel - class Dataset - alias_method :sequel_quote_identifier, :quote_identifier - - def quote_identifier(name) - if name == :content or name == "content" - name = Spontaneous::Content.current_revision_table - end - sequel_quote_identifier(name) - end - end -end - module Spontaneous::Plugins module Publishing + extend ActiveSupport::Concern module ClassMethods @@dataset = nil @@revision = nil @@ -51,39 +37,27 @@ def revision @@revision end - def reset_revision - @@revision, @@dataset = revision_stack.first - revision_stack.clear + def with_revision(revision=nil, &block) + saved_revision = @@revision + @@revision = revision + self.with_table(revision_table(revision), &block) + ensure + @@revision = saved_revision end - def with_revision(revision=nil) - revision_push(revision) - begin - yield - ensure - revision_pop - end if block_given? - end - def with_editable(&block) with_revision(nil, &block) end def with_published(&block) revision = Spontaneous::Site.published_revision with_revision(revision, &block) end - def revision_push(revision) - revision_stack.push([@@revision, (@@dataset || self.dataset)]) - @@dataset = revision_dataset(revision) - @@revision = revision - end - def database Spontaneous.database end ## @@ -106,11 +80,11 @@ with_editable do first_published = self.filter(:first_published_at => nil) published = self.filter must_publish_all = (content.nil? || (!revision_exists?(revision-1)) || \ - (content.is_a?(Array) && content.empty?)) + (content.is_a?(Array) && content.empty?)) if must_publish_all create_revision(revision) else content = content.map do |c| @@ -171,85 +145,67 @@ def delete_all_revisions! database.tables.each do |table| database.drop_table(table) if revision_table?(table) end end + end # ClassMethods - def revision_pop - @@revision, @@dataset = revision_stack.pop - end + # InstanceMethods - def revision_stack - @revision_stack ||= [] - end + def after_update + super + page.modified!(page?) if page + end - def revision_dataset(revision=nil) - Spontaneous.database.dataset.from(revision_table(revision)) + def modified!(caller_is_page) + unless caller_is_page + self.model.where(:id => self.id).update(:modified_at => Sequel.datetime_class.now) end + push_page_change end - module InstanceMethods - def after_update - super - page.modified!(page?) if page - end + def push_page_change + Spontaneous::Change.push(self) if page? + end - # def after_create - # super - # push_page_change - # end - def modified!(caller_is_page) - unless caller_is_page - self.model.where(:id => self.id).update(:modified_at => Sequel.datetime_class.now) - end - push_page_change - end + def with_revision(revision, &block) + self.class.with_revision(revision, &block) + end + def with_editable(&block) + self.class.with_editable(&block) + end - def push_page_change - Spontaneous::Change.push(self) if page? - end + def sync_to_revision(revision, origin=true) + # 'publish' is a lock to make sure the duplication doesn't cross + # page boundaries unless that's necessary (such as in the case + # of a page addition) + publish = origin || !self.page? - - def with_revision(revision, &block) - self.class.with_revision(revision, &block) - end - def with_editable(&block) - self.class.with_editable(&block) - end - - def sync_to_revision(revision, origin=true) - # 'publish' is a lock to make sure the duplication doesn't cross - # page boundaries unless that's necessary (such as in the case - # of a page addition) - publish = origin || !self.page? - - with_revision(revision) do - published_copy = Spontaneous::Content[self.id] - if published_copy - if publish and published_copy.entry_store - pieces_to_delete = published_copy.entry_store - self.entry_store - pieces_to_delete.each do |entry| - if c = Spontaneous::Content[entry[0]] - c.destroy(false) - end + with_revision(revision) do + published_copy = Spontaneous::Content[self.id] + if published_copy + if publish and published_copy.entry_store + pieces_to_delete = published_copy.entry_store - self.entry_store + pieces_to_delete.each do |entry| + if c = Spontaneous::Content[entry[0]] + c.destroy(false) end end - else # missing content (so force a publish) - Spontaneous::Content.insert({:id => self.id}) - publish = true end + else # missing content (so force a publish) + Spontaneous::Content.insert({:id => self.id}) + publish = true + end - if publish - with_editable do - self.pieces.each do |entry| - entry.sync_to_revision(revision, false) - end + if publish + with_editable do + self.pieces.each do |entry| + entry.sync_to_revision(revision, false) end - Spontaneous::Content.where(:id => self.id).update(self.values) end + Spontaneous::Content.where(:id => self.id).update(self.values) end end end end end -