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 CHANGED
@@ -18,24 +18,24 @@ Create the movable table
18
18
  Migration:
19
19
 
20
20
  <pre>
21
- class CreateArchivedArticles < ActiveRecord::Migration
21
+ class CreateArticlesArchive < ActiveRecord::Migration
22
22
  def self.up
23
23
  Article.create_movable_table(
24
- :archived,
24
+ :archive,
25
25
  :columns => %w(id title body created_at),
26
26
  :indexes => %w(id created_at)
27
27
  )
28
- add_column :archived_articles, :move_id, :string
29
- add_column :archived_articles, :moved_at, :datetime
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(:archived)
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>archived_articles</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 :archived
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(:archived)
65
- Article.move_to(:archived, [ "created_at > ?", Date.today ])
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(:archived, [ "created_at > ?", Date.today ])
75
- ArchivedArticle.last.move_from
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>ArchivedArticle</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
 
@@ -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] = [ type, table_name ].join('_')
32
+ args[0] = [ table_name, type ].join('_')
32
33
  if method == :rename_table
33
- args[1] = [ type, args[1].to_s ].join('_')
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
@@ -48,7 +48,7 @@ module Mover
48
48
  end
49
49
 
50
50
  def movable_class(type)
51
- eval(type.to_s.classify + self.table_name.classify)
51
+ eval(self.name + type.to_s.classify)
52
52
  rescue
53
53
  raise "#{self.table_name.classify} needs an `is_movable :#{type}` definition"
54
54
  end
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 = [ type, table_name ].join('_')
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 #{[ type, table_name ].join('_')}")
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 ::#{type.to_s.classify}#{self.table_name.classify} < ActiveRecord::Base
26
+ class ::#{self.name}#{type.to_s.classify} < ActiveRecord::Base
27
27
  include Mover::Base::Record::InstanceMethods
28
28
 
29
- self.table_name = "#{type}_#{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.table_name.classify}
36
+ #{self.name}
37
37
  end
38
38
  end
39
39
  RUBY
data/require.rb CHANGED
@@ -17,7 +17,7 @@ Require do
17
17
  name 'mover'
18
18
  homepage "http://github.com/winton/#{name}"
19
19
  summary "Move ActiveRecord records across tables like it ain't no thang"
20
- version '0.1.0'
20
+ version '0.1.1'
21
21
  end
22
22
 
23
23
  bin { require 'lib/mover' }
@@ -7,11 +7,11 @@ class CreateArticles < ActiveRecord::Migration
7
7
  end
8
8
  add_index :articles, :title
9
9
 
10
- Article.create_movable_table(:archived)
11
- add_column :archived_articles, :move_id, :string
12
- add_column :archived_articles, :moved_at, :datetime
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(:drafted)
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(:archived)
24
- add_column :archived_comments, :move_id, :string
25
- add_column :archived_comments, :moved_at, :datetime
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(:archived)
32
- Comment.drop_movable_table(:archived)
31
+ Article.drop_movable_table(:archive)
32
+ Comment.drop_movable_table(:archive)
33
33
  end
34
34
  end
@@ -1,4 +1,4 @@
1
1
  class Article < ActiveRecord::Base
2
2
  has_many :comments
3
- is_movable :archived, :drafted
3
+ is_movable :archive, :draft
4
4
  end
@@ -1,4 +1,4 @@
1
1
  class Comment < ActiveRecord::Base
2
2
  belongs_to :article
3
- is_movable :archived
3
+ is_movable :archive
4
4
  end