Permalink
Browse files

* Adding an initial hook to handle reverting a block, plus its associ…

…ated block.
  • Loading branch information...
1 parent 672ee43 commit 39aae700bb0436d9328b238d70b9f50772065b7d @peakpg peakpg committed Apr 9, 2012
@@ -170,12 +170,6 @@ def content_type
alias :file_type :content_type
- # Paperclip Callback - Normally this deletes old files whenever they are replaced.
- # We override this here to keep all versions on the server, forever. Since each version of the file needs to live
- #def destroy_attached_files
- # Rails.logger.warn "Called@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
- # true
- #end
protected
@@ -234,6 +234,19 @@ def after_as_of_version()
end
end
+ # Callback - Ensure attachments get reverted whenver a block does.
+ def after_revert(version)
+ version_number = version.version
+ logger.warn "Calling after_revert #{version}"
+ attachments_for_version = self.class.attachments_as_of_version(version_number, self)
+ new_attachments = []
+ attachments_for_version.each do |a|
+ a.revert_to(version_number)
+ new_attachments << a
+ end
+ self.attachments = new_attachments
+ end
+
private
# Saves associated attachments if they were updated. (Used in place of :autosave=>true, since the CMS Versioning API seems to break that)
@@ -76,7 +76,7 @@ def is_versioned(options={})
before_save :build_new_version
attr_accessor :skip_callbacks
- attr_accessor :revert_to_version
+ #attr_accessor :revert_to_version
#Define the version class
const_set("Version", Class.new(ActiveRecord::Base)).class_eval do
@@ -296,11 +296,14 @@ def revert
def revert_to_without_save(version)
raise "Version parameter missing" if version.blank?
- self.revert_to_version = find_version(version)
+ revert_to_version = find_version(version)
raise "Could not find version #{version}" unless revert_to_version
+ self.before_revert(revert_to_version) if self.respond_to?(:before_revert)
+
(self.class.versioned_columns - ["version"]).each do |a|
send("#{a}=", revert_to_version.send(a))
end
+ self.after_revert(revert_to_version) if self.respond_to?(:after_revert)
self.version_comment = "Reverted to version #{version}"
self
end
@@ -326,37 +329,7 @@ def different_from_last_draft?
(self.class.versioned_columns - %w( version )).each do |col|
return true if self.send(col) != last_draft.send(col)
end
- return false
- end
-
- private
-
- # Given a ::Version object of a given type, create an original object from its attributes.
- #
- # @param [Class#name::Version] version_of_object (i.e. HtmlBlock::Version)
- # @return [Class#name] i.e. HtmlBlock
- def build_object_from_version(version_of_object)
- obj = self.class.new
-
- (self.class.versioned_columns + [:version, :created_at, :created_by_id, :updated_at, :updated_by_id]).each do |a|
- obj.send("#{a}=", version_of_object.send(a))
- end
- obj.id = id
- obj.lock_version = lock_version
-
- # Need to do this so associations can be loaded
- obj.instance_variable_set("@persisted", true)
- obj.instance_variable_set("@new_record", false)
-
- # Callback to allow us to load other data when an older version is loaded
- obj.after_as_of_version if obj.respond_to?(:after_as_of_version)
-
- # Last but not least, clear the changed attributes
- if changed_attrs = obj.send(:changed_attributes)
- changed_attrs.clear
- end
-
- obj
+ false
end
end
end
@@ -48,6 +48,8 @@ def setup
@attachable.publish!
@attachable.reload
+ log_table Cms::Attachment
+ log_table Cms::Attachment::Version
assert_equal 3, @attachable.version
assert_equal "/version1.jpg", @attachable.attachments[0].data_file_path

0 comments on commit 39aae70

Please sign in to comment.