edouard-version_fu 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +24 -0
- data/README.rdoc +113 -0
- data/Rakefile +30 -0
- data/VERSION +1 -0
- data/init.rb +2 -0
- data/lib/version_fu.rb +2 -0
- data/lib/version_fu/version_fu.rb +109 -0
- data/test/db/database.yml +18 -0
- data/test/db/schema.rb +37 -0
- data/test/fixtures/author_versions.yml +27 -0
- data/test/fixtures/authors.yml +17 -0
- data/test/fixtures/page_versions.yml +19 -0
- data/test/fixtures/pages.yml +10 -0
- data/test/models/author.rb +16 -0
- data/test/models/page.rb +8 -0
- data/test/test_helper.rb +26 -0
- data/test/version_fu_test.rb +134 -0
- metadata +74 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
== version_fu
|
2
|
+
Copyright (c) 2008-9 Jordan McKible
|
3
|
+
|
4
|
+
== acts_as_versioned
|
5
|
+
Copyright (c) 2005 Rick Olson
|
6
|
+
====================================================================
|
7
|
+
|
8
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
9
|
+
this software and associated documentation files (the "Software"), to deal in
|
10
|
+
the Software without restriction, including without limitation the rights to
|
11
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
12
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
13
|
+
so, subject to the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be included in all
|
16
|
+
copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
19
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
20
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
21
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
22
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
23
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24
|
+
SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
= version_fu
|
2
|
+
|
3
|
+
version_fu is a ActveRecord versioning plugin that that is based on the dirty attribute checking introduced in Rails 2.1. It has been updated for compatibility with Rails 3.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install version_fu
|
8
|
+
|
9
|
+
If you're using Rails 3, add it to your Gemfile
|
10
|
+
|
11
|
+
gem 'version_fu'
|
12
|
+
|
13
|
+
== Usage
|
14
|
+
|
15
|
+
Let's say I have a pages table:
|
16
|
+
|
17
|
+
class Page < ActiveRecord::Base
|
18
|
+
# attributes: id, type, title, body, created_at, updated_at, creator_id, author_id
|
19
|
+
end
|
20
|
+
|
21
|
+
I want to track any changes made. First step will be to make a new page_versions table:
|
22
|
+
|
23
|
+
class CreatePageVersions < ActiveRecord::Migration
|
24
|
+
def self.up
|
25
|
+
create_table :page_versions do |t|
|
26
|
+
t.integer :page_id, :version, :author_id
|
27
|
+
t.string :title
|
28
|
+
t.text :body
|
29
|
+
t.timestamps
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.down
|
34
|
+
drop_table :page_versions
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
In this case, the author_id column represents the last person to edit the page. We want to track this attribute with every version. However, the creator_id is the person who created the page. The will never change, so it's not part of the versioned table.
|
39
|
+
|
40
|
+
Don't forget to add a version column to your pages table. Have it default to 1 just to be safe (although the plugin should account for this):
|
41
|
+
|
42
|
+
class AddVersionToPages < ActiveRecord::Migration
|
43
|
+
def self.up
|
44
|
+
add_column :pages, :version, :integer, :default=>1
|
45
|
+
end
|
46
|
+
def self.down
|
47
|
+
remove_column :pages, :version
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
Of course if you're adding this plugin to a table with existing data, you'll probably want to instantiate some initial versions to start with.
|
52
|
+
|
53
|
+
Alright, so now that the database tables are in place, we can fire up version_fu. It's quite simple:
|
54
|
+
|
55
|
+
class Page < ActiveRecord::Base
|
56
|
+
version_fu
|
57
|
+
end
|
58
|
+
|
59
|
+
Thats it.
|
60
|
+
|
61
|
+
|
62
|
+
== Configuration
|
63
|
+
|
64
|
+
You can pass a few configuration options if need be. If you stick with the defaults above, you can skip all this.
|
65
|
+
|
66
|
+
class Page < ActiveRecord::Base
|
67
|
+
version_fu :class_name=>'Version', :foreign_key=>'page_id', :table_name=>'page_versions', :version_column=>'version'
|
68
|
+
end
|
69
|
+
|
70
|
+
* :class_name - The name of the versioned class. It will be a submodule of the versioning class - e.g. Page::Version
|
71
|
+
|
72
|
+
* :foreign_key - The column in the versioned table associated with the versioning class
|
73
|
+
|
74
|
+
* :table_name - The name of the versioned table
|
75
|
+
|
76
|
+
* :version_column - The name of the version column
|
77
|
+
|
78
|
+
|
79
|
+
== Extensions
|
80
|
+
|
81
|
+
Now that you've got some versions, it would be nice to use ActiveRecord associations on it. For example, Page.first.versions.latest.author wouldn't currently work because the Page::Version class doesn't know about the author method. The version_fu call does all you to pass a block which is executed by the versioned class. There is just one gotcha for associations:
|
82
|
+
|
83
|
+
class Page < ActiveRecord::Base
|
84
|
+
version_fu do
|
85
|
+
belongs_to :author, :class_name=>'::Author'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
Don't forget the class name, or you'll get a warning
|
90
|
+
|
91
|
+
== When to Version
|
92
|
+
|
93
|
+
By default a new version will be saved whenever a versioned column is changed. However, you can control this at a more fine grained level. Just override the create_new_version? method. For example, let's say you only want to save a new version if both the page title and body changed. Taking advantage of the dirty attribute methods, you could do something like this:
|
94
|
+
|
95
|
+
class Page < ActiveRecord::Base
|
96
|
+
version_fu do
|
97
|
+
belongs_to :author, :class_name=>'::Author'
|
98
|
+
end
|
99
|
+
def create_new_version?
|
100
|
+
title_changed? && body_changed?
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
== Author
|
106
|
+
|
107
|
+
* version_fu was created by Jordan McKible http://jordan.mckible.com
|
108
|
+
|
109
|
+
* Available on GitHub at http://github.com/jmckible/version_fu
|
110
|
+
|
111
|
+
* Available on RubyGems.org at http://rubygems.org/gems/version_fu
|
112
|
+
|
113
|
+
* acts_as_versioned by Rick Olson http://github.com/technoweenie/acts_as_versioned/tree/master
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default=>:test
|
8
|
+
|
9
|
+
desc 'Test the version_fu plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.libs << 'test/models'
|
14
|
+
t.pattern = 'test/**/*_test.rb'
|
15
|
+
t.verbose = true
|
16
|
+
end
|
17
|
+
|
18
|
+
begin
|
19
|
+
require 'jeweler'
|
20
|
+
Jeweler::Tasks.new do |gemspec|
|
21
|
+
gemspec.name = "nbudin-version_fu"
|
22
|
+
gemspec.summary = "Gemified version of the version_fu plugin, tracking changes from revo and jmckible."
|
23
|
+
gemspec.description = "version_fu is a ActveRecord versioning gem that takes advantage of the new dirty attribute checking available in Rails 2.1. Previous solutions like Rick Olson's acts_as_versioned are no long compatible with Rails."
|
24
|
+
gemspec.email = ""
|
25
|
+
gemspec.homepage = "http://github.com/nbudin/version_fu"
|
26
|
+
gemspec.authors = ["Jordan McKible"]
|
27
|
+
end
|
28
|
+
rescue LoadError
|
29
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
30
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.1
|
data/init.rb
ADDED
data/lib/version_fu.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
module VersionFu
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def version_fu(options={}, &block)
|
8
|
+
return if self.included_modules.include? VersionFu::InstanceMethods
|
9
|
+
__send__ :include, VersionFu::InstanceMethods
|
10
|
+
|
11
|
+
cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name,
|
12
|
+
:version_column, :versioned_columns
|
13
|
+
|
14
|
+
self.versioned_class_name = options[:class_name] || 'Version'
|
15
|
+
self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
|
16
|
+
self.versioned_table_name = options[:table_name] || "#{table_name_prefix}#{base_class.name.demodulize.underscore}_versions#{table_name_suffix}"
|
17
|
+
self.version_column = options[:version_column] || 'version'
|
18
|
+
|
19
|
+
# Setup versions association
|
20
|
+
class_eval do
|
21
|
+
has_many :versions, :class_name => "#{self.to_s}::#{versioned_class_name}",
|
22
|
+
:foreign_key => versioned_foreign_key,
|
23
|
+
:dependent => :destroy do
|
24
|
+
def latest
|
25
|
+
find :first, :order=>'version desc'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
before_save :check_for_new_version
|
30
|
+
end
|
31
|
+
|
32
|
+
# Versioned Model
|
33
|
+
const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
|
34
|
+
# find first version before the given version
|
35
|
+
def self.before(version)
|
36
|
+
find :first, :order => 'version desc',
|
37
|
+
:conditions => ["#{original_class.versioned_foreign_key} = ? and version < ?", version.send(original_class.versioned_foreign_key), version.version]
|
38
|
+
end
|
39
|
+
|
40
|
+
# find first version after the given version.
|
41
|
+
def self.after(version)
|
42
|
+
find :first, :order => 'version',
|
43
|
+
:conditions => ["#{original_class.versioned_foreign_key} = ? and version > ?", version.send(original_class.versioned_foreign_key), version.version]
|
44
|
+
end
|
45
|
+
|
46
|
+
def previous
|
47
|
+
self.class.before(self)
|
48
|
+
end
|
49
|
+
|
50
|
+
def next
|
51
|
+
self.class.after(self)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Housekeeping on versioned class
|
56
|
+
versioned_class.cattr_accessor :original_class
|
57
|
+
versioned_class.original_class = self
|
58
|
+
versioned_class.table_name = versioned_table_name
|
59
|
+
|
60
|
+
# Version parent association
|
61
|
+
versioned_class.belongs_to self.to_s.demodulize.underscore.to_sym,
|
62
|
+
:class_name => "::#{self.to_s}",
|
63
|
+
:foreign_key => versioned_foreign_key
|
64
|
+
|
65
|
+
# Block extension
|
66
|
+
versioned_class.class_eval &block if block_given?
|
67
|
+
|
68
|
+
if self.versioned_class.table_exists?
|
69
|
+
# Finally setup which columns to version
|
70
|
+
self.versioned_columns = versioned_class.new.attributes.keys -
|
71
|
+
[versioned_class.primary_key, versioned_foreign_key, version_column, 'created_at', 'updated_at']
|
72
|
+
else
|
73
|
+
ActiveRecord::Base.logger.warn "Version Table not found"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def versioned_class
|
78
|
+
const_get versioned_class_name
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
module InstanceMethods
|
84
|
+
def find_version(number)
|
85
|
+
versions.find :first, :conditions=>{:version=>number}
|
86
|
+
end
|
87
|
+
|
88
|
+
def check_for_new_version
|
89
|
+
instatiate_revision if create_new_version?
|
90
|
+
true # Never halt save
|
91
|
+
end
|
92
|
+
|
93
|
+
# This the method to override if you want to have more control over when to version
|
94
|
+
def create_new_version?
|
95
|
+
# Any versioned column changed?
|
96
|
+
self.class.versioned_columns.detect {|a| __send__ "#{a}_changed?"}
|
97
|
+
end
|
98
|
+
|
99
|
+
def instatiate_revision
|
100
|
+
new_version = versions.build
|
101
|
+
versioned_columns.each do |attribute|
|
102
|
+
new_version.__send__ "#{attribute}=", __send__(attribute)
|
103
|
+
end
|
104
|
+
version_number = new_record? ? 1 : version + 1
|
105
|
+
new_version.version = version_number
|
106
|
+
self.version = version_number
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
sqlite:
|
2
|
+
:adapter: sqlite
|
3
|
+
:database: version_fu_plugin.sqlite.db
|
4
|
+
sqlite3:
|
5
|
+
:adapter: sqlite3
|
6
|
+
:database: db/version_fu_plugin.sqlite3.db
|
7
|
+
postgresql:
|
8
|
+
:adapter: postgresql
|
9
|
+
:username: postgres
|
10
|
+
:password: postgres
|
11
|
+
:database: version_fu_plugin_test
|
12
|
+
:min_messages: ERROR
|
13
|
+
mysql:
|
14
|
+
:adapter: mysql
|
15
|
+
:host: localhost
|
16
|
+
:username: root
|
17
|
+
:password:
|
18
|
+
:database: version_fu_plugin_test
|
data/test/db/schema.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version=>0) do
|
2
|
+
|
3
|
+
create_table :authors, :force=>true do |t|
|
4
|
+
t.column :version, :integer
|
5
|
+
t.column :first_name, :string, :limit=>255
|
6
|
+
t.column :last_name, :string, :limit=>255
|
7
|
+
end
|
8
|
+
|
9
|
+
create_table :author_versions, :force=>true do |t|
|
10
|
+
t.column :author_id, :integer
|
11
|
+
t.column :version, :integer
|
12
|
+
t.column :first_name, :string, :limit=>255
|
13
|
+
t.column :last_name, :string, :limit=>255
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :pages, :force=>true do |t|
|
17
|
+
t.column :type, :string
|
18
|
+
t.column :version, :integer
|
19
|
+
t.column :title, :string, :limit=>255
|
20
|
+
t.column :body, :text
|
21
|
+
t.column :created_at, :datetime
|
22
|
+
t.column :updated_at, :datetime
|
23
|
+
t.column :creator_id, :integer
|
24
|
+
t.column :author_id, :integer
|
25
|
+
end
|
26
|
+
|
27
|
+
create_table :page_versions, :force=>true do |t|
|
28
|
+
t.column :page_id, :integer
|
29
|
+
t.column :version, :integer
|
30
|
+
t.column :title, :string, :limit=>255
|
31
|
+
t.column :body, :text
|
32
|
+
t.column :created_at, :datetime
|
33
|
+
t.column :updated_at, :datetime
|
34
|
+
t.column :author_id, :integer
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
larry_1:
|
2
|
+
id: 1
|
3
|
+
version: 1
|
4
|
+
author_id: 1
|
5
|
+
first_name: Larry
|
6
|
+
last_name: Appleton
|
7
|
+
|
8
|
+
sara_1:
|
9
|
+
id: 2
|
10
|
+
version: 1
|
11
|
+
author_id: 2
|
12
|
+
first_name: Sara
|
13
|
+
last_name: Maiden
|
14
|
+
|
15
|
+
sara_2:
|
16
|
+
id: 4
|
17
|
+
version: 2
|
18
|
+
author_id: 2
|
19
|
+
first_name: Sara
|
20
|
+
last_name: Smiles
|
21
|
+
|
22
|
+
peter_1:
|
23
|
+
id: 3
|
24
|
+
version: 1
|
25
|
+
author_id: 3
|
26
|
+
first_name: Peter
|
27
|
+
last_name: Parker
|
@@ -0,0 +1,19 @@
|
|
1
|
+
welcome_1:
|
2
|
+
id: 1
|
3
|
+
page_id: 1
|
4
|
+
version: 1
|
5
|
+
title: Welcome
|
6
|
+
body: Initial body
|
7
|
+
created_at: <%= 1.hour.ago.to_s :db %>
|
8
|
+
updated_at: <%= 1.hour.ago.to_s :db %>
|
9
|
+
author_id: 1
|
10
|
+
|
11
|
+
welcome_2:
|
12
|
+
id: 2
|
13
|
+
page_id: 1
|
14
|
+
version: 2
|
15
|
+
title: Version 2
|
16
|
+
body: two
|
17
|
+
created_at: <%= 30.minutes.ago.to_s :db %>
|
18
|
+
updated_at: <%= 30.minutes.ago.to_s :db %>
|
19
|
+
author_id: 2
|
data/test/models/page.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rubygems'
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
|
4
|
+
|
5
|
+
require 'active_record'
|
6
|
+
require 'active_record/fixtures'
|
7
|
+
|
8
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/db/database.yml'))
|
9
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
10
|
+
ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']}
|
11
|
+
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
|
12
|
+
|
13
|
+
load(File.dirname(__FILE__) + '/db/schema.rb')
|
14
|
+
|
15
|
+
class ActiveSupport::TestCase
|
16
|
+
include ActiveRecord::TestFixtures
|
17
|
+
|
18
|
+
self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
|
19
|
+
|
20
|
+
self.use_transactional_fixtures = true
|
21
|
+
self.use_instantiated_fixtures = false
|
22
|
+
|
23
|
+
fixtures :all
|
24
|
+
set_fixture_class :page_versions => Page::Version
|
25
|
+
set_fixture_class :author_versions => Author::Version
|
26
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class VersionFuTest < ActiveSupport::TestCase
|
4
|
+
#############################################################################
|
5
|
+
# A S S O C I A T I O N S #
|
6
|
+
#############################################################################
|
7
|
+
test 'parent has many versions' do
|
8
|
+
assert_equal page_versions(:welcome_1, :welcome_2), pages(:welcome).versions
|
9
|
+
end
|
10
|
+
|
11
|
+
test 'version belongs to parent' do
|
12
|
+
assert_equal pages(:welcome), page_versions(:welcome_1).page
|
13
|
+
end
|
14
|
+
|
15
|
+
#############################################################################
|
16
|
+
# A T T R I B U T E S #
|
17
|
+
#############################################################################
|
18
|
+
test 'version proper columns' do
|
19
|
+
assert_equal ['title', 'body', 'author_id'], Page.new.versioned_columns
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'do not version non-existing columns' do
|
23
|
+
assert !Page.new.versioned_columns.include?(:creator_id)
|
24
|
+
end
|
25
|
+
|
26
|
+
#############################################################################
|
27
|
+
# C R E A T E #
|
28
|
+
#############################################################################
|
29
|
+
test 'save version on create' do
|
30
|
+
old_count = Page.count
|
31
|
+
old_version_count = Page::Version.count
|
32
|
+
page = Page.create :title=>'New', :body=>'body', :creator=>authors(:larry), :author=>authors(:larry)
|
33
|
+
assert_equal old_count + 1, Page.count
|
34
|
+
assert_equal old_version_count + 1, Page::Version.count
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'wire up associations on create' do
|
38
|
+
page = Page.create :title=>'New', :body=>'body', :creator=>authors(:larry), :author=>authors(:larry)
|
39
|
+
assert_equal Page::Version.find(:first, :order=>'id desc'), page.versions.first
|
40
|
+
end
|
41
|
+
|
42
|
+
test 'begin version numbering at 1' do
|
43
|
+
page = Page.create :title=>'New', :body=>'body', :creator=>authors(:larry), :author=>authors(:larry)
|
44
|
+
assert_equal 1, page.version
|
45
|
+
assert_equal 1, page.versions.first.version
|
46
|
+
end
|
47
|
+
|
48
|
+
test 'assign attributes on create' do
|
49
|
+
page = Page.create :title=>'New', :body=>'body', :creator=>authors(:larry), :author=>authors(:larry)
|
50
|
+
version = page.versions.first
|
51
|
+
assert_equal 'New', version.title
|
52
|
+
assert_equal 'body', version.body
|
53
|
+
assert_equal authors(:larry).id, version.author_id
|
54
|
+
end
|
55
|
+
|
56
|
+
#############################################################################
|
57
|
+
# U P D A T E #
|
58
|
+
#############################################################################
|
59
|
+
test 'save version on update' do
|
60
|
+
old_count = Page::Version.count
|
61
|
+
page = pages(:welcome)
|
62
|
+
page.update_attributes :title=>'New title', :body=>'new body', :author=>authors(:sara)
|
63
|
+
assert_equal old_count + 1, Page::Version.count
|
64
|
+
end
|
65
|
+
|
66
|
+
test 'increment verson number' do
|
67
|
+
page = pages(:welcome)
|
68
|
+
old_count = page.version
|
69
|
+
page.update_attributes :title=>'New title', :body=>'new body', :author=>authors(:sara)
|
70
|
+
assert_equal old_count + 1, page.reload.version
|
71
|
+
end
|
72
|
+
|
73
|
+
test 'update version attributes' do
|
74
|
+
page = pages(:welcome)
|
75
|
+
page.update_attributes :title=>'Latest', :body=>'newest', :author=>authors(:peter)
|
76
|
+
version = page.reload.versions.latest
|
77
|
+
assert_equal 'Latest', version.title
|
78
|
+
assert_equal 'newest', version.body
|
79
|
+
assert_equal authors(:peter).id, version.author_id
|
80
|
+
end
|
81
|
+
|
82
|
+
#############################################################################
|
83
|
+
# S K I P V E R S I O N I N G #
|
84
|
+
#############################################################################
|
85
|
+
test 'do not create version if nothing changed' do
|
86
|
+
old_count = Page::Version.count
|
87
|
+
pages(:welcome).save
|
88
|
+
assert_equal old_count, Page::Version.count
|
89
|
+
end
|
90
|
+
|
91
|
+
test 'do not create version if untracked attribute changed' do
|
92
|
+
old_count = Page::Version.count
|
93
|
+
pages(:welcome).update_attributes :author=>authors(:sara)
|
94
|
+
assert_equal old_count, Page::Version.count
|
95
|
+
end
|
96
|
+
|
97
|
+
test 'do not create version if custom version check' do
|
98
|
+
old_count = Author::Version.count
|
99
|
+
authors(:larry).update_attributes :last_name=>'Lessig'
|
100
|
+
assert_equal old_count, Author::Version.count
|
101
|
+
end
|
102
|
+
|
103
|
+
test 'still save if no new version with custom version check' do
|
104
|
+
authors(:larry).update_attributes :last_name=>'Lessig'
|
105
|
+
assert_equal 'Lessig', authors(:larry).reload.last_name
|
106
|
+
end
|
107
|
+
|
108
|
+
#############################################################################
|
109
|
+
# F I N D #
|
110
|
+
#############################################################################
|
111
|
+
test 'find version given number' do
|
112
|
+
assert_equal page_versions(:welcome_1), pages(:welcome).find_version(1)
|
113
|
+
assert_equal page_versions(:welcome_2), pages(:welcome).find_version(2)
|
114
|
+
end
|
115
|
+
|
116
|
+
test 'find latest version' do
|
117
|
+
assert_equal page_versions(:welcome_2), pages(:welcome).versions.latest
|
118
|
+
end
|
119
|
+
|
120
|
+
test 'find previous version' do
|
121
|
+
assert_equal page_versions(:welcome_1), page_versions(:welcome_2).previous
|
122
|
+
end
|
123
|
+
|
124
|
+
test 'find next version' do
|
125
|
+
assert_equal page_versions(:welcome_2), page_versions(:welcome_1).next
|
126
|
+
end
|
127
|
+
|
128
|
+
#############################################################################
|
129
|
+
# B L O C K E X T E N S I O N #
|
130
|
+
#############################################################################
|
131
|
+
test 'take a block containing ActiveRecord extension' do
|
132
|
+
assert_equal authors(:larry), page_versions(:welcome_1).author
|
133
|
+
end
|
134
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: edouard-version_fu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jordan McKible
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2010-09-01 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: version_fu helps version your ActiveRecord models. It is based on Rick
|
15
|
+
Olson's acts_as_versioned and is compatible with Rails 3.
|
16
|
+
email: ''
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files:
|
20
|
+
- README.rdoc
|
21
|
+
files:
|
22
|
+
- init.rb
|
23
|
+
- MIT-LICENSE
|
24
|
+
- Rakefile
|
25
|
+
- README.rdoc
|
26
|
+
- VERSION
|
27
|
+
- lib/version_fu.rb
|
28
|
+
- lib/version_fu/version_fu.rb
|
29
|
+
- test/db/database.yml
|
30
|
+
- test/db/schema.rb
|
31
|
+
- test/fixtures/author_versions.yml
|
32
|
+
- test/fixtures/authors.yml
|
33
|
+
- test/fixtures/page_versions.yml
|
34
|
+
- test/fixtures/pages.yml
|
35
|
+
- test/models/author.rb
|
36
|
+
- test/models/page.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
- test/version_fu_test.rb
|
39
|
+
homepage: ''
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --charset=UTF-8
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.24
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Gemified version of the version_fu plugin.
|
64
|
+
test_files:
|
65
|
+
- test/db/database.yml
|
66
|
+
- test/db/schema.rb
|
67
|
+
- test/fixtures/author_versions.yml
|
68
|
+
- test/fixtures/authors.yml
|
69
|
+
- test/fixtures/page_versions.yml
|
70
|
+
- test/fixtures/pages.yml
|
71
|
+
- test/models/author.rb
|
72
|
+
- test/models/page.rb
|
73
|
+
- test/test_helper.rb
|
74
|
+
- test/version_fu_test.rb
|