acts_as_archive 0.3.4 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -12
- data/config/gemsets.yml +2 -2
- data/config/gemspec.yml +1 -1
- data/lib/acts_as_archive.rb +66 -32
- data/lib/acts_as_archive/adapters/rails2.rb +1 -0
- data/lib/acts_as_archive/adapters/rails3.rb +9 -0
- data/lib/acts_as_archive/adapters/sinatra.rb +12 -0
- data/spec/fixtures/config/acts_as_archive.yml +3 -0
- data/spec/fixtures/frameworks.yml +5 -0
- data/spec/fixtures/frameworks/rails3/Gemfile +2 -0
- data/spec/fixtures/frameworks/sinatra/application.rb +1 -0
- data/spec/fixtures/models/record.rb +0 -2
- data/spec/spec_helper.rb +4 -2
- metadata +10 -5
data/README.md
CHANGED
@@ -32,23 +32,29 @@ gem 'acts_as_archive'
|
|
32
32
|
|
33
33
|
<pre>
|
34
34
|
require 'acts_as_archive'
|
35
|
-
</pre>
|
36
35
|
|
37
|
-
|
38
|
-
|
36
|
+
class Application < Sinatra::Base
|
37
|
+
include ActsAsArchive::Adapters::Sinatra
|
38
|
+
end
|
39
|
+
</pre>
|
39
40
|
|
40
|
-
|
41
|
+
config/acts\_as\_archive.yml
|
42
|
+
----------------------------
|
41
43
|
|
42
44
|
<pre>
|
43
|
-
|
44
|
-
|
45
|
-
|
45
|
+
Article:
|
46
|
+
- class: Article::Archive
|
47
|
+
table: archived_articles
|
46
48
|
</pre>
|
47
49
|
|
50
|
+
Specify the name of your model, the name of the archive class, and the name of the archive table.
|
51
|
+
|
52
|
+
If the archive model is created automatically if it does not exist.
|
53
|
+
|
48
54
|
Migrate
|
49
55
|
-------
|
50
56
|
|
51
|
-
|
57
|
+
Run <code>rake db:migrate</code>. Your archive table is created automatically.
|
52
58
|
|
53
59
|
That's it!
|
54
60
|
----------
|
@@ -60,9 +66,7 @@ Records move into the archive table instead of being destroyed.
|
|
60
66
|
Automatically archive relationships
|
61
67
|
-----------------------------------
|
62
68
|
|
63
|
-
If your
|
64
|
-
|
65
|
-
__To use this feature, you must declare your relationships before the <code>acts\_as\_archive</code> call within your model!__
|
69
|
+
If your model's relationship has the <code>:dependent</code> option, and the relationship also uses <code>acts\_as\_archive</code>, that relationship will archive automatically.
|
66
70
|
|
67
71
|
What if my schema changes?
|
68
72
|
--------------------------
|
@@ -74,7 +78,7 @@ No action is necessary on your part.
|
|
74
78
|
Query the archive
|
75
79
|
-----------------
|
76
80
|
|
77
|
-
|
81
|
+
Use the Archive model you specified in the configuration:
|
78
82
|
|
79
83
|
<pre>
|
80
84
|
Article::Archive.first
|
data/config/gemsets.yml
CHANGED
data/config/gemspec.yml
CHANGED
data/lib/acts_as_archive.rb
CHANGED
@@ -4,6 +4,7 @@ ActsAsArchive::Gems.activate %w(also_migrate mover)
|
|
4
4
|
|
5
5
|
require 'also_migrate'
|
6
6
|
require 'mover'
|
7
|
+
require 'yaml'
|
7
8
|
|
8
9
|
$:.unshift File.dirname(__FILE__)
|
9
10
|
|
@@ -41,18 +42,34 @@ class ActsAsArchive
|
|
41
42
|
end
|
42
43
|
end
|
43
44
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
45
|
+
def load_from_yaml(root)
|
46
|
+
if File.exists?(yaml = "#{root}/config/acts_as_archive.yml")
|
47
|
+
YAML.load(File.read(yaml)).each do |klass, config|
|
48
|
+
klass = eval(klass) rescue nil
|
49
|
+
if klass
|
50
|
+
if (%w(class table) - config.last.keys).empty?
|
51
|
+
options = {}
|
52
|
+
else
|
53
|
+
options = config.pop
|
54
|
+
end
|
55
|
+
config.each do |c|
|
56
|
+
klass.acts_as_archive options.merge(c)
|
57
|
+
end
|
58
|
+
end
|
51
59
|
end
|
52
|
-
config[:from].move_to(to, options)
|
53
60
|
end
|
54
61
|
end
|
55
62
|
|
63
|
+
def move(config, where, merge_options={})
|
64
|
+
options = config[:options].dup.merge(merge_options)
|
65
|
+
if options[:conditions]
|
66
|
+
options[:conditions] += " AND #{where}"
|
67
|
+
elsif where
|
68
|
+
options[:conditions] = where
|
69
|
+
end
|
70
|
+
config[:from].move_to(config[:to], options)
|
71
|
+
end
|
72
|
+
|
56
73
|
def update(*args)
|
57
74
|
deprecate "ActsAsArchive.update is deprecated and no longer necessary."
|
58
75
|
end
|
@@ -67,17 +84,17 @@ class ActsAsArchive
|
|
67
84
|
end
|
68
85
|
|
69
86
|
module ClassMethods
|
70
|
-
def acts_as_archive(
|
87
|
+
def acts_as_archive(options={})
|
71
88
|
return unless ActsAsArchive.find(self).empty?
|
72
89
|
|
73
90
|
ActsAsArchive.configuration ||= []
|
74
91
|
ActsAsArchive.configuration << (config = { :from => self })
|
75
92
|
|
76
|
-
options = args.last.is_a?(::Hash) ? args.pop : {}
|
77
93
|
options[:copy] = true
|
78
94
|
|
79
95
|
if options[:archive]
|
80
96
|
options[:magic] = 'restored_at'
|
97
|
+
klass = options[:class]
|
81
98
|
else
|
82
99
|
options[:magic] = 'deleted_at' if options[:magic].nil?
|
83
100
|
options[:add] = [[ options[:magic], :datetime ]]
|
@@ -85,35 +102,49 @@ class ActsAsArchive
|
|
85
102
|
options[:subtract] = 'restored_at'
|
86
103
|
options[:timestamps] = false if options[:timestamps].nil?
|
87
104
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
105
|
+
unless options[:class]
|
106
|
+
options[:class] = "#{self}::Archive"
|
107
|
+
end
|
108
|
+
|
109
|
+
unless options[:table]
|
110
|
+
options[:table] = "archived_#{self.table_name}"
|
111
|
+
end
|
112
|
+
|
113
|
+
klass = eval(options[:class]) rescue nil
|
114
|
+
|
115
|
+
if klass
|
116
|
+
klass.send :set_table_name, options[:table]
|
117
|
+
else
|
118
|
+
eval <<-EVAL
|
119
|
+
class ::#{options[:class]} < ActiveRecord::Base
|
120
|
+
set_table_name "#{options[:table]}"
|
92
121
|
end
|
93
122
|
EVAL
|
94
|
-
|
123
|
+
klass = eval("::#{options[:class]}")
|
95
124
|
end
|
125
|
+
|
126
|
+
klass.record_timestamps = options[:timestamps].inspect
|
127
|
+
klass.acts_as_archive(:class => self, :archive => true)
|
96
128
|
|
97
|
-
|
98
|
-
klass.
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
if !ActsAsArchive.find(association.klass).empty? && association.options[:dependent]
|
104
|
-
opts = association.options.dup
|
105
|
-
opts[:class_name] = "::#{association.class_name}::Archive"
|
106
|
-
opts[:foreign_key] = association.primary_key_name
|
107
|
-
klass.send association.macro, association.name, opts
|
108
|
-
end
|
109
|
-
end
|
110
|
-
unless options[:migrate] == false
|
111
|
-
self.also_migrate klass.table_name, options
|
129
|
+
self.reflect_on_all_associations.each do |association|
|
130
|
+
if !ActsAsArchive.find(association.klass).empty? && association.options[:dependent]
|
131
|
+
opts = association.options.dup
|
132
|
+
opts[:class_name] = "::#{association.class_name}::Archive"
|
133
|
+
opts[:foreign_key] = association.primary_key_name
|
134
|
+
klass.send association.macro, association.name, opts
|
112
135
|
end
|
113
136
|
end
|
137
|
+
|
138
|
+
unless options[:migrate] == false
|
139
|
+
AlsoMigrate.configuration ||= []
|
140
|
+
AlsoMigrate.configuration << options.merge(
|
141
|
+
:source => self.table_name,
|
142
|
+
:destination => klass.table_name
|
143
|
+
)
|
144
|
+
end
|
114
145
|
end
|
115
146
|
|
116
|
-
config[:to] =
|
147
|
+
config[:to] = klass
|
117
148
|
config[:options] = options
|
118
149
|
end
|
119
150
|
|
@@ -192,4 +223,7 @@ class ActsAsArchive
|
|
192
223
|
end
|
193
224
|
|
194
225
|
::ActiveRecord::Base.send(:include, ::ActsAsArchive::Base)
|
195
|
-
::ActiveRecord::ConnectionAdapters::DatabaseStatements.send(:include, ::ActsAsArchive::DatabaseStatements)
|
226
|
+
::ActiveRecord::ConnectionAdapters::DatabaseStatements.send(:include, ::ActsAsArchive::DatabaseStatements)
|
227
|
+
|
228
|
+
require "acts_as_archive/adapters/rails#{Rails.version[0..0]}" if defined?(Rails)
|
229
|
+
require "acts_as_archive/adapters/sinatra" if defined?(Sinatra)
|
@@ -0,0 +1 @@
|
|
1
|
+
ActsAsArchive.load_from_yaml(Rails.root)
|
@@ -1,5 +1,7 @@
|
|
1
1
|
rails:
|
2
2
|
<3:
|
3
|
+
config: &c
|
4
|
+
- config/acts_as_archive.yml
|
3
5
|
frameworks/rails2:
|
4
6
|
- app/controllers/application_controller.rb
|
5
7
|
- config/database.yml
|
@@ -17,6 +19,7 @@ rails:
|
|
17
19
|
- app/models/has_one_through_through.rb
|
18
20
|
- app/models/record.rb
|
19
21
|
<4:
|
22
|
+
config: *c
|
20
23
|
frameworks/rails3:
|
21
24
|
- app/controllers/application_controller.rb
|
22
25
|
- config/database.yml
|
@@ -26,11 +29,13 @@ rails:
|
|
26
29
|
models: *m
|
27
30
|
sinatra:
|
28
31
|
<1:
|
32
|
+
config: *c
|
29
33
|
frameworks/sinatra: &s
|
30
34
|
- application.rb
|
31
35
|
helpers: *h
|
32
36
|
models: *m
|
33
37
|
<2:
|
38
|
+
config: *c
|
34
39
|
frameworks/sinatra: *s
|
35
40
|
helpers: *h
|
36
41
|
models: *m
|
data/spec/spec_helper.rb
CHANGED
@@ -45,8 +45,6 @@ else
|
|
45
45
|
ActiveSupport::Dependencies.autoload_paths << "#{$root}/spec/fixtures/models"
|
46
46
|
ActiveSupport::Dependencies.autoload_paths << "#{$root}/spec/fixtures/helpers"
|
47
47
|
|
48
|
-
Record # Load up an instance so first also_migrate works
|
49
|
-
|
50
48
|
include SpecHelper
|
51
49
|
end
|
52
50
|
|
@@ -57,6 +55,10 @@ $db, $log, $mail = ActiveWrapper.setup(
|
|
57
55
|
)
|
58
56
|
$db.establish_connection
|
59
57
|
|
58
|
+
unless FrameworkFixture.framework
|
59
|
+
ActsAsArchive.load_from_yaml("#{$root}/spec/fixtures")
|
60
|
+
end
|
61
|
+
|
60
62
|
if FrameworkFixture.framework == 'sinatra'
|
61
63
|
FrameworkFixture.generate File.dirname(__FILE__) + '/fixtures'
|
62
64
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 3
|
8
7
|
- 4
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.4.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Winton Welsh
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-25 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -28,8 +28,8 @@ dependencies:
|
|
28
28
|
segments:
|
29
29
|
- 0
|
30
30
|
- 3
|
31
|
-
-
|
32
|
-
version: 0.3.
|
31
|
+
- 5
|
32
|
+
version: 0.3.5
|
33
33
|
type: :runtime
|
34
34
|
version_requirements: *id001
|
35
35
|
- !ruby/object:Gem::Dependency
|
@@ -67,11 +67,15 @@ files:
|
|
67
67
|
- config/gemspec.yml
|
68
68
|
- init.rb
|
69
69
|
- lib/acts_as_archive.rb
|
70
|
+
- lib/acts_as_archive/adapters/rails2.rb
|
71
|
+
- lib/acts_as_archive/adapters/rails3.rb
|
72
|
+
- lib/acts_as_archive/adapters/sinatra.rb
|
70
73
|
- lib/acts_as_archive/gems.rb
|
71
74
|
- rails/init.rb
|
72
75
|
- spec/Rakefile
|
73
76
|
- spec/acts_as_archive/gems_spec.rb
|
74
77
|
- spec/acts_as_archive_spec.rb
|
78
|
+
- spec/fixtures/config/acts_as_archive.yml
|
75
79
|
- spec/fixtures/config/database.yml.example
|
76
80
|
- spec/fixtures/db/migrate/001_belongs_tos.rb
|
77
81
|
- spec/fixtures/db/migrate/002_records.rb
|
@@ -141,6 +145,7 @@ test_files:
|
|
141
145
|
- spec/Rakefile
|
142
146
|
- spec/acts_as_archive/gems_spec.rb
|
143
147
|
- spec/acts_as_archive_spec.rb
|
148
|
+
- spec/fixtures/config/acts_as_archive.yml
|
144
149
|
- spec/fixtures/config/database.yml.example
|
145
150
|
- spec/fixtures/db/migrate/001_belongs_tos.rb
|
146
151
|
- spec/fixtures/db/migrate/002_records.rb
|