public
Description: Dirty ActiveRecord Versioning (update of acts_as_versioned)
Homepage: http://tuples.us/2008/05/03/lazily-announcing-version_fu/
Clone URL: git://github.com/jmckible/version_fu.git
Setup test infrastructure.  Versioned class associations.
jmckible (author)
Sun Apr 27 17:39:29 -0700 2008
commit  5a1bedc3e323c8c297738f0f8a7f7841259680e9
tree    edc6bda1104fc595f8c273ccd345caae62621a91
parent  4204d64ce2ad151f969753fe7dafdbf3a12f4735
...
1
2
 
 
3
...
 
1
2
3
4
0
@@ -1 +1,2 @@
0
-test/debug.log
0
\ No newline at end of file
0
+test/*.log
0
+*.db
0
\ No newline at end of file
...
1
2
 
 
 
3
4
5
...
1
2
3
4
5
6
7
8
0
@@ -1,5 +1,8 @@
0
 == version_fu
0
 Copyright (c) 2008 Jordan McKible
0
+
0
+== acts_as_versioned
0
+Copyright (c) 2005 Rick Olson
0
 ====================================================================
0
 
0
 Permission is hereby granted, free of charge, to any person obtaining a copy of
0
...
3
4
5
 
6
...
3
4
5
6
7
0
@@ -3,3 +3,4 @@ version_fu
0
 
0
 version_fu is a ActveRecord versioning plugin that takes advantage of the new dirty attribute checking available in Edge Rails (to be released in version 2.1). Previous solutions like Rick Olsen's acts_as_versioned are no long compatible with Rails.
0
 
0
+This is definitely a work in progress. I'm currently just transitioning the elements of acts_as_versioned I use in one project.
0
\ No newline at end of file
...
1
2
 
3
...
 
1
2
3
0
@@ -1 +1 @@
0
-version_fu is being developed against Edge Rails, so it needs to load ../vendor/rails
0
\ No newline at end of file
0
+version_fu is being developed against Edge Rails, so it needs to be available vendor/rails
0
\ No newline at end of file
...
1
2
3
 
4
5
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
8
9
 
 
10
11
12
13
 
 
14
15
16
...
1
2
 
3
4
 
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
 
44
45
46
47
48
 
49
50
51
52
53
0
@@ -1,16 +1,53 @@
0
 module VersionFu
0
   def self.included(base)
0
- base.extend(ClassMethods)
0
+ base.extend ClassMethods
0
   end
0
-
0
+
0
   module ClassMethods
0
+ def version_fu
0
+ return if self.included_modules.include? VersionFu::InstanceMethods
0
+ __send__ :include, VersionFu::InstanceMethods
0
+
0
+ cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column,
0
+ :version_column, :non_versioned_columns
0
+
0
+ send :attr_accessor, :aav_changed_attributes
0
+
0
+ self.versioned_class_name = "Version"
0
+ self.versioned_foreign_key = self.to_s.foreign_key
0
+ self.versioned_table_name = "#{table_name_prefix}#{base_class.name.demodulize.underscore}_versions#{table_name_suffix}"
0
+ self.versioned_inheritance_column = "versioned_#{inheritance_column}"
0
+ self.version_column = 'version'
0
+ self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column]
0
+
0
+ # Setup versions association
0
+ class_eval do
0
+ has_many :versions, :class_name => "#{self.to_s}::#{versioned_class_name}",
0
+ :foreign_key => versioned_foreign_key,
0
+ :order => 'version',
0
+ :dependent => :delete_all
0
+ end
0
+
0
+ # Versioned Model
0
+ const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
0
+ def self.reloadable? ; false ; end
0
+ end
0
+
0
+ versioned_class.cattr_accessor :original_class
0
+ versioned_class.original_class = self
0
+ versioned_class.set_table_name versioned_table_name
0
+ versioned_class.belongs_to self.to_s.demodulize.underscore.to_sym,
0
+ :class_name => "::#{self.to_s}",
0
+ :foreign_key => versioned_foreign_key
0
+ end
0
     
0
- def version_fu(options={})
0
-
0
+ def versioned_class
0
+ const_get versioned_class_name
0
     end
0
     
0
   end
0
-
0
+
0
+
0
   module InstanceMethods
0
   end
0
   
...
13
14
15
16
 
17
18
19
...
13
14
15
 
16
17
18
19
0
@@ -13,6 +13,6 @@ postgresql:
0
 mysql:
0
   :adapter: mysql
0
   :host: localhost
0
- :username: rails
0
+ :username: root
0
   :password:
0
   :database: version_fu_plugin_test
0
\ No newline at end of file
...
1
 
 
2
3
4
...
 
1
2
3
4
5
0
@@ -1,4 +1,5 @@
0
-welcome:
0
+welcome_1:
0
+ id: 1
0
   page_id: 1
0
   version: 1
0
   title: Welcome
...
1
2
3
4
5
...
7
8
9
10
 
 
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
25
26
 
27
28
29
30
31
32
33
 
34
...
 
 
1
2
3
...
5
6
7
 
8
9
10
11
12
13
 
 
 
 
 
 
 
 
 
 
14
15
 
16
17
 
 
18
 
 
 
19
20
0
@@ -1,5 +1,3 @@
0
-$:.unshift(File.dirname(__FILE__) + '/../lib')
0
-
0
 require 'test/unit'
0
 require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
0
 require 'rubygems'
0
@@ -7,27 +5,15 @@ require 'active_record/fixtures'
0
 
0
 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
0
 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
0
-ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
0
+ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
0
+ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
0
 
0
 load(File.dirname(__FILE__) + "/schema.rb")
0
 
0
 Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
0
-$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
0
-
0
-class Test::Unit::TestCase #:nodoc:
0
- def create_fixtures(*table_names)
0
- if block_given?
0
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
0
- else
0
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
0
- end
0
- end
0
+$:.unshift(Test::Unit::TestCase.fixture_path)
0
 
0
- # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
0
+class Test::Unit::TestCase
0
   self.use_transactional_fixtures = true
0
-
0
- # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
0
   self.use_instantiated_fixtures = false
0
-
0
- # Add more helper methods to be used by all tests here...
0
-end
0
+end
0
\ No newline at end of file
...
1
2
3
4
5
 
6
7
8
 
 
 
 
 
9
10
11
 
 
12
 
13
14
...
1
 
 
 
 
2
3
4
5
6
7
8
9
10
11
 
 
12
13
14
15
16
17
0
@@ -1,13 +1,16 @@
0
 require File.join(File.dirname(__FILE__), 'test_helper')
0
-
0
-class Page < ActiveRecord::Base
0
- version_fu
0
-end
0
+require File.join(File.dirname(__FILE__), 'fixtures/page')
0
 
0
 class VersionFuTest < Test::Unit::TestCase
0
   fixtures :pages, :page_versions
0
+ set_fixture_class :page_versions => Page::Version
0
+
0
+ def test_parent_has_many_version
0
+ assert_equal pages(:welcome).versions, [page_versions(:welcome_1)]
0
+ end
0
   
0
- def test_true
0
- assert true
0
+ def test_version_belongs_to_parent
0
+ assert_equal page_versions(:welcome_1).page, pages(:welcome)
0
   end
0
+
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.