diff options
| author | rick <technoweenie@gmail.com> | 2008-10-03 15:37:17 -0700 |
|---|---|---|
| committer | rick <technoweenie@gmail.com> | 2008-10-03 15:37:17 -0700 |
| commit | d010b497bd548a6dd629ade03e0852d257ea0872 (patch) | |
| tree | d57df33c8283dae53de5f8339b0e749e596ef700 | |
| parent | 49463ee4dde1bc6527f26f64b5fd8695e12dba63 (diff) | |
| parent | 41c26cd1035cb11bf298710792baa0571be7e00c (diff) | |
Merge branch 'master' of git://github.com/francis/acts_as_versioned
| -rw-r--r-- | lib/acts_as_versioned.rb | 44 | ||||
| -rw-r--r-- | test/fixtures/locked_pages_revisions.yml | 8 | ||||
| -rw-r--r-- | test/schema.rb | 4 | ||||
| -rw-r--r-- | test/versioned_test.rb | 4 |
4 files changed, 31 insertions, 29 deletions
diff --git a/lib/acts_as_versioned.rb b/lib/acts_as_versioned.rb index 5299e0dc7..efc44b795 100644 --- a/lib/acts_as_versioned.rb +++ b/lib/acts_as_versioned.rb @@ -95,12 +95,7 @@ module ActiveRecord #:nodoc: # end # # * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes - # either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have. - # Use this instead if you want to write your own attribute setters (and ignore if_changed): - # - # def name=(new_name) - # write_changed_attribute :name, new_name - # end + # either a symbol or array of symbols. # # * <tt>extend</tt> - Lets you specify a module to be mixed in both the original and versioned models. You can also just pass a block # to create an anonymous mixin: @@ -185,7 +180,7 @@ module ActiveRecord #:nodoc: self.version_sequence_name = options[:sequence_name] self.max_version_limit = options[:limit].to_i self.version_condition = options[:if] || true - self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column, 'created_at', 'created_on'] + self.non_versioned_columns = [self.primary_key, inheritance_column, self.version_column, 'lock_version', versioned_inheritance_column, 'created_at', 'created_on'] + options[:non_versioned_columns].to_a.map(&:to_s) self.version_association_options = { :class_name => "#{self.to_s}::#{versioned_class_name}", :foreign_key => versioned_foreign_key, @@ -201,16 +196,16 @@ module ActiveRecord #:nodoc: options[:extend] = self.const_get(extension_module_name) end - class_eval do + class_eval <<-CLASS_METHODS has_many :versions, version_association_options do # finds earliest version of this record def earliest - @earliest ||= find(:first, :order => 'version') + @earliest ||= find(:first, :order => '#{version_column}') end # find latest version of this record def latest - @latest ||= find(:first, :order => 'version desc') + @latest ||= find(:first, :order => '#{version_column} desc') end end before_save :set_new_version @@ -220,11 +215,11 @@ module ActiveRecord #:nodoc: unless options[:if_changed].nil? self.track_altered_attributes = true options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array) - self.version_if_changed = options[:if_changed] + self.version_if_changed = options[:if_changed].map(&:to_s) end include options[:extend] if options[:extend].is_a?(Module) - end + CLASS_METHODS # create the dynamic versioned model const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do @@ -262,6 +257,8 @@ module ActiveRecord #:nodoc: :foreign_key => versioned_foreign_key versioned_class.send :include, options[:extend] if options[:extend].is_a?(Module) versioned_class.set_sequence_name version_sequence_name if version_sequence_name + + create_versioned_table end end @@ -276,7 +273,7 @@ module ActiveRecord #:nodoc: @saving_version = nil rev = self.class.versioned_class.new clone_versioned_model(self, rev) - rev.version = send(self.class.version_column) + rev.send("#{self.class.version_column}=", send(self.class.version_column)) rev.send("#{self.class.versioned_foreign_key}=", id) rev.save end @@ -288,7 +285,7 @@ module ActiveRecord #:nodoc: return if self.class.max_version_limit == 0 excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit if excess_baggage > 0 - self.class.versioned_class.delete_all ["version <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id] + self.class.versioned_class.delete_all ["#{self.class.version_column} <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id] end end @@ -297,10 +294,10 @@ module ActiveRecord #:nodoc: if version.is_a?(self.class.versioned_class) return false unless version.send(self.class.versioned_foreign_key) == id and !version.new_record? else - return false unless version = versions.find_by_version(version) + return false unless version = versions.send("find_by_#{self.class.version_column}", version) end self.clone_versioned_model(version, self) - send("#{self.class.version_column}=", version.version) + send("#{self.class.version_column}=", version.send(self.class.version_column)) true end @@ -327,7 +324,7 @@ module ActiveRecord #:nodoc: end def altered? - track_altered_attributes ? (version_if_changed.map(&:to_s) - changed).length < version_if_changed.length : changed? + track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed? end # Clones a model. Used when saving a new version or reverting a model's version. @@ -392,7 +389,7 @@ module ActiveRecord #:nodoc: # Gets the next available version for the current record, or 1 for a new record def next_version - (new_record? ? 0 : versions.calculate(:max, :version).to_i) + 1 + (new_record? ? 0 : versions.calculate(:max, version_column).to_i) + 1 end module ClassMethods @@ -409,13 +406,16 @@ module ActiveRecord #:nodoc: # Rake migration task to create the versioned table using options passed to acts_as_versioned def create_versioned_table(create_table_options = {}) # create version column in main table if it does not exist - if !self.content_columns.find { |c| %w(version lock_version).include? c.name } - self.connection.add_column table_name, :version, :integer + if !self.content_columns.find { |c| [version_column.to_s, 'lock_version'].include? c.name } + self.connection.add_column table_name, version_column, :integer + self.reset_column_information end + return if connection.tables.include?(versioned_table_name) + self.connection.create_table(versioned_table_name, create_table_options) do |t| t.column versioned_foreign_key, :integer - t.column :version, :integer + t.column version_column, :integer end updated_col = nil @@ -439,6 +439,8 @@ module ActiveRecord #:nodoc: if updated_col.nil? self.connection.add_column versioned_table_name, :updated_at, :timestamp end + + self.connection.create_index versioned_table_name, versioned_foreign_key end # Rake migration task to drop the versioned table diff --git a/test/fixtures/locked_pages_revisions.yml b/test/fixtures/locked_pages_revisions.yml index 5c978e629..3a1be5a9a 100644 --- a/test/fixtures/locked_pages_revisions.yml +++ b/test/fixtures/locked_pages_revisions.yml @@ -2,26 +2,26 @@ welcome_1: id: 1 page_id: 1 title: Welcome to the weblg - version: 23 + lock_version: 23 version_type: LockedPage welcome_2: id: 2 page_id: 1 title: Welcome to the weblog - version: 24 + lock_version: 24 version_type: LockedPage thinking_1: id: 3 page_id: 2 title: So I was thinking!!! - version: 23 + lock_version: 23 version_type: SpecialLockedPage thinking_2: id: 4 page_id: 2 title: So I was thinking - version: 24 + lock_version: 24 version_type: SpecialLockedPage diff --git a/test/schema.rb b/test/schema.rb index 4e7e96319..8f36749c7 100644 --- a/test/schema.rb +++ b/test/schema.rb @@ -35,13 +35,13 @@ ActiveRecord::Schema.define(:version => 0) do create_table :locked_pages_revisions, :force => true do |t| t.column :page_id, :integer - t.column :version, :integer + t.column :lock_version, :integer t.column :title, :string, :limit => 255 t.column :version_type, :string, :limit => 255 t.column :updated_at, :datetime end - add_index :locked_pages_revisions, [:page_id, :version], :unique => true + add_index :locked_pages_revisions, [:page_id, :lock_version], :unique => true create_table :widgets, :force => true do |t| t.column :name, :string, :limit => 50 diff --git a/test/versioned_test.rb b/test/versioned_test.rb index 6ab9e739c..4c8c89fa3 100644 --- a/test/versioned_test.rb +++ b/test/versioned_test.rb @@ -88,7 +88,7 @@ class VersionedTest < Test::Unit::TestCase assert_equal 'Welcome to the weblog', p.title assert_equal 'LockedPage', p.versions.first.version_type - assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23" + assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 23" assert_equal 'Welcome to the weblg', p.title assert_equal 'LockedPage', p.versions.first.version_type end @@ -115,7 +115,7 @@ class VersionedTest < Test::Unit::TestCase p = locked_pages(:thinking) assert_equal 'So I was thinking', p.title - assert p.revert_to!(p.versions.first.version), "Couldn't revert to 1" + assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 1" assert_equal 'So I was thinking!!!', p.title assert_equal 'SpecialLockedPage', p.versions.first.version_type end |
