mover 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +12 -12
- data/lib/mover/migrator.rb +3 -2
- data/lib/mover/record.rb +1 -1
- data/lib/mover/table.rb +2 -2
- data/lib/mover.rb +3 -3
- data/require.rb +1 -1
- data/spec/db/migrate/001_create_articles.rb +9 -9
- data/spec/fixtures/article.rb +1 -1
- data/spec/fixtures/comment.rb +1 -1
- data/spec/log/test.log +7266 -3635
- data/spec/mover/migrator_spec.rb +1 -0
- data/spec/mover/record_spec.rb +27 -26
- data/spec/mover/table_spec.rb +12 -11
- data/spec/spec_helper.rb +2 -2
- metadata +1 -1
data/README.markdown
CHANGED
@@ -18,24 +18,24 @@ Create the movable table
|
|
18
18
|
Migration:
|
19
19
|
|
20
20
|
<pre>
|
21
|
-
class
|
21
|
+
class CreateArticlesArchive < ActiveRecord::Migration
|
22
22
|
def self.up
|
23
23
|
Article.create_movable_table(
|
24
|
-
:
|
24
|
+
:archive,
|
25
25
|
:columns => %w(id title body created_at),
|
26
26
|
:indexes => %w(id created_at)
|
27
27
|
)
|
28
|
-
add_column :
|
29
|
-
add_column :
|
28
|
+
add_column :articles_archive, :move_id, :string
|
29
|
+
add_column :articles_archive, :moved_at, :datetime
|
30
30
|
end
|
31
31
|
|
32
32
|
def self.down
|
33
|
-
Article.drop_movable_table(:
|
33
|
+
Article.drop_movable_table(:archive)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
</pre>
|
37
37
|
|
38
|
-
The first parameter names your movable table. In this example, the table is named <code>
|
38
|
+
The first parameter names your movable table. In this example, the table is named <code>articles_archive</code>.
|
39
39
|
|
40
40
|
Options:
|
41
41
|
|
@@ -51,7 +51,7 @@ Define the model
|
|
51
51
|
|
52
52
|
<pre>
|
53
53
|
class Article < ActiveRecord::Base
|
54
|
-
is_movable :
|
54
|
+
is_movable :archive
|
55
55
|
end
|
56
56
|
</pre>
|
57
57
|
|
@@ -61,8 +61,8 @@ Moving records
|
|
61
61
|
--------------
|
62
62
|
|
63
63
|
<pre>
|
64
|
-
Article.last.move_to(:
|
65
|
-
Article.move_to(:
|
64
|
+
Article.last.move_to(:archive)
|
65
|
+
Article.move_to(:archive, [ "created_at > ?", Date.today ])
|
66
66
|
</pre>
|
67
67
|
|
68
68
|
Associations move if they are movable and if all movable tables have a <code>move_id</code> column (see <a href="#magic_columns">magic columns</a>).
|
@@ -71,11 +71,11 @@ Restoring records
|
|
71
71
|
-----------------
|
72
72
|
|
73
73
|
<pre>
|
74
|
-
Article.move_from(:
|
75
|
-
|
74
|
+
Article.move_from(:archive, [ "created_at > ?", Date.today ])
|
75
|
+
ArticleArchive.last.move_from
|
76
76
|
</pre>
|
77
77
|
|
78
|
-
You can access the movable table by prepending its name to the original class name. In this example, you would use <code>
|
78
|
+
You can access the movable table by prepending its name to the original class name. In this example, you would use <code>ArticleArchive</code>.
|
79
79
|
|
80
80
|
<a name="magic_columns"></a>
|
81
81
|
|
data/lib/mover/migrator.rb
CHANGED
@@ -4,6 +4,7 @@ module Mover
|
|
4
4
|
def method_missing_with_mover(method, *arguments, &block)
|
5
5
|
args = Marshal.load(Marshal.dump(arguments))
|
6
6
|
method_missing_without_mover(method, *arguments, &block)
|
7
|
+
|
7
8
|
supported = [
|
8
9
|
:add_column, :add_timestamps, :change_column,
|
9
10
|
:change_column_default, :change_table,
|
@@ -28,9 +29,9 @@ module Mover
|
|
28
29
|
# Run migration on movable table
|
29
30
|
if klass
|
30
31
|
klass.movable_types.each do |type|
|
31
|
-
args[0] = [
|
32
|
+
args[0] = [ table_name, type ].join('_')
|
32
33
|
if method == :rename_table
|
33
|
-
args[1] = [
|
34
|
+
args[1] = [ args[1].to_s, type ].join('_')
|
34
35
|
end
|
35
36
|
if connection.table_exists?(args[0])
|
36
37
|
connection.send(method, *args, &block)
|
data/lib/mover/record.rb
CHANGED
data/lib/mover/table.rb
CHANGED
@@ -3,7 +3,7 @@ module Mover
|
|
3
3
|
module Table
|
4
4
|
|
5
5
|
def create_movable_table(type, options={})
|
6
|
-
movable_table = [
|
6
|
+
movable_table = [ table_name, type ].join('_')
|
7
7
|
columns =
|
8
8
|
if options[:columns]
|
9
9
|
options[:columns].collect { |c| "`#{c}`" }.join(', ')
|
@@ -33,7 +33,7 @@ module Mover
|
|
33
33
|
|
34
34
|
def drop_movable_table(*types)
|
35
35
|
types.each do |type|
|
36
|
-
connection.execute("DROP TABLE IF EXISTS #{[
|
36
|
+
connection.execute("DROP TABLE IF EXISTS #{[ table_name, type ].join('_')}")
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
data/lib/mover.rb
CHANGED
@@ -23,17 +23,17 @@ module Mover
|
|
23
23
|
|
24
24
|
types.each do |type|
|
25
25
|
eval <<-RUBY
|
26
|
-
class ::#{
|
26
|
+
class ::#{self.name}#{type.to_s.classify} < ActiveRecord::Base
|
27
27
|
include Mover::Base::Record::InstanceMethods
|
28
28
|
|
29
|
-
self.table_name = "#{
|
29
|
+
self.table_name = "#{self.table_name}_#{type}"
|
30
30
|
|
31
31
|
def self.movable_type
|
32
32
|
#{type.inspect}
|
33
33
|
end
|
34
34
|
|
35
35
|
def moved_from_class
|
36
|
-
#{self.
|
36
|
+
#{self.name}
|
37
37
|
end
|
38
38
|
end
|
39
39
|
RUBY
|
data/require.rb
CHANGED
@@ -7,11 +7,11 @@ class CreateArticles < ActiveRecord::Migration
|
|
7
7
|
end
|
8
8
|
add_index :articles, :title
|
9
9
|
|
10
|
-
Article.create_movable_table(:
|
11
|
-
add_column :
|
12
|
-
add_column :
|
10
|
+
Article.create_movable_table(:archive)
|
11
|
+
add_column :articles_archive, :move_id, :string
|
12
|
+
add_column :articles_archive, :moved_at, :datetime
|
13
13
|
|
14
|
-
Article.create_movable_table(:
|
14
|
+
Article.create_movable_table(:draft)
|
15
15
|
|
16
16
|
create_table :comments do |t|
|
17
17
|
t.string :title
|
@@ -20,15 +20,15 @@ class CreateArticles < ActiveRecord::Migration
|
|
20
20
|
t.integer :article_id
|
21
21
|
end
|
22
22
|
|
23
|
-
Comment.create_movable_table(:
|
24
|
-
add_column :
|
25
|
-
add_column :
|
23
|
+
Comment.create_movable_table(:archive)
|
24
|
+
add_column :comments_archive, :move_id, :string
|
25
|
+
add_column :comments_archive, :moved_at, :datetime
|
26
26
|
end
|
27
27
|
|
28
28
|
def self.down
|
29
29
|
drop_table :articles
|
30
30
|
drop_table :comments
|
31
|
-
Article.drop_movable_table(:
|
32
|
-
Comment.drop_movable_table(:
|
31
|
+
Article.drop_movable_table(:archive)
|
32
|
+
Comment.drop_movable_table(:archive)
|
33
33
|
end
|
34
34
|
end
|
data/spec/fixtures/article.rb
CHANGED
data/spec/fixtures/comment.rb
CHANGED