Skip to content

Commit

Permalink
Allow customization of #versions association options [Dan Peterson]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.techno-weenie.net/projects/plugins/acts_as_versioned@2329 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information
technoweenie committed Oct 5, 2006
1 parent 95a223d commit 51b7540
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -1,3 +1,7 @@
*SVN* (version numbers are overrated)

* (5 Oct 2006) Allow customization of #versions association options [Dan Peterson]

*0.5.1*

* (8 Aug 2006) Versioned models now belong to the unversioned model. @article_version.article.class => Article [Aslak Hellesoy]
Expand Down
14 changes: 9 additions & 5 deletions lib/acts_as_versioned.rb
Expand Up @@ -159,7 +159,8 @@ def acts_as_versioned(options = {}, &extension)
send :include, ActiveRecord::Acts::Versioned::ActMethods

cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
:version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_columns
:version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
:version_association_options

# legacy
alias_method :non_versioned_fields, :non_versioned_columns
Expand All @@ -181,6 +182,12 @@ class << self
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]
self.version_association_options = {
:class_name => "#{self.to_s}::#{versioned_class_name}",
:foreign_key => "#{versioned_foreign_key}",
:order => 'version',
:dependent => :delete_all
}.merge(options[:association_options] || {})

if block_given?
extension_module_name = "#{versioned_class_name}Extension"
Expand All @@ -192,10 +199,7 @@ class << self
end

class_eval do
has_many :versions,
:class_name => "#{self.to_s}::#{versioned_class_name}",
:foreign_key => "#{versioned_foreign_key}",
:order => 'version', :dependent => :delete_all
has_many :versions, version_association_options
before_save :set_new_version
after_create :save_version_on_create
after_update :save_version
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/widget.rb
@@ -1,4 +1,6 @@
class Widget < ActiveRecord::Base
acts_as_versioned :sequence_name => 'widgets_seq'
acts_as_versioned :sequence_name => 'widgets_seq', :association_options => {
:dependent => nil, :order => 'version desc'
}
non_versioned_columns << 'foo'
end
20 changes: 20 additions & 0 deletions test/versioned_test.rb
Expand Up @@ -274,6 +274,26 @@ def test_referential_integrity
assert_equal 0, Page.count
assert_equal 0, Page::Version.count
end

def test_association_options
association = Page.reflect_on_association(:versions)
options = association.options
assert_equal :delete_all, options[:dependent]
assert_equal 'version', options[:order]

association = Widget.reflect_on_association(:versions)
options = association.options
assert_nil options[:dependent]
assert_equal 'version desc', options[:order]
assert_equal 'widget_id', options[:foreign_key]

widget = Widget.create :name => 'new widget'
assert_equal 1, Widget.count
assert_equal 1, Widget.versioned_class.count
widget.destroy
assert_equal 0, Widget.count
assert_equal 1, Widget.versioned_class.count
end

def test_versioned_records_should_belong_to_parent
page = pages(:welcome)
Expand Down

0 comments on commit 51b7540

Please sign in to comment.