rich-acts_as_revisable 1.0.2 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/generators/revisable_migration/revisable_migration_generator.rb +14 -2
- data/generators/revisable_migration/templates/migration.rb +6 -5
- data/lib/acts_as_revisable/acts/common.rb +2 -2
- data/lib/acts_as_revisable/acts/revisable.rb +4 -4
- data/lib/acts_as_revisable/acts/revision.rb +2 -2
- data/lib/acts_as_revisable/version.rb +1 -1
- data/spec/general_spec.rb +16 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/sti_spec.rb +42 -0
- metadata +5 -3
@@ -1,9 +1,21 @@
|
|
1
1
|
class RevisableMigrationGenerator < Rails::Generator::NamedBase
|
2
2
|
def manifest
|
3
3
|
record do |m|
|
4
|
-
revisable_columns = [
|
4
|
+
revisable_columns = [
|
5
|
+
["revisable_original_id", "integer"],
|
6
|
+
["revisable_branched_from_id", "integer"],
|
7
|
+
["revisable_number", "integer", 0],
|
8
|
+
["revisable_name", "string"],
|
9
|
+
["revisable_type", "string"],
|
10
|
+
["revisable_current_at", "datetime"],
|
11
|
+
["revisable_revised_at", "datetime"],
|
12
|
+
["revisable_deleted_at", "datetime"],
|
13
|
+
["revisable_is_current", "boolean", 1]
|
14
|
+
]
|
5
15
|
|
6
|
-
m.migration_template 'migration.rb', 'db/migrate',
|
16
|
+
m.migration_template 'migration.rb', 'db/migrate',
|
17
|
+
:migration_file_name => "make_#{class_name.underscore.pluralize}_revisable",
|
18
|
+
:assigns => { :cols => revisable_columns }
|
7
19
|
end
|
8
20
|
end
|
9
21
|
end
|
@@ -1,13 +1,14 @@
|
|
1
|
-
|
1
|
+
<% table_name = class_name.underscore.pluralize -%>
|
2
|
+
class Make<%= class_name.underscore.pluralize.camelize %>Revisable < ActiveRecord::Migration
|
2
3
|
def self.up
|
3
|
-
<% cols.each do |
|
4
|
-
add_column :<%=
|
4
|
+
<% cols.each do |column_name,column_type,default| -%>
|
5
|
+
add_column :<%= table_name %>, :<%= column_name %>, :<%= column_type %><%= ", :default => #{default}" unless default.blank? %>
|
5
6
|
<% end -%>
|
6
7
|
end
|
7
8
|
|
8
9
|
def self.down
|
9
|
-
<% cols.each do |
|
10
|
-
remove_column :<%=
|
10
|
+
<% cols.each do |column_name,_| -%>
|
11
|
+
remove_column :<%= table_name %>, :<%= column_name %>
|
11
12
|
<% end -%>
|
12
13
|
end
|
13
14
|
end
|
@@ -20,9 +20,9 @@ module WithoutScope
|
|
20
20
|
|
21
21
|
base.instance_eval do
|
22
22
|
define_callbacks :before_branch, :after_branch
|
23
|
-
has_many :branches, (revisable_options.revision_association_options || {}).merge({:class_name => base.
|
23
|
+
has_many :branches, (revisable_options.revision_association_options || {}).merge({:class_name => base.name, :foreign_key => :revisable_branched_from_id})
|
24
24
|
|
25
|
-
belongs_to :branch_source, :class_name => base.
|
25
|
+
belongs_to :branch_source, :class_name => base.name, :foreign_key => :revisable_branched_from_id
|
26
26
|
after_save :execute_blocks_after_save
|
27
27
|
end
|
28
28
|
end
|
@@ -40,11 +40,11 @@ module WithoutScope
|
|
40
40
|
has_many assoc, (revisable_options.revision_association_options || {}).merge({:class_name => revision_class_name, :foreign_key => :revisable_original_id, :order => "#{quoted_table_name}.#{connection.quote_column_name(:revisable_number)} DESC", :dependent => :destroy})
|
41
41
|
end
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
if !Object.const_defined?(base.revision_class_name) && base.revisable_options.generate_revision_class?
|
45
|
-
Object.const_set(base.revision_class_name, Class.new(ActiveRecord::Base)).
|
45
|
+
Object.const_set(base.revision_class_name, Class.new(ActiveRecord::Base)).instance_eval do
|
46
46
|
acts_as_revision
|
47
|
-
end
|
47
|
+
end
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -446,7 +446,7 @@ module WithoutScope
|
|
446
446
|
# Returns the +revision_class_name+ as configured in
|
447
447
|
# +acts_as_revisable+.
|
448
448
|
def revision_class_name #:nodoc:
|
449
|
-
self.revisable_options.revision_class_name || "#{self.
|
449
|
+
self.revisable_options.revision_class_name || "#{self.name}Revision"
|
450
450
|
end
|
451
451
|
|
452
452
|
# Returns the actual +Revision+ class based on the
|
@@ -71,7 +71,7 @@ module WithoutScope
|
|
71
71
|
self[:revisable_current_at] = now + 1.second
|
72
72
|
self[:revisable_is_current] = false
|
73
73
|
self[:revisable_branched_from_id] = current_revision[:revisable_branched_from_id]
|
74
|
-
self[:revisable_type] = current_revision[:type]
|
74
|
+
self[:revisable_type] = current_revision[:type] || current_revision.class.name
|
75
75
|
self[:revisable_number] = (self.class.maximum(:revisable_number, :conditions => {:revisable_original_id => self[:revisable_original_id]}) || 0) + 1
|
76
76
|
end
|
77
77
|
|
@@ -103,7 +103,7 @@ module WithoutScope
|
|
103
103
|
# Returns the +revisable_class_name+ as configured in
|
104
104
|
# +acts_as_revisable+.
|
105
105
|
def revisable_class_name #:nodoc:
|
106
|
-
self.revisable_options.revisable_class_name || self.
|
106
|
+
self.revisable_options.revisable_class_name || self.name.gsub(/Revision/, '')
|
107
107
|
end
|
108
108
|
|
109
109
|
# Returns the actual +Revisable+ class based on the
|
data/spec/general_spec.rb
CHANGED
@@ -7,6 +7,22 @@ describe WithoutScope::ActsAsRevisable do
|
|
7
7
|
|
8
8
|
before(:each) do
|
9
9
|
@project = Project.create(:name => "Rich", :notes => "this plugin's author")
|
10
|
+
@post = Post.create(:name => 'a name')
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "with auto-detected revision class" do
|
14
|
+
it "should find the revision class" do
|
15
|
+
Post.revision_class.should == PostRevision
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should find the revisable class" do
|
19
|
+
PostRevision.revisable_class.should == Post
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should use the revision class" do
|
23
|
+
@post.update_attribute(:name, 'another name')
|
24
|
+
@post.revisions(true).first.class.should == PostRevision
|
25
|
+
end
|
10
26
|
end
|
11
27
|
|
12
28
|
describe "with auto-generated revision class" do
|
data/spec/spec_helper.rb
CHANGED
@@ -45,6 +45,14 @@ def setup_db
|
|
45
45
|
t.datetime :revisable_current_at, :revisable_revised_at, :revisable_deleted_at
|
46
46
|
t.timestamps
|
47
47
|
end
|
48
|
+
|
49
|
+
create_table :posts do |t|
|
50
|
+
t.string :name, :revisable_name, :revisable_type, :type
|
51
|
+
t.boolean :revisable_is_current
|
52
|
+
t.integer :revisable_original_id, :revisable_branched_from_id, :revisable_number
|
53
|
+
t.datetime :revisable_current_at, :revisable_revised_at, :revisable_deleted_at
|
54
|
+
t.timestamps
|
55
|
+
end
|
48
56
|
end
|
49
57
|
end
|
50
58
|
|
@@ -90,4 +98,20 @@ end
|
|
90
98
|
|
91
99
|
class Foo < ActiveRecord::Base
|
92
100
|
acts_as_revisable :generate_revision_class => true
|
101
|
+
end
|
102
|
+
|
103
|
+
class Post < ActiveRecord::Base
|
104
|
+
acts_as_revisable
|
105
|
+
end
|
106
|
+
|
107
|
+
class PostRevision < ActiveRecord::Base
|
108
|
+
acts_as_revision
|
109
|
+
end
|
110
|
+
|
111
|
+
class Article < Post
|
112
|
+
acts_as_revisable
|
113
|
+
end
|
114
|
+
|
115
|
+
class ArticleRevision < PostRevision
|
116
|
+
acts_as_revision
|
93
117
|
end
|
data/spec/sti_spec.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe WithoutScope::ActsAsRevisable, "with single table inheritance" do
|
4
|
+
after(:each) do
|
5
|
+
cleanup_db
|
6
|
+
end
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
@article = Article.create(:name => 'an article')
|
10
|
+
@post = Post.create(:name => 'a post')
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "after a revision" do
|
14
|
+
it "an article has revisions of the right type" do
|
15
|
+
@article.revise!
|
16
|
+
@article.revisions(true).first.class.should == ArticleRevision
|
17
|
+
end
|
18
|
+
|
19
|
+
it "a post has revisions of the right type" do
|
20
|
+
@post.revise!
|
21
|
+
@post.revisions(true).first.class.should == PostRevision
|
22
|
+
end
|
23
|
+
|
24
|
+
it "revisable_type column is nil for the root type" do
|
25
|
+
@post.revise!
|
26
|
+
@post.revisions(true).first.revisable_type.should == 'Post'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "revisable_type column is set properly" do
|
30
|
+
@article.revise!
|
31
|
+
@article.revisions(true).first.revisable_type.should == 'Article'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "can find an article by name" do
|
35
|
+
Article.find_by_name('an article').should_not be_nil
|
36
|
+
end
|
37
|
+
|
38
|
+
it "can find a post by name" do
|
39
|
+
Post.find_by_name('a post').should_not be_nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rich-acts_as_revisable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rich Cavanaugh
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-
|
13
|
+
date: 2009-05-16 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- spec/revert_spec.rb
|
38
38
|
- spec/spec.opts
|
39
39
|
- spec/spec_helper.rb
|
40
|
+
- spec/sti_spec.rb
|
40
41
|
- lib/acts_as_revisable
|
41
42
|
- lib/acts_as_revisable/acts
|
42
43
|
- lib/acts_as_revisable/acts/common.rb
|
@@ -56,6 +57,7 @@ files:
|
|
56
57
|
- rails/init.rb
|
57
58
|
has_rdoc: true
|
58
59
|
homepage: http://github.com/rich/acts_as_revisable
|
60
|
+
licenses:
|
59
61
|
post_install_message:
|
60
62
|
rdoc_options:
|
61
63
|
- --main
|
@@ -77,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
79
|
requirements: []
|
78
80
|
|
79
81
|
rubyforge_project:
|
80
|
-
rubygems_version: 1.
|
82
|
+
rubygems_version: 1.3.5
|
81
83
|
signing_key:
|
82
84
|
specification_version: 2
|
83
85
|
summary: acts_as_revisable enables revision tracking, querying, reverting and branching of ActiveRecord models. Inspired by acts_as_versioned.
|