Commit d1415e1f1c458312bbb8ec58a30b6a3189d138c0
1 parent
4ff37093
Exists in
staging
and in
42 other branches
ActionItem93: adding acts_as_versioned (comatose had a copy of it)
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@935 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
18 changed files
with
995 additions
and
1 deletions
Show diff stats
config/environment.rb
| ... | ... | @@ -72,8 +72,9 @@ Localist.supported_locales = %w[en-US pt-BR] |
| 72 | 72 | Localist.default_locale = "pt-BR" |
| 73 | 73 | Localist.callback = lambda { |l| GetText.locale= l } |
| 74 | 74 | |
| 75 | - | |
| 76 | 75 | Tag.hierarchical = true |
| 77 | 76 | |
| 78 | 77 | # string transliteration |
| 79 | 78 | require 'noosfero/transliterations' |
| 79 | + | |
| 80 | +require 'acts_as_versioned' | ... | ... |
| ... | ... | @@ -0,0 +1,39 @@ |
| 1 | +*0.2.3* | |
| 2 | + | |
| 3 | +* (12 Nov 2005) fixed bug with old behavior of #blank? [Michael Schuerig] | |
| 4 | +* (12 Nov 2005) updated tests to use ActiveRecord Schema | |
| 5 | + | |
| 6 | +*0.2.2* | |
| 7 | + | |
| 8 | +* (3 Nov 2005) added documentation note to #acts_as_versioned [Martin Jul] | |
| 9 | + | |
| 10 | +*0.2.1* | |
| 11 | + | |
| 12 | +* (6 Oct 2005) renamed dirty? to changed? to keep it uniform. it was aliased to keep it backwards compatible. | |
| 13 | + | |
| 14 | +*0.2* | |
| 15 | + | |
| 16 | +* (6 Oct 2005) added find_versions and find_version class methods. | |
| 17 | + | |
| 18 | +* (6 Oct 2005) removed transaction from create_versioned_table(). | |
| 19 | + this way you can specify your own transaction around a group of operations. | |
| 20 | + | |
| 21 | +* (30 Sep 2005) fixed bug where find_versions() would order by 'version' twice. (found by Joe Clark) | |
| 22 | + | |
| 23 | +* (26 Sep 2005) added :sequence_name option to acts_as_versioned to set the sequence name on the versioned model | |
| 24 | + | |
| 25 | +*0.1.3* (18 Sep 2005) | |
| 26 | + | |
| 27 | +* First RubyForge release | |
| 28 | + | |
| 29 | +*0.1.2* | |
| 30 | + | |
| 31 | +* check if module is already included when acts_as_versioned is called | |
| 32 | + | |
| 33 | +*0.1.1* | |
| 34 | + | |
| 35 | +* Adding tests and rdocs | |
| 36 | + | |
| 37 | +*0.1* | |
| 38 | + | |
| 39 | +* Initial transfer from Rails ticket: http://dev.rubyonrails.com/ticket/1974 | |
| 0 | 40 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,20 @@ |
| 1 | +Copyright (c) 2005 Rick Olson | |
| 2 | + | |
| 3 | +Permission is hereby granted, free of charge, to any person obtaining | |
| 4 | +a copy of this software and associated documentation files (the | |
| 5 | +"Software"), to deal in the Software without restriction, including | |
| 6 | +without limitation the rights to use, copy, modify, merge, publish, | |
| 7 | +distribute, sublicense, and/or sell copies of the Software, and to | |
| 8 | +permit persons to whom the Software is furnished to do so, subject to | |
| 9 | +the following conditions: | |
| 10 | + | |
| 11 | +The above copyright notice and this permission notice shall be | |
| 12 | +included in all copies or substantial portions of the Software. | |
| 13 | + | |
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| 15 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| 16 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| 17 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| 18 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| 19 | +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| 20 | +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 0 | 21 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,28 @@ |
| 1 | += acts_as_versioned | |
| 2 | + | |
| 3 | +This library adds simple versioning to an ActiveRecord module. ActiveRecord is required. | |
| 4 | + | |
| 5 | +== Resources | |
| 6 | + | |
| 7 | +Install | |
| 8 | + | |
| 9 | +* gem install acts_as_versioned | |
| 10 | + | |
| 11 | +Rubyforge project | |
| 12 | + | |
| 13 | +* http://rubyforge.org/projects/ar-versioned | |
| 14 | + | |
| 15 | +RDocs | |
| 16 | + | |
| 17 | +* http://ar-versioned.rubyforge.org | |
| 18 | + | |
| 19 | +Subversion | |
| 20 | + | |
| 21 | +* http://techno-weenie.net/svn/projects/acts_as_versioned | |
| 22 | + | |
| 23 | +Collaboa | |
| 24 | + | |
| 25 | +* http://collaboa.techno-weenie.net/repository/browse/acts_as_versioned | |
| 26 | + | |
| 27 | +Special thanks to Dreamer on ##rubyonrails for help in early testing. His ServerSideWiki (http://serversidewiki.com) | |
| 28 | +was the first project to use acts_as_versioned <em>in the wild</em>. | |
| 0 | 29 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,41 @@ |
| 1 | +== Creating the test database | |
| 2 | + | |
| 3 | +The default name for the test databases is "activerecord_versioned". If you | |
| 4 | +want to use another database name then be sure to update the connection | |
| 5 | +adapter setups you want to test with in test/connections/<your database>/connection.rb. | |
| 6 | +When you have the database online, you can import the fixture tables with | |
| 7 | +the test/fixtures/db_definitions/*.sql files. | |
| 8 | + | |
| 9 | +Make sure that you create database objects with the same user that you specified in i | |
| 10 | +connection.rb otherwise (on Postgres, at least) tests for default values will fail. | |
| 11 | + | |
| 12 | +== Running with Rake | |
| 13 | + | |
| 14 | +The easiest way to run the unit tests is through Rake. The default task runs | |
| 15 | +the entire test suite for all the adapters. You can also run the suite on just | |
| 16 | +one adapter by using the tasks test_mysql_ruby, test_ruby_mysql, test_sqlite, | |
| 17 | +or test_postresql. For more information, checkout the full array of rake tasks with "rake -T" | |
| 18 | + | |
| 19 | +Rake can be found at http://rake.rubyforge.org | |
| 20 | + | |
| 21 | +== Running by hand | |
| 22 | + | |
| 23 | +Unit tests are located in test directory. If you only want to run a single test suite, | |
| 24 | +or don't want to bother with Rake, you can do so with something like: | |
| 25 | + | |
| 26 | + cd test; ruby -I "connections/native_mysql" base_test.rb | |
| 27 | + | |
| 28 | +That'll run the base suite using the MySQL-Ruby adapter. Change the adapter | |
| 29 | +and test suite name as needed. | |
| 30 | + | |
| 31 | +== Faster tests | |
| 32 | + | |
| 33 | +If you are using a database that supports transactions, you can set the | |
| 34 | +"AR_TX_FIXTURES" environment variable to "yes" to use transactional fixtures. | |
| 35 | +This gives a very large speed boost. With rake: | |
| 36 | + | |
| 37 | + rake AR_TX_FIXTURES=yes | |
| 38 | + | |
| 39 | +Or, by hand: | |
| 40 | + | |
| 41 | + AR_TX_FIXTURES=yes ruby -I connections/native_sqlite3 base_test.rb | ... | ... |
vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb
0 → 100644
| ... | ... | @@ -0,0 +1,397 @@ |
| 1 | +# Copyright (c) 2005 Rick Olson | |
| 2 | +# | |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining | |
| 4 | +# a copy of this software and associated documentation files (the | |
| 5 | +# "Software"), to deal in the Software without restriction, including | |
| 6 | +# without limitation the rights to use, copy, modify, merge, publish, | |
| 7 | +# distribute, sublicense, and/or sell copies of the Software, and to | |
| 8 | +# permit persons to whom the Software is furnished to do so, subject to | |
| 9 | +# the following conditions: | |
| 10 | +# | |
| 11 | +# The above copyright notice and this permission notice shall be | |
| 12 | +# included in all copies or substantial portions of the Software. | |
| 13 | +# | |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| 15 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| 16 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| 17 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
| 18 | +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
| 19 | +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
| 20 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| 21 | + | |
| 22 | +module ActiveRecord #:nodoc: | |
| 23 | + module Acts #:nodoc: | |
| 24 | + # Specify this act if you want to save a copy of the row in a versioned table. This assumes there is a | |
| 25 | + # versioned table ready and that your model has a version field. This works with optimisic locking if the lock_version | |
| 26 | + # column is present as well. | |
| 27 | + # | |
| 28 | + # The class for the versioned model is derived the first time it is seen. Therefore, if you change your database schema you have to restart | |
| 29 | + # your container for the changes to be reflected. In development mode this usually means restarting WEBrick. | |
| 30 | + # | |
| 31 | + # class Page < ActiveRecord::Base | |
| 32 | + # # assumes pages_versions table | |
| 33 | + # acts_as_versioned | |
| 34 | + # end | |
| 35 | + # | |
| 36 | + # Example: | |
| 37 | + # | |
| 38 | + # page = Page.create(:title => 'hello world!') | |
| 39 | + # page.version # => 1 | |
| 40 | + # | |
| 41 | + # page.title = 'hello world' | |
| 42 | + # page.save | |
| 43 | + # page.version # => 2 | |
| 44 | + # page.versions.size # => 2 | |
| 45 | + # | |
| 46 | + # page.revert_to(1) # using version number | |
| 47 | + # page.title # => 'hello world!' | |
| 48 | + # | |
| 49 | + # page.revert_to(page.versions.last) # using versioned instance | |
| 50 | + # page.title # => 'hello world' | |
| 51 | + # | |
| 52 | + # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options | |
| 53 | + module Versioned | |
| 54 | + def self.included(base) # :nodoc: | |
| 55 | + base.extend ClassMethods | |
| 56 | + end | |
| 57 | + | |
| 58 | + module ClassMethods | |
| 59 | + # == Configuration options | |
| 60 | + # | |
| 61 | + # * <tt>class_name</tt> - versioned model class name (default: PageVersion in the above example) | |
| 62 | + # * <tt>table_name</tt> - versioned model table name (default: page_versions in the above example) | |
| 63 | + # * <tt>foreign_key</tt> - foreign key used to relate the versioned model to the original model (default: page_id in the above example) | |
| 64 | + # * <tt>inheritance_column</tt> - name of the column to save the model's inheritance_column value for STI. (default: versioned_type) | |
| 65 | + # * <tt>version_column</tt> - name of the column in the model that keeps the version number (default: version) | |
| 66 | + # * <tt>sequence_name</tt> - name of the custom sequence to be used by the versioned model. | |
| 67 | + # * <tt>limit</tt> - number of revisions to keep, defaults to unlimited | |
| 68 | + # * <tt>if</tt> - symbol of method to check before saving a new version. If this method returns false, a new version is not saved. | |
| 69 | + # For finer control, pass either a Proc or modify Model#version_condition_met? | |
| 70 | + # | |
| 71 | + # acts_as_versioned :if => Proc.new { |auction| !auction.expired? } | |
| 72 | + # | |
| 73 | + # or... | |
| 74 | + # | |
| 75 | + # class Auction | |
| 76 | + # def version_condition_met? # totally bypasses the <tt>:if</tt> option | |
| 77 | + # !expired? | |
| 78 | + # end | |
| 79 | + # end | |
| 80 | + # | |
| 81 | + # * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes | |
| 82 | + # either a symbol or array of symbols. | |
| 83 | + # | |
| 84 | + # == Database Schema | |
| 85 | + # | |
| 86 | + # The model that you're versioning needs to have a 'version' attribute. The model is versioned | |
| 87 | + # into a table called #{model}_versions where the model name is singlular. The _versions table should | |
| 88 | + # contain all the fields you want versioned, the same version column, and a #{model}_id foreign key field. | |
| 89 | + # | |
| 90 | + # A lock_version field is also accepted if your model uses Optimistic Locking. If your table uses Single Table inheritance, | |
| 91 | + # then that field is reflected in the versioned model as 'versioned_type' by default. | |
| 92 | + # | |
| 93 | + # Acts_as_versioned comes prepared with the ActiveRecord::Acts::Versioned::ActMethods::ClassMethods#create_versioned_table | |
| 94 | + # method, perfect for a migration. It will also create the version column if the main model does not already have it. | |
| 95 | + # | |
| 96 | + # class AddVersions < ActiveRecord::Migration | |
| 97 | + # def self.up | |
| 98 | + # # create_versioned_table takes the same options hash | |
| 99 | + # # that create_table does | |
| 100 | + # Post.create_versioned_table | |
| 101 | + # end | |
| 102 | + # | |
| 103 | + # def self.down | |
| 104 | + # Post.drop_versioned_table | |
| 105 | + # end | |
| 106 | + # end | |
| 107 | + def acts_as_versioned(options = {}) | |
| 108 | + # don't allow multiple calls | |
| 109 | + return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods) | |
| 110 | + | |
| 111 | + class_eval do | |
| 112 | + include ActiveRecord::Acts::Versioned::ActMethods | |
| 113 | + cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column, | |
| 114 | + :version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name | |
| 115 | + attr_accessor :changed_attributes | |
| 116 | + end | |
| 117 | + | |
| 118 | + self.versioned_class_name = options[:class_name] || "#{self.to_s.demodulize}Version" | |
| 119 | + self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key | |
| 120 | + self.versioned_table_name = options[:table_name] || "#{table_name_prefix}#{Inflector.underscore(Inflector.demodulize(class_name_of_active_record_descendant(self)))}_versions#{table_name_suffix}" | |
| 121 | + self.versioned_inheritance_column = options[:inheritance_column] || "versioned_#{inheritance_column}" | |
| 122 | + self.version_column = options[:version_column] || 'version' | |
| 123 | + self.version_sequence_name = options[:sequence_name] | |
| 124 | + self.max_version_limit = options[:limit].to_i | |
| 125 | + self.version_condition = options[:if] || true | |
| 126 | + | |
| 127 | + class_eval do | |
| 128 | + has_many :versions, | |
| 129 | + :class_name => "ActiveRecord::Acts::Versioned::#{versioned_class_name}", | |
| 130 | + :foreign_key => "#{versioned_foreign_key}", | |
| 131 | + :order => 'version' | |
| 132 | + before_save :set_new_version | |
| 133 | + after_create :save_version_on_create | |
| 134 | + after_update :save_version | |
| 135 | + after_save :clear_old_versions | |
| 136 | + after_save :clear_changed_attributes | |
| 137 | + | |
| 138 | + unless options[:if_changed].nil? | |
| 139 | + self.track_changed_attributes = true | |
| 140 | + options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array) | |
| 141 | + options[:if_changed].each do |attr_name| | |
| 142 | + define_method("#{attr_name}=") do |value| | |
| 143 | + (self.changed_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) or self.send(attr_name) == value | |
| 144 | + write_attribute(attr_name.to_s, value) | |
| 145 | + end | |
| 146 | + end | |
| 147 | + end | |
| 148 | + end | |
| 149 | + | |
| 150 | + # create the dynamic versioned model | |
| 151 | + # maybe if i sit down long enough i can think up a better way to do this. | |
| 152 | + dynamic_model = <<-EOV | |
| 153 | + class ActiveRecord::Acts::Versioned::#{versioned_class_name} < ActiveRecord::Base | |
| 154 | + set_table_name "#{versioned_table_name}" | |
| 155 | + belongs_to :#{self.to_s.demodulize.underscore}, :class_name => "#{self.to_s}" | |
| 156 | + EOV | |
| 157 | + | |
| 158 | + dynamic_model += %Q{set_sequence_name "#{version_sequence_name}"\n} if version_sequence_name | |
| 159 | + | |
| 160 | + eval dynamic_model + 'end' | |
| 161 | + end | |
| 162 | + end | |
| 163 | + | |
| 164 | + module ActMethods | |
| 165 | + def self.included(base) # :nodoc: | |
| 166 | + base.extend ClassMethods | |
| 167 | + end | |
| 168 | + | |
| 169 | + # Saves a version of the model if applicable | |
| 170 | + def save_version | |
| 171 | + save_version_on_create if save_version? | |
| 172 | + end | |
| 173 | + | |
| 174 | + # Saves a version of the model in the versioned table. This is called in the after_save callback by default | |
| 175 | + def save_version_on_create | |
| 176 | + rev = self.class.versioned_class.new | |
| 177 | + self.clone_versioned_model(self, rev) | |
| 178 | + rev.version = send(self.class.version_column) | |
| 179 | + rev.send("#{self.class.versioned_foreign_key}=", self.id) | |
| 180 | + rev.save | |
| 181 | + end | |
| 182 | + | |
| 183 | + # Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>. | |
| 184 | + # Override this method to set your own criteria for clearing old versions. | |
| 185 | + def clear_old_versions | |
| 186 | + return if self.class.max_version_limit == 0 | |
| 187 | + excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit | |
| 188 | + if excess_baggage > 0 | |
| 189 | + sql = "DELETE FROM #{self.class.versioned_table_name} WHERE version <= #{excess_baggage} AND #{self.class.versioned_foreign_key} = #{self.id}" | |
| 190 | + self.class.versioned_class.connection.execute sql | |
| 191 | + end | |
| 192 | + end | |
| 193 | + | |
| 194 | + # Finds a specific version of this model. | |
| 195 | + def find_version(version) | |
| 196 | + return version if version.is_a?(self.class.versioned_class) | |
| 197 | + return nil if version.is_a?(ActiveRecord::Base) | |
| 198 | + find_versions(:conditions => ['version = ?', version], :limit => 1).first | |
| 199 | + end | |
| 200 | + | |
| 201 | + # Finds versions of this model. Takes an options hash like <tt>find</tt> | |
| 202 | + def find_versions(options = {}) | |
| 203 | + versions.find(:all, options) | |
| 204 | + end | |
| 205 | + | |
| 206 | + # Reverts a model to a given version. Takes either a version number or an instance of the versioned model | |
| 207 | + def revert_to(version) | |
| 208 | + if version.is_a?(self.class.versioned_class) | |
| 209 | + return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record? | |
| 210 | + else | |
| 211 | + return false unless version = find_version(version) | |
| 212 | + end | |
| 213 | + self.clone_versioned_model(version, self) | |
| 214 | + self.send("#{self.class.version_column}=", version.version) | |
| 215 | + true | |
| 216 | + end | |
| 217 | + | |
| 218 | + # Reverts a model to a given version and saves the model. | |
| 219 | + # Takes either a version number or an instance of the versioned model | |
| 220 | + def revert_to!(version) | |
| 221 | + revert_to(version) ? save_without_revision : false | |
| 222 | + end | |
| 223 | + | |
| 224 | + # Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created. | |
| 225 | + def save_without_revision | |
| 226 | + old_lock_value = ActiveRecord::Base.lock_optimistically | |
| 227 | + ActiveRecord::Base.lock_optimistically = false if old_lock_value | |
| 228 | + disable_acts_as_versioned_callbacks | |
| 229 | + save_result = self.save | |
| 230 | + enable_acts_as_versioned_callbacks | |
| 231 | + ActiveRecord::Base.lock_optimistically = true if old_lock_value | |
| 232 | + save_result | |
| 233 | + end | |
| 234 | + | |
| 235 | + # Returns an array of attribute keys that are versioned. See non_versioned_fields | |
| 236 | + def versioned_attributes | |
| 237 | + self.attributes.keys.select { |k| !self.class.non_versioned_fields.include?(k) } | |
| 238 | + end | |
| 239 | + | |
| 240 | + # If called with no parameters, gets whether the current model has changed and needs to be versioned. | |
| 241 | + # If called with a single parameter, gets whether the parameter has changed. | |
| 242 | + def changed?(attr_name = nil) | |
| 243 | + attr_name.nil? ? | |
| 244 | + (!self.class.track_changed_attributes or (changed_attributes and changed_attributes.length > 0)) : | |
| 245 | + (changed_attributes and changed_attributes.include?(attr_name.to_s)) | |
| 246 | + end | |
| 247 | + | |
| 248 | + # keep old dirty? method | |
| 249 | + alias_method :dirty?, :changed? | |
| 250 | + | |
| 251 | + # Clones a model. Used when saving a new version or reverting a model's version. | |
| 252 | + def clone_versioned_model(orig_model, new_model) | |
| 253 | + self.versioned_attributes.each do |key| | |
| 254 | + new_model.send("#{key}=", orig_model.attributes[key]) if orig_model.attribute_present?(key) | |
| 255 | + end | |
| 256 | + | |
| 257 | + if orig_model.is_a?(self.class.versioned_class) | |
| 258 | + new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column] | |
| 259 | + elsif new_model.is_a?(self.class.versioned_class) | |
| 260 | + new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column] | |
| 261 | + end | |
| 262 | + end | |
| 263 | + | |
| 264 | + # Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>. | |
| 265 | + def save_version? | |
| 266 | + version_condition_met? and changed? | |
| 267 | + end | |
| 268 | + | |
| 269 | + # Checks condition set in the :if option to check whether a revision should be created or not. Override this for | |
| 270 | + # custom version condition checking. | |
| 271 | + def version_condition_met? | |
| 272 | + case | |
| 273 | + when version_condition.is_a?(Symbol) | |
| 274 | + send(version_condition) | |
| 275 | + when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1) | |
| 276 | + version_condition.call(self) | |
| 277 | + else | |
| 278 | + version_condition | |
| 279 | + end | |
| 280 | + end | |
| 281 | + | |
| 282 | + protected | |
| 283 | + # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version. | |
| 284 | + def set_new_version | |
| 285 | + self.send("#{self.class.version_column}=", self.next_version) if new_record? or (!locking_enabled? and save_version?) | |
| 286 | + end | |
| 287 | + | |
| 288 | + # Gets the next available version for the current record, or 1 for a new record | |
| 289 | + def next_version | |
| 290 | + return 1 if new_record? | |
| 291 | + connection.select_one("SELECT MAX(version)+1 AS next_version FROM #{self.class.versioned_table_name} WHERE #{self.class.versioned_foreign_key} = #{self.id}")['next_version'] || 1 | |
| 292 | + end | |
| 293 | + | |
| 294 | + # clears current changed attributes. Called after save. | |
| 295 | + def clear_changed_attributes | |
| 296 | + self.changed_attributes = [] | |
| 297 | + end | |
| 298 | + | |
| 299 | + private | |
| 300 | + unless defined?(ACTS_AS_VERSIONED_CALLBACKS) | |
| 301 | + ACTS_AS_VERSIONED_CALLBACKS = [:set_new_version, :save_version_on_create, :save_version, :clear_changed_attributes] | |
| 302 | + end | |
| 303 | + | |
| 304 | + ACTS_AS_VERSIONED_CALLBACKS.each do |attr_name| | |
| 305 | + alias_method "orig_#{attr_name}".to_sym, attr_name | |
| 306 | + end | |
| 307 | + | |
| 308 | + def empty_callback() end #:nodoc: | |
| 309 | + | |
| 310 | + def enable_acts_as_versioned_callbacks | |
| 311 | + self.class.class_eval do | |
| 312 | + ACTS_AS_VERSIONED_CALLBACKS.each do |attr_name| | |
| 313 | + alias_method attr_name, "orig_#{attr_name}".to_sym | |
| 314 | + end | |
| 315 | + end | |
| 316 | + end | |
| 317 | + | |
| 318 | + def disable_acts_as_versioned_callbacks | |
| 319 | + self.class.class_eval do | |
| 320 | + ACTS_AS_VERSIONED_CALLBACKS.each do |attr_name| | |
| 321 | + alias_method attr_name, :empty_callback | |
| 322 | + end | |
| 323 | + end | |
| 324 | + end | |
| 325 | + | |
| 326 | + module ClassMethods | |
| 327 | + # Finds a specific version of a specific row of this model | |
| 328 | + def find_version(id, version) | |
| 329 | + find_versions(id, | |
| 330 | + :conditions => ["#{versioned_foreign_key} = ? AND version = ?", id, version], | |
| 331 | + :limit => 1).first | |
| 332 | + end | |
| 333 | + | |
| 334 | + # Finds versions of a specific model. Takes an options hash like <tt>find</tt> | |
| 335 | + def find_versions(id, options = {}) | |
| 336 | + versioned_class.find :all, { | |
| 337 | + :conditions => ["#{versioned_foreign_key} = ?", id], | |
| 338 | + :order => 'version' }.merge(options) | |
| 339 | + end | |
| 340 | + | |
| 341 | + # Returns an array of columns that are versioned. See non_versioned_fields | |
| 342 | + def versioned_columns | |
| 343 | + self.columns.select { |c| !non_versioned_fields.include?(c.name) } | |
| 344 | + end | |
| 345 | + | |
| 346 | + # Returns an instance of the dynamic versioned model | |
| 347 | + def versioned_class | |
| 348 | + "ActiveRecord::Acts::Versioned::#{versioned_class_name}".constantize | |
| 349 | + end | |
| 350 | + | |
| 351 | + # An array of fields that are not saved in the versioned table | |
| 352 | + def non_versioned_fields | |
| 353 | + [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column] | |
| 354 | + end | |
| 355 | + | |
| 356 | + # Rake migration task to create the versioned table using options passed to acts_as_versioned | |
| 357 | + def create_versioned_table(create_table_options = {}) | |
| 358 | + # create version column in main table if it does not exist | |
| 359 | + if !self.content_columns.find { |c| %w(version lock_version).include? c.name } | |
| 360 | + self.connection.add_column table_name, :version, :integer | |
| 361 | + end | |
| 362 | + | |
| 363 | + self.connection.create_table(versioned_table_name, create_table_options) do |t| | |
| 364 | + t.column versioned_foreign_key, :integer | |
| 365 | + t.column :version, :integer | |
| 366 | + end | |
| 367 | + | |
| 368 | + updated_col = nil | |
| 369 | + self.versioned_columns.each do |col| | |
| 370 | + updated_col = col if !updated_col and %(updated_at updated_on).include?(col.name) | |
| 371 | + self.connection.add_column versioned_table_name, col.name, col.type, | |
| 372 | + :limit => col.limit, | |
| 373 | + :default => col.default | |
| 374 | + end | |
| 375 | + | |
| 376 | + if type_col = self.columns_hash[inheritance_column] | |
| 377 | + self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type, | |
| 378 | + :limit => type_col.limit, | |
| 379 | + :default => type_col.default | |
| 380 | + end | |
| 381 | + | |
| 382 | + if updated_col.nil? | |
| 383 | + self.connection.add_column versioned_table_name, :updated_at, :timestamp | |
| 384 | + end | |
| 385 | + end | |
| 386 | + | |
| 387 | + # Rake migration task to drop the versioned table | |
| 388 | + def drop_versioned_table | |
| 389 | + self.connection.drop_table versioned_table_name | |
| 390 | + end | |
| 391 | + end | |
| 392 | + end | |
| 393 | + end | |
| 394 | + end | |
| 395 | +end | |
| 396 | + | |
| 397 | +ActiveRecord::Base.class_eval { include ActiveRecord::Acts::Versioned } | |
| 0 | 398 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,43 @@ |
| 1 | +$:.unshift(File.dirname(__FILE__) + '/../lib') | |
| 2 | + | |
| 3 | +require 'test/unit' | |
| 4 | +require 'active_record' | |
| 5 | +require 'active_record/fixtures' | |
| 6 | +require 'active_support/binding_of_caller' | |
| 7 | +require 'active_support/breakpoint' | |
| 8 | +require "#{File.dirname(__FILE__)}/../init" | |
| 9 | + | |
| 10 | +config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) | |
| 11 | +ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") | |
| 12 | +ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite']) | |
| 13 | + | |
| 14 | +load(File.dirname(__FILE__) + "/schema.rb") | |
| 15 | + | |
| 16 | +# set up custom sequence on widget_versions for DBs that support sequences | |
| 17 | +if ENV['DB'] == 'postgresql' | |
| 18 | + ActiveRecord::Base.connection.execute "DROP SEQUENCE widgets_seq;" rescue nil | |
| 19 | + ActiveRecord::Base.connection.remove_column :widget_versions, :id | |
| 20 | + ActiveRecord::Base.connection.execute "CREATE SEQUENCE widgets_seq START 101;" | |
| 21 | + ActiveRecord::Base.connection.execute "ALTER TABLE widget_versions ADD COLUMN id INTEGER PRIMARY KEY DEFAULT nextval('widgets_seq');" | |
| 22 | +end | |
| 23 | + | |
| 24 | +Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/" | |
| 25 | +$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path) | |
| 26 | + | |
| 27 | +class Test::Unit::TestCase #:nodoc: | |
| 28 | + def create_fixtures(*table_names) | |
| 29 | + if block_given? | |
| 30 | + Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield } | |
| 31 | + else | |
| 32 | + Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) | |
| 33 | + end | |
| 34 | + end | |
| 35 | + | |
| 36 | + # Turn off transactional fixtures if you're working with MyISAM tables in MySQL | |
| 37 | + self.use_transactional_fixtures = true | |
| 38 | + | |
| 39 | + # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david) | |
| 40 | + self.use_instantiated_fixtures = false | |
| 41 | + | |
| 42 | + # Add more helper methods to be used by all tests here... | |
| 43 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,18 @@ |
| 1 | +sqlite: | |
| 2 | + :adapter: sqlite | |
| 3 | + :dbfile: acts_as_versioned_plugin.sqlite.db | |
| 4 | +sqlite3: | |
| 5 | + :adapter: sqlite3 | |
| 6 | + :dbfile: acts_as_versioned_plugin.sqlite3.db | |
| 7 | +postgresql: | |
| 8 | + :adapter: postgresql | |
| 9 | + :username: postgres | |
| 10 | + :password: postgres | |
| 11 | + :database: acts_as_versioned_plugin_test | |
| 12 | + :min_messages: ERROR | |
| 13 | +mysql: | |
| 14 | + :adapter: mysql | |
| 15 | + :host: localhost | |
| 16 | + :username: rails | |
| 17 | + :password: | |
| 18 | + :database: acts_as_versioned_plugin_test | |
| 0 | 19 | \ No newline at end of file | ... | ... |
vendor/plugins/acts_as_versioned/test/fixtures/locked_pages.yml
0 → 100644
vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml
0 → 100644
| ... | ... | @@ -0,0 +1,27 @@ |
| 1 | +welcome_1: | |
| 2 | + id: 1 | |
| 3 | + page_id: 1 | |
| 4 | + title: Welcome to the weblg | |
| 5 | + version: 23 | |
| 6 | + version_type: LockedPage | |
| 7 | + | |
| 8 | +welcome_2: | |
| 9 | + id: 2 | |
| 10 | + page_id: 1 | |
| 11 | + title: Welcome to the weblog | |
| 12 | + version: 24 | |
| 13 | + version_type: LockedPage | |
| 14 | + | |
| 15 | +thinking_1: | |
| 16 | + id: 3 | |
| 17 | + page_id: 2 | |
| 18 | + title: So I was thinking!!! | |
| 19 | + version: 23 | |
| 20 | + version_type: SpecialLockedPage | |
| 21 | + | |
| 22 | +thinking_2: | |
| 23 | + id: 4 | |
| 24 | + page_id: 2 | |
| 25 | + title: So I was thinking | |
| 26 | + version: 24 | |
| 27 | + version_type: SpecialLockedPage | ... | ... |
vendor/plugins/acts_as_versioned/test/fixtures/migrations/1_add_versioned_tables.rb
0 → 100644
| ... | ... | @@ -0,0 +1,13 @@ |
| 1 | +class AddVersionedTables < ActiveRecord::Migration | |
| 2 | + def self.up | |
| 3 | + create_table("things") do |t| | |
| 4 | + t.column :title, :text | |
| 5 | + end | |
| 6 | + Thing.create_versioned_table | |
| 7 | + end | |
| 8 | + | |
| 9 | + def self.down | |
| 10 | + Thing.drop_versioned_table | |
| 11 | + drop_table "things" rescue nil | |
| 12 | + end | |
| 13 | +end | |
| 0 | 14 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,24 @@ |
| 1 | +class Page < ActiveRecord::Base | |
| 2 | + cattr_accessor :feeling_good | |
| 3 | + @@feeling_good = true | |
| 4 | + | |
| 5 | + acts_as_versioned :if => :feeling_good? | |
| 6 | + | |
| 7 | + def feeling_good? | |
| 8 | + @@feeling_good == true | |
| 9 | + end | |
| 10 | +end | |
| 11 | + | |
| 12 | +class LockedPage < ActiveRecord::Base | |
| 13 | + acts_as_versioned \ | |
| 14 | + :inheritance_column => :version_type, | |
| 15 | + :foreign_key => :page_id, | |
| 16 | + :table_name => :locked_pages_revisions, | |
| 17 | + :class_name => 'LockedPageRevision', | |
| 18 | + :version_column => :lock_version, | |
| 19 | + :limit => 2, | |
| 20 | + :if_changed => :title | |
| 21 | +end | |
| 22 | + | |
| 23 | +class SpecialLockedPage < LockedPage | |
| 24 | +end | |
| 0 | 25 | \ No newline at end of file | ... | ... |
vendor/plugins/acts_as_versioned/test/fixtures/page_versions.yml
0 → 100644
vendor/plugins/acts_as_versioned/test/fixtures/pages.yml
0 → 100644
vendor/plugins/acts_as_versioned/test/fixtures/widget.rb
0 → 100644
| ... | ... | @@ -0,0 +1,32 @@ |
| 1 | +require File.join(File.dirname(__FILE__), 'abstract_unit') | |
| 2 | + | |
| 3 | +if ActiveRecord::Base.connection.supports_migrations? | |
| 4 | + class Thing < ActiveRecord::Base | |
| 5 | + attr_accessor :version | |
| 6 | + acts_as_versioned | |
| 7 | + end | |
| 8 | + | |
| 9 | + class MigrationTest < Test::Unit::TestCase | |
| 10 | + self.use_transactional_fixtures = false | |
| 11 | + def teardown | |
| 12 | + ActiveRecord::Base.connection.initialize_schema_information | |
| 13 | + ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" | |
| 14 | + | |
| 15 | + Thing.connection.drop_table "things" rescue nil | |
| 16 | + Thing.connection.drop_table "thing_versions" rescue nil | |
| 17 | + Thing.reset_column_information | |
| 18 | + end | |
| 19 | + | |
| 20 | + def test_versioned_migration | |
| 21 | + assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' } | |
| 22 | + # take 'er up | |
| 23 | + ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') | |
| 24 | + t = Thing.create :title => 'blah blah' | |
| 25 | + assert_equal 1, t.versions.size | |
| 26 | + | |
| 27 | + # now lets take 'er back down | |
| 28 | + ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/') | |
| 29 | + assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' } | |
| 30 | + end | |
| 31 | + end | |
| 32 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,43 @@ |
| 1 | +ActiveRecord::Schema.define(:version => 0) do | |
| 2 | + create_table :pages, :force => true do |t| | |
| 3 | + t.column :version, :integer | |
| 4 | + t.column :title, :string, :limit => 255 | |
| 5 | + t.column :body, :text | |
| 6 | + t.column :updated_on, :datetime | |
| 7 | + end | |
| 8 | + | |
| 9 | + create_table :page_versions, :force => true do |t| | |
| 10 | + t.column :page_id, :integer | |
| 11 | + t.column :version, :integer | |
| 12 | + t.column :title, :string, :limit => 255 | |
| 13 | + t.column :body, :text | |
| 14 | + t.column :updated_on, :datetime | |
| 15 | + end | |
| 16 | + | |
| 17 | + create_table :locked_pages, :force => true do |t| | |
| 18 | + t.column :lock_version, :integer | |
| 19 | + t.column :title, :string, :limit => 255 | |
| 20 | + t.column :type, :string, :limit => 255 | |
| 21 | + end | |
| 22 | + | |
| 23 | + create_table :locked_pages_revisions, :force => true do |t| | |
| 24 | + t.column :page_id, :integer | |
| 25 | + t.column :version, :integer | |
| 26 | + t.column :title, :string, :limit => 255 | |
| 27 | + t.column :version_type, :string, :limit => 255 | |
| 28 | + t.column :updated_at, :datetime | |
| 29 | + end | |
| 30 | + | |
| 31 | + create_table :widgets, :force => true do |t| | |
| 32 | + t.column :name, :string, :limit => 50 | |
| 33 | + t.column :version, :integer | |
| 34 | + t.column :updated_at, :datetime | |
| 35 | + end | |
| 36 | + | |
| 37 | + create_table :widget_versions, :force => true do |t| | |
| 38 | + t.column :widget_id, :integer | |
| 39 | + t.column :name, :string, :limit => 50 | |
| 40 | + t.column :version, :integer | |
| 41 | + t.column :updated_at, :datetime | |
| 42 | + end | |
| 43 | +end | |
| 0 | 44 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,238 @@ |
| 1 | +require File.join(File.dirname(__FILE__), 'abstract_unit') | |
| 2 | +require File.join(File.dirname(__FILE__), 'fixtures/page') | |
| 3 | +require File.join(File.dirname(__FILE__), 'fixtures/widget') | |
| 4 | + | |
| 5 | +class VersionedTest < Test::Unit::TestCase | |
| 6 | + fixtures :pages, :page_versions, :locked_pages, :locked_pages_revisions | |
| 7 | + | |
| 8 | + def test_saves_versioned_copy | |
| 9 | + p = Page.create :title => 'first title', :body => 'first body' | |
| 10 | + assert !p.new_record? | |
| 11 | + assert_equal 1, p.versions.size | |
| 12 | + assert_equal 1, p.version | |
| 13 | + assert_instance_of Page.versioned_class, p.versions.first | |
| 14 | + end | |
| 15 | + | |
| 16 | + def test_rollback_with_version_number | |
| 17 | + p = pages(:welcome) | |
| 18 | + assert_equal 24, p.version | |
| 19 | + assert_equal 'Welcome to the weblog', p.title | |
| 20 | + | |
| 21 | + assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23" | |
| 22 | + assert_equal 23, p.version | |
| 23 | + assert_equal 'Welcome to the weblg', p.title | |
| 24 | + end | |
| 25 | + | |
| 26 | + def test_versioned_class_name | |
| 27 | + assert_equal 'PageVersion', Page.versioned_class_name | |
| 28 | + assert_equal 'LockedPageRevision', LockedPage.versioned_class_name | |
| 29 | + end | |
| 30 | + | |
| 31 | + def test_rollback_with_version_class | |
| 32 | + p = pages(:welcome) | |
| 33 | + assert_equal 24, p.version | |
| 34 | + assert_equal 'Welcome to the weblog', p.title | |
| 35 | + | |
| 36 | + assert p.revert_to!(p.versions.first), "Couldn't revert to 23" | |
| 37 | + assert_equal 23, p.version | |
| 38 | + assert_equal 'Welcome to the weblg', p.title | |
| 39 | + end | |
| 40 | + | |
| 41 | + def test_rollback_fails_with_invalid_revision | |
| 42 | + p = locked_pages(:welcome) | |
| 43 | + assert !p.revert_to!(locked_pages(:thinking)) | |
| 44 | + end | |
| 45 | + | |
| 46 | + def test_saves_versioned_copy_with_options | |
| 47 | + p = LockedPage.create :title => 'first title' | |
| 48 | + assert !p.new_record? | |
| 49 | + assert_equal 1, p.versions.size | |
| 50 | + assert_instance_of LockedPage.versioned_class, p.versions.first | |
| 51 | + end | |
| 52 | + | |
| 53 | + def test_rollback_with_version_number_with_options | |
| 54 | + p = locked_pages(:welcome) | |
| 55 | + assert_equal 'Welcome to the weblog', p.title | |
| 56 | + assert_equal 'LockedPage', p.versions.first.version_type | |
| 57 | + | |
| 58 | + assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23" | |
| 59 | + assert_equal 'Welcome to the weblg', p.title | |
| 60 | + assert_equal 'LockedPage', p.versions.first.version_type | |
| 61 | + end | |
| 62 | + | |
| 63 | + def test_rollback_with_version_class_with_options | |
| 64 | + p = locked_pages(:welcome) | |
| 65 | + assert_equal 'Welcome to the weblog', p.title | |
| 66 | + assert_equal 'LockedPage', p.versions.first.version_type | |
| 67 | + | |
| 68 | + assert p.revert_to!(p.versions.first), "Couldn't revert to 1" | |
| 69 | + assert_equal 'Welcome to the weblg', p.title | |
| 70 | + assert_equal 'LockedPage', p.versions.first.version_type | |
| 71 | + end | |
| 72 | + | |
| 73 | + def test_saves_versioned_copy_with_sti | |
| 74 | + p = SpecialLockedPage.create :title => 'first title' | |
| 75 | + assert !p.new_record? | |
| 76 | + assert_equal 1, p.versions.size | |
| 77 | + assert_instance_of LockedPage.versioned_class, p.versions.first | |
| 78 | + assert_equal 'SpecialLockedPage', p.versions.first.version_type | |
| 79 | + end | |
| 80 | + | |
| 81 | + def test_rollback_with_version_number_with_sti | |
| 82 | + p = locked_pages(:thinking) | |
| 83 | + assert_equal 'So I was thinking', p.title | |
| 84 | + | |
| 85 | + assert p.revert_to!(p.versions.first.version), "Couldn't revert to 1" | |
| 86 | + assert_equal 'So I was thinking!!!', p.title | |
| 87 | + assert_equal 'SpecialLockedPage', p.versions.first.version_type | |
| 88 | + end | |
| 89 | + | |
| 90 | + def test_lock_version_works_with_versioning | |
| 91 | + p = locked_pages(:thinking) | |
| 92 | + p2 = LockedPage.find(p.id) | |
| 93 | + | |
| 94 | + p.title = 'fresh title' | |
| 95 | + p.save | |
| 96 | + assert_equal 2, p.versions.size # limit! | |
| 97 | + | |
| 98 | + assert_raises(ActiveRecord::StaleObjectError) do | |
| 99 | + p2.title = 'stale title' | |
| 100 | + p2.save | |
| 101 | + end | |
| 102 | + end | |
| 103 | + | |
| 104 | + def test_version_if_condition | |
| 105 | + p = Page.create :title => "title" | |
| 106 | + assert_equal 1, p.version | |
| 107 | + | |
| 108 | + Page.feeling_good = false | |
| 109 | + p.save | |
| 110 | + assert_equal 1, p.version | |
| 111 | + Page.feeling_good = true | |
| 112 | + end | |
| 113 | + | |
| 114 | + def test_version_if_condition2 | |
| 115 | + # set new if condition | |
| 116 | + Page.class_eval do | |
| 117 | + def new_feeling_good() title[0..0] == 'a'; end | |
| 118 | + alias_method :old_feeling_good, :feeling_good? | |
| 119 | + alias_method :feeling_good?, :new_feeling_good | |
| 120 | + end | |
| 121 | + | |
| 122 | + p = Page.create :title => "title" | |
| 123 | + assert_equal 1, p.version # version does not increment | |
| 124 | + assert_equal 1, p.versions(true).size | |
| 125 | + | |
| 126 | + p.update_attributes(:title => 'new title') | |
| 127 | + assert_equal 1, p.version # version does not increment | |
| 128 | + assert_equal 1, p.versions(true).size | |
| 129 | + | |
| 130 | + p.update_attributes(:title => 'a title') | |
| 131 | + assert_equal 2, p.version | |
| 132 | + assert_equal 2, p.versions(true).size | |
| 133 | + | |
| 134 | + # reset original if condition | |
| 135 | + Page.class_eval { alias_method :feeling_good?, :old_feeling_good } | |
| 136 | + end | |
| 137 | + | |
| 138 | + def test_version_if_condition_with_block | |
| 139 | + # set new if condition | |
| 140 | + old_condition = Page.version_condition | |
| 141 | + Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' } | |
| 142 | + | |
| 143 | + p = Page.create :title => "title" | |
| 144 | + assert_equal 1, p.version # version does not increment | |
| 145 | + assert_equal 1, p.versions(true).size | |
| 146 | + | |
| 147 | + p.update_attributes(:title => 'a title') | |
| 148 | + assert_equal 1, p.version # version does not increment | |
| 149 | + assert_equal 1, p.versions(true).size | |
| 150 | + | |
| 151 | + p.update_attributes(:title => 'b title') | |
| 152 | + assert_equal 2, p.version | |
| 153 | + assert_equal 2, p.versions(true).size | |
| 154 | + | |
| 155 | + # reset original if condition | |
| 156 | + Page.version_condition = old_condition | |
| 157 | + end | |
| 158 | + | |
| 159 | + def test_version_no_limit | |
| 160 | + p = Page.create :title => "title", :body => 'first body' | |
| 161 | + p.save | |
| 162 | + p.save | |
| 163 | + 5.times do |i| | |
| 164 | + assert_page_title p, i | |
| 165 | + end | |
| 166 | + end | |
| 167 | + | |
| 168 | + def test_version_max_limit | |
| 169 | + p = LockedPage.create :title => "title" | |
| 170 | + p.update_attributes(:title => "title1") | |
| 171 | + p.update_attributes(:title => "title2") | |
| 172 | + 5.times do |i| | |
| 173 | + assert_page_title p, i, :lock_version | |
| 174 | + assert p.versions(true).size <= 2, "locked version can only store 2 versions" | |
| 175 | + end | |
| 176 | + end | |
| 177 | + | |
| 178 | + def test_track_changed_attributes_default_value | |
| 179 | + assert !Page.track_changed_attributes | |
| 180 | + assert LockedPage.track_changed_attributes | |
| 181 | + assert SpecialLockedPage.track_changed_attributes | |
| 182 | + end | |
| 183 | + | |
| 184 | + def test_version_order | |
| 185 | + assert_equal 23, pages(:welcome).versions.first.version | |
| 186 | + assert_equal 24, pages(:welcome).versions.last.version | |
| 187 | + assert_equal 23, pages(:welcome).find_versions.first.version | |
| 188 | + assert_equal 24, pages(:welcome).find_versions.last.version | |
| 189 | + end | |
| 190 | + | |
| 191 | + def test_track_changed_attributes | |
| 192 | + p = LockedPage.create :title => "title" | |
| 193 | + assert_equal 1, p.lock_version | |
| 194 | + assert_equal 1, p.versions(true).size | |
| 195 | + | |
| 196 | + p.title = 'title' | |
| 197 | + assert !p.save_version? | |
| 198 | + p.save | |
| 199 | + assert_equal 2, p.lock_version # still increments version because of optimistic locking | |
| 200 | + assert_equal 1, p.versions(true).size | |
| 201 | + | |
| 202 | + p.title = 'updated title' | |
| 203 | + assert p.save_version? | |
| 204 | + p.save | |
| 205 | + assert_equal 3, p.lock_version | |
| 206 | + assert_equal 1, p.versions(true).size # version 1 deleted | |
| 207 | + | |
| 208 | + p.title = 'updated title!' | |
| 209 | + assert p.save_version? | |
| 210 | + p.save | |
| 211 | + assert_equal 4, p.lock_version | |
| 212 | + assert_equal 2, p.versions(true).size # version 1 deleted | |
| 213 | + end | |
| 214 | + | |
| 215 | + def assert_page_title(p, i, version_field = :version) | |
| 216 | + p.title = "title#{i}" | |
| 217 | + p.save | |
| 218 | + assert_equal "title#{i}", p.title | |
| 219 | + assert_equal (i+4), p.send(version_field) | |
| 220 | + end | |
| 221 | + | |
| 222 | + def test_find_versions | |
| 223 | + assert_equal 2, locked_pages(:welcome).versions.size | |
| 224 | + assert_equal 1, locked_pages(:welcome).find_versions(:conditions => ['title LIKE ?', '%weblog%']).length | |
| 225 | + assert_equal 2, locked_pages(:welcome).find_versions(:conditions => ['title LIKE ?', '%web%']).length | |
| 226 | + assert_equal 0, locked_pages(:thinking).find_versions(:conditions => ['title LIKE ?', '%web%']).length | |
| 227 | + assert_equal 2, locked_pages(:welcome).find_versions.length | |
| 228 | + end | |
| 229 | + | |
| 230 | + def test_with_sequence | |
| 231 | + assert_equal 'widgets_seq', Widget.versioned_class.sequence_name | |
| 232 | + Widget.create :name => 'new widget' | |
| 233 | + Widget.create :name => 'new widget' | |
| 234 | + Widget.create :name => 'new widget' | |
| 235 | + assert_equal 3, Widget.count | |
| 236 | + assert_equal 3, Widget.versioned_class.count | |
| 237 | + end | |
| 238 | +end | ... | ... |