public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
(acts_as_versioned) Allow customization of #versions association options 
[Dan Peterson]

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2330 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Thu Oct 05 07:01:24 -0700 2006
commit  5afada7a089c3be601c83b5f89eadda4ee4aa8fb
tree    39ca93606ae904edcf901d8e0004f97579da59ca
parent  1d7f42d81f9f7769807925f692ad2eb19805fdf9
...
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
0
@@ -1,3 +1,7 @@
0
+*SVN* (version numbers are overrated)
0
+
0
+* (5 Oct 2006) Allow customization of #versions association options [Dan Peterson]
0
+
0
 *0.5.1*
0
 
0
 * (8 Aug 2006) Versioned models now belong to the unversioned model. @article_version.article.class => Article [Aslak Hellesoy]
...
159
160
161
162
 
 
163
164
165
...
181
182
183
 
 
 
 
 
 
184
185
186
...
192
193
194
195
196
197
198
 
199
200
201
...
159
160
161
 
162
163
164
165
166
...
182
183
184
185
186
187
188
189
190
191
192
193
...
199
200
201
 
 
 
 
202
203
204
205
0
@@ -159,7 +159,8 @@ module ActiveRecord #:nodoc:
0
           send :include, ActiveRecord::Acts::Versioned::ActMethods
0
           
0
           cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
0
- :version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_columns
0
+ :version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
0
+ :version_association_options
0
             
0
           # legacy
0
           alias_method :non_versioned_fields, :non_versioned_columns
0
@@ -181,6 +182,12 @@ module ActiveRecord #:nodoc:
0
           self.max_version_limit = options[:limit].to_i
0
           self.version_condition = options[:if] || true
0
           self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
0
+ self.version_association_options = {
0
+ :class_name => "#{self.to_s}::#{versioned_class_name}",
0
+ :foreign_key => "#{versioned_foreign_key}",
0
+ :order => 'version',
0
+ :dependent => :delete_all
0
+ }.merge(options[:association_options] || {})
0
 
0
           if block_given?
0
             extension_module_name = "#{versioned_class_name}Extension"
0
@@ -192,10 +199,7 @@ module ActiveRecord #:nodoc:
0
           end
0
 
0
           class_eval do
0
- has_many :versions,
0
- :class_name => "#{self.to_s}::#{versioned_class_name}",
0
- :foreign_key => "#{versioned_foreign_key}",
0
- :order => 'version', :dependent => :delete_all
0
+ has_many :versions, version_association_options
0
             before_save :set_new_version
0
             after_create :save_version_on_create
0
             after_update :save_version
...
1
2
 
 
 
3
4
5
...
1
 
2
3
4
5
6
7
0
@@ -1,4 +1,6 @@
0
 class Widget < ActiveRecord::Base
0
- acts_as_versioned :sequence_name => 'widgets_seq'
0
+ acts_as_versioned :sequence_name => 'widgets_seq', :association_options => {
0
+ :dependent => nil, :order => 'version desc'
0
+ }
0
   non_versioned_columns << 'foo'
0
 end
0
\ No newline at end of file
...
274
275
276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
278
279
...
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
0
@@ -274,6 +274,26 @@ class VersionedTest < Test::Unit::TestCase
0
     assert_equal 0, Page.count
0
     assert_equal 0, Page::Version.count
0
   end
0
+
0
+ def test_association_options
0
+ association = Page.reflect_on_association(:versions)
0
+ options = association.options
0
+ assert_equal :delete_all, options[:dependent]
0
+ assert_equal 'version', options[:order]
0
+
0
+ association = Widget.reflect_on_association(:versions)
0
+ options = association.options
0
+ assert_nil options[:dependent]
0
+ assert_equal 'version desc', options[:order]
0
+ assert_equal 'widget_id', options[:foreign_key]
0
+
0
+ widget = Widget.create :name => 'new widget'
0
+ assert_equal 1, Widget.count
0
+ assert_equal 1, Widget.versioned_class.count
0
+ widget.destroy
0
+ assert_equal 0, Widget.count
0
+ assert_equal 1, Widget.versioned_class.count
0
+ end
0
 
0
   def test_versioned_records_should_belong_to_parent
0
     page = pages(:welcome)

Comments

    No one has commented yet.