acts_as_archive 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.gitignore +9 -0
  2. data/LICENSE +18 -0
  3. data/README.md +112 -0
  4. data/Rakefile +90 -0
  5. data/acts_as_archive.gemspec +32 -0
  6. data/bin/acts_as_archive +2 -0
  7. data/config/externals.yml +12 -0
  8. data/config/gemsets.yml +10 -0
  9. data/config/gemspec.yml +17 -0
  10. data/init.rb +1 -0
  11. data/lib/acts_as_archive.rb +192 -0
  12. data/lib/acts_as_archive/gems.rb +154 -0
  13. data/rails/init.rb +1 -0
  14. data/spec/Rakefile +9 -0
  15. data/spec/acts_as_archive/gems_spec.rb +249 -0
  16. data/spec/acts_as_archive_spec.rb +113 -0
  17. data/spec/fixtures/config/database.yml.example +6 -0
  18. data/spec/fixtures/db/migrate/001_belongs_tos.rb +14 -0
  19. data/spec/fixtures/db/migrate/002_records.rb +15 -0
  20. data/spec/fixtures/db/migrate/003_has_ones.rb +15 -0
  21. data/spec/fixtures/db/migrate/004_has_manies.rb +15 -0
  22. data/spec/fixtures/db/migrate/005_has_many_through_throughs.rb +16 -0
  23. data/spec/fixtures/db/migrate/006_has_many_throughs.rb +14 -0
  24. data/spec/fixtures/db/migrate/007_has_one_through_throughs.rb +15 -0
  25. data/spec/fixtures/db/migrate/008_has_one_throughs.rb +15 -0
  26. data/spec/fixtures/frameworks.yml +36 -0
  27. data/spec/fixtures/frameworks/rails2/application_controller.rb +58 -0
  28. data/spec/fixtures/frameworks/rails2/database.yml +12 -0
  29. data/spec/fixtures/frameworks/rails2/init.rb +1 -0
  30. data/spec/fixtures/frameworks/rails2/routes.rb +46 -0
  31. data/spec/fixtures/frameworks/rails3/Gemfile +34 -0
  32. data/spec/fixtures/frameworks/rails3/application_controller.rb +51 -0
  33. data/spec/fixtures/frameworks/rails3/database.yml +12 -0
  34. data/spec/fixtures/frameworks/rails3/routes.rb +60 -0
  35. data/spec/fixtures/frameworks/sinatra/application.rb +58 -0
  36. data/spec/fixtures/gemsets.yml +9 -0
  37. data/spec/fixtures/gemspec.yml +15 -0
  38. data/spec/fixtures/helpers/spec_helper.rb +210 -0
  39. data/spec/fixtures/models/belongs_to.rb +6 -0
  40. data/spec/fixtures/models/has_many.rb +6 -0
  41. data/spec/fixtures/models/has_many_through.rb +7 -0
  42. data/spec/fixtures/models/has_many_through_through.rb +7 -0
  43. data/spec/fixtures/models/has_one.rb +6 -0
  44. data/spec/fixtures/models/has_one_through.rb +6 -0
  45. data/spec/fixtures/models/has_one_through_through.rb +7 -0
  46. data/spec/fixtures/models/record.rb +16 -0
  47. data/spec/run +27 -0
  48. data/spec/spec.opts +1 -0
  49. data/spec/spec_helper.rb +73 -0
  50. metadata +96 -13
@@ -0,0 +1,154 @@
1
+ unless defined?(ActsAsArchive::Gems)
2
+
3
+ require 'yaml'
4
+
5
+ class ActsAsArchive
6
+ module Gems
7
+ class <<self
8
+
9
+ attr_accessor :config
10
+ attr_reader :gemset, :gemsets, :versions
11
+
12
+ class SimpleStruct
13
+ attr_reader :hash
14
+
15
+ def initialize(hash)
16
+ @hash = hash
17
+ @hash.each do |key, value|
18
+ self.class.send(:define_method, key) { @hash[key] }
19
+ self.class.send(:define_method, "#{key}=") { |v| @hash[key] = v }
20
+ end
21
+ end
22
+ end
23
+
24
+ Gems.config = SimpleStruct.new(
25
+ :gemsets => [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ],
26
+ :gemspec => "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml",
27
+ :warn => true
28
+ )
29
+
30
+ def activate(*gems)
31
+ begin
32
+ require 'rubygems' unless defined?(::Gem)
33
+ rescue LoadError
34
+ puts "rubygems library could not be required" if @config.warn
35
+ end
36
+
37
+ self.gemset ||= gemset_from_loaded_specs
38
+
39
+ gems.flatten.collect(&:to_sym).each do |name|
40
+ version = @versions[name]
41
+ vendor = File.expand_path("../../../vendor/#{name}/lib", __FILE__)
42
+ if File.exists?(vendor)
43
+ $:.unshift vendor
44
+ elsif defined?(gem)
45
+ gem name.to_s, version
46
+ else
47
+ puts "#{name} #{"(#{version})" if version} failed to activate" if @config.warn
48
+ end
49
+ end
50
+ end
51
+
52
+ def dependencies
53
+ dependency_filter(@gemspec.dependencies, @gemset)
54
+ end
55
+
56
+ def development_dependencies
57
+ dependency_filter(@gemspec.development_dependencies, @gemset)
58
+ end
59
+
60
+ def gemset=(gemset)
61
+ if gemset
62
+ @gemset = gemset.to_sym
63
+
64
+ @gemsets = @config.gemsets.reverse.collect { |config|
65
+ if config.is_a?(::String)
66
+ YAML::load(File.read(config)) rescue {}
67
+ elsif config.is_a?(::Hash)
68
+ config
69
+ end
70
+ }.inject({}) do |hash, config|
71
+ deep_merge(hash, symbolize_keys(config))
72
+ end
73
+
74
+ @versions = (@gemsets[gemspec.name.to_sym] || {}).inject({}) do |hash, (key, value)|
75
+ if !value.is_a?(::Hash) && value
76
+ hash[key] = value
77
+ elsif key == @gemset
78
+ (value || {}).each { |k, v| hash[k] = v }
79
+ end
80
+ hash
81
+ end
82
+ else
83
+ @gemset = nil
84
+ @gemsets = nil
85
+ @versions = nil
86
+ end
87
+ end
88
+
89
+ def gemset_names
90
+ (
91
+ [ :default ] +
92
+ @gemsets[gemspec.name.to_sym].inject([]) { |array, (key, value)|
93
+ array.push(key) if value.is_a?(::Hash) || value.nil?
94
+ array
95
+ }
96
+ ).uniq
97
+ end
98
+
99
+ def gemspec(reload=false)
100
+ if @gemspec && !reload
101
+ @gemspec
102
+ else
103
+ data = YAML::load(File.read(@config.gemspec)) rescue {}
104
+ @gemspec = SimpleStruct.new(data)
105
+ end
106
+ end
107
+
108
+ private
109
+
110
+ def deep_merge(first, second)
111
+ merger = lambda do |key, v1, v2|
112
+ Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
113
+ end
114
+ first.merge(second, &merger)
115
+ end
116
+
117
+ def dependency_filter(dependencies, match)
118
+ (dependencies || []).inject([]) { |array, value|
119
+ if value.is_a?(::Hash)
120
+ array += value[match.to_s] if value[match.to_s]
121
+ else
122
+ array << value
123
+ end
124
+ array
125
+ }.uniq.collect(&:to_sym)
126
+ end
127
+
128
+ def gemset_from_loaded_specs
129
+ if defined?(Gem)
130
+ Gem.loaded_specs.each do |name, spec|
131
+ if name == gemspec.name
132
+ return :default
133
+ elsif name[0..gemspec.name.length] == "#{gemspec.name}-"
134
+ return name[gemspec.name.length+1..-1].to_sym
135
+ end
136
+ end
137
+ :default
138
+ else
139
+ :none
140
+ end
141
+ end
142
+
143
+ def symbolize_keys(hash)
144
+ return {} unless hash.is_a?(::Hash)
145
+ hash.inject({}) do |options, (key, value)|
146
+ value = symbolize_keys(value) if value.is_a?(::Hash)
147
+ options[(key.to_sym rescue key) || key] = value
148
+ options
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/acts_as_archive')
data/spec/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/acts_as_archive/gems')
2
+ ActsAsArchive::Gems.activate :active_wrapper
3
+
4
+ require 'active_wrapper/tasks'
5
+
6
+ ActiveWrapper::Tasks.new(
7
+ :base => File.dirname(__FILE__) + "/fixtures",
8
+ :env => ENV['ENV']
9
+ )
@@ -0,0 +1,249 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsArchive::Gems do
4
+
5
+ before(:each) do
6
+ @old_config = ActsAsArchive::Gems.config
7
+
8
+ ActsAsArchive::Gems.config.gemspec = "#{$root}/spec/fixtures/gemspec.yml"
9
+ ActsAsArchive::Gems.config.gemsets = [
10
+ "#{$root}/spec/fixtures/gemsets.yml"
11
+ ]
12
+ ActsAsArchive::Gems.config.warn = true
13
+
14
+ ActsAsArchive::Gems.gemspec true
15
+ ActsAsArchive::Gems.gemset = nil
16
+ end
17
+
18
+ after(:each) do
19
+ ActsAsArchive::Gems.config = @old_config
20
+ end
21
+
22
+ describe :activate do
23
+ it "should activate gems" do
24
+ ActsAsArchive::Gems.stub!(:gem)
25
+ ActsAsArchive::Gems.should_receive(:gem).with('rspec', '=1.3.1')
26
+ ActsAsArchive::Gems.should_receive(:gem).with('rake', '=0.8.7')
27
+ ActsAsArchive::Gems.activate :rspec, 'rake'
28
+ end
29
+ end
30
+
31
+ describe :gemset= do
32
+ before(:each) do
33
+ ActsAsArchive::Gems.config.gemsets = [
34
+ {
35
+ :name => {
36
+ :rake => '>0.8.6',
37
+ :default => {
38
+ :externals => '=1.0.2'
39
+ }
40
+ }
41
+ },
42
+ "#{$root}/spec/fixtures/gemsets.yml"
43
+ ]
44
+ end
45
+
46
+ describe :default do
47
+ before(:each) do
48
+ ActsAsArchive::Gems.gemset = :default
49
+ end
50
+
51
+ it "should set @gemset" do
52
+ ActsAsArchive::Gems.gemset.should == :default
53
+ end
54
+
55
+ it "should set @gemsets" do
56
+ ActsAsArchive::Gems.gemsets.should == {
57
+ :name => {
58
+ :rake => ">0.8.6",
59
+ :default => {
60
+ :externals => '=1.0.2',
61
+ :mysql => "=2.8.1",
62
+ :rspec => "=1.3.1"
63
+ },
64
+ :rspec2 => {
65
+ :mysql2 => "=0.2.6",
66
+ :rspec => "=2.3.0"
67
+ },
68
+ :solo => nil
69
+ }
70
+ }
71
+ end
72
+
73
+ it "should set Gems.versions" do
74
+ ActsAsArchive::Gems.versions.should == {
75
+ :externals => "=1.0.2",
76
+ :mysql => "=2.8.1",
77
+ :rake => ">0.8.6",
78
+ :rspec => "=1.3.1"
79
+ }
80
+ end
81
+
82
+ it "should return proper values for Gems.dependencies" do
83
+ ActsAsArchive::Gems.dependencies.should == [ :rake, :mysql ]
84
+ ActsAsArchive::Gems.development_dependencies.should == []
85
+ end
86
+
87
+ it "should return proper values for Gems.gemset_names" do
88
+ ActsAsArchive::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
89
+ end
90
+ end
91
+
92
+ describe :rspec2 do
93
+ before(:each) do
94
+ ActsAsArchive::Gems.gemset = "rspec2"
95
+ end
96
+
97
+ it "should set @gemset" do
98
+ ActsAsArchive::Gems.gemset.should == :rspec2
99
+ end
100
+
101
+ it "should set @gemsets" do
102
+ ActsAsArchive::Gems.gemsets.should == {
103
+ :name => {
104
+ :rake => ">0.8.6",
105
+ :default => {
106
+ :externals => '=1.0.2',
107
+ :mysql => "=2.8.1",
108
+ :rspec => "=1.3.1"
109
+ },
110
+ :rspec2 => {
111
+ :mysql2=>"=0.2.6",
112
+ :rspec => "=2.3.0"
113
+ },
114
+ :solo => nil
115
+ }
116
+ }
117
+ end
118
+
119
+ it "should set Gems.versions" do
120
+ ActsAsArchive::Gems.versions.should == {
121
+ :mysql2 => "=0.2.6",
122
+ :rake => ">0.8.6",
123
+ :rspec => "=2.3.0"
124
+ }
125
+ end
126
+
127
+ it "should return proper values for Gems.dependencies" do
128
+ ActsAsArchive::Gems.dependencies.should == [ :rake, :mysql2 ]
129
+ ActsAsArchive::Gems.development_dependencies.should == []
130
+ end
131
+
132
+ it "should return proper values for Gems.gemset_names" do
133
+ ActsAsArchive::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
134
+ end
135
+ end
136
+
137
+ describe :solo do
138
+ before(:each) do
139
+ ActsAsArchive::Gems.gemset = :solo
140
+ end
141
+
142
+ it "should set @gemset" do
143
+ ActsAsArchive::Gems.gemset.should == :solo
144
+ end
145
+
146
+ it "should set @gemsets" do
147
+ ActsAsArchive::Gems.gemsets.should == {
148
+ :name => {
149
+ :rake => ">0.8.6",
150
+ :default => {
151
+ :externals => '=1.0.2',
152
+ :mysql => "=2.8.1",
153
+ :rspec => "=1.3.1"
154
+ },
155
+ :rspec2 => {
156
+ :mysql2=>"=0.2.6",
157
+ :rspec => "=2.3.0"
158
+ },
159
+ :solo => nil
160
+ }
161
+ }
162
+ end
163
+
164
+ it "should set Gems.versions" do
165
+ ActsAsArchive::Gems.versions.should == {:rake=>">0.8.6"}
166
+ end
167
+
168
+ it "should return proper values for Gems.dependencies" do
169
+ ActsAsArchive::Gems.dependencies.should == [:rake]
170
+ ActsAsArchive::Gems.development_dependencies.should == []
171
+ end
172
+
173
+ it "should return proper values for Gems.gemset_names" do
174
+ ActsAsArchive::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
175
+ end
176
+ end
177
+
178
+ describe :nil do
179
+ before(:each) do
180
+ ActsAsArchive::Gems.gemset = nil
181
+ end
182
+
183
+ it "should set everything to nil" do
184
+ ActsAsArchive::Gems.gemset.should == nil
185
+ ActsAsArchive::Gems.gemsets.should == nil
186
+ ActsAsArchive::Gems.versions.should == nil
187
+ end
188
+ end
189
+ end
190
+
191
+ describe :gemset_from_loaded_specs do
192
+ before(:each) do
193
+ Gem.stub!(:loaded_specs)
194
+ end
195
+
196
+ it "should return the correct gemset for name gem" do
197
+ Gem.should_receive(:loaded_specs).and_return({ "name" => nil })
198
+ ActsAsArchive::Gems.send(:gemset_from_loaded_specs).should == :default
199
+ end
200
+
201
+ it "should return the correct gemset for name-rspec gem" do
202
+ Gem.should_receive(:loaded_specs).and_return({ "name-rspec2" => nil })
203
+ ActsAsArchive::Gems.send(:gemset_from_loaded_specs).should == :rspec2
204
+ end
205
+ end
206
+
207
+ describe :reload_gemspec do
208
+ it "should populate @gemspec" do
209
+ ActsAsArchive::Gems.gemspec.hash.should == {
210
+ "name" => "name",
211
+ "version" => "0.1.0",
212
+ "authors" => ["Author"],
213
+ "email" => "email@email.com",
214
+ "homepage" => "http://github.com/author/name",
215
+ "summary" => "Summary",
216
+ "description" => "Description",
217
+ "dependencies" => [
218
+ "rake",
219
+ { "default" => [ "mysql" ] },
220
+ { "rspec2" => [ "mysql2" ] }
221
+ ],
222
+ "development_dependencies" => nil
223
+ }
224
+ end
225
+
226
+ it "should create methods from keys of @gemspec" do
227
+ ActsAsArchive::Gems.gemspec.name.should == "name"
228
+ ActsAsArchive::Gems.gemspec.version.should == "0.1.0"
229
+ ActsAsArchive::Gems.gemspec.authors.should == ["Author"]
230
+ ActsAsArchive::Gems.gemspec.email.should == "email@email.com"
231
+ ActsAsArchive::Gems.gemspec.homepage.should == "http://github.com/author/name"
232
+ ActsAsArchive::Gems.gemspec.summary.should == "Summary"
233
+ ActsAsArchive::Gems.gemspec.description.should == "Description"
234
+ ActsAsArchive::Gems.gemspec.dependencies.should == [
235
+ "rake",
236
+ { "default" => ["mysql"] },
237
+ { "rspec2" => [ "mysql2" ] }
238
+ ]
239
+ ActsAsArchive::Gems.gemspec.development_dependencies.should == nil
240
+ end
241
+
242
+ it "should produce a valid gemspec" do
243
+ ActsAsArchive::Gems.gemset = :default
244
+ gemspec = File.expand_path("../../../acts_as_archive.gemspec", __FILE__)
245
+ gemspec = eval(File.read(gemspec), binding, gemspec)
246
+ gemspec.validate.should == true
247
+ end
248
+ end
249
+ end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+
3
+ unless FrameworkFixture.framework
4
+ describe ActsAsArchive do
5
+
6
+ before(:each) do
7
+ before_each
8
+ end
9
+
10
+ it "should have valid schema" do
11
+ should_have_valid_schema
12
+ end
13
+
14
+ it "should create records" do
15
+ should_create_records
16
+ end
17
+
18
+ describe :migrate_from_acts_as_paranoid do
19
+ it "should migrate record and preserve deleted_at" do
20
+ should_migrate_record_and_preserve_deleted_at
21
+ end
22
+ end
23
+
24
+ describe :restore_all do
25
+ it "should emulate delete_all" do
26
+ should_emulate_delete_all
27
+ end
28
+ end
29
+
30
+ %w(delete delete_all destroy destroy_all).each do |type|
31
+ describe type do
32
+ it "should move records to archive tables" do
33
+ should_move_records_to_archive_tables(type)
34
+ end
35
+
36
+ it "should move records back to original tables" do
37
+ should_move_records_back_to_original_tables(type)
38
+ end
39
+ end
40
+
41
+ describe "#{type}!" do
42
+ it "should delete records without archiving" do
43
+ should_delete_records_without_archiving(type)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ if FrameworkFixture.framework
51
+ describe "#{FrameworkFixture.framework} #{FrameworkFixture.exact_version}" do
52
+
53
+ include Rack::Test::Methods
54
+
55
+ def app
56
+ FrameworkFixture.app.call
57
+ end
58
+
59
+ before(:each) do
60
+ before_each true, false
61
+ end
62
+
63
+ it "should have a pulse" do
64
+ get "/pulse"
65
+ last_response.body.should == '1'
66
+ end
67
+
68
+ it "should have valid schema" do
69
+ get "/should_have_valid_schema_action"
70
+ last_response.body.should == '1'
71
+ end
72
+
73
+ it "should create records" do
74
+ get "/should_create_records_action"
75
+ last_response.body.should == '1'
76
+ end
77
+
78
+ describe :migrate_from_acts_as_paranoid do
79
+ it "should migrate record and preserve deleted_at" do
80
+ get "/should_migrate_record_and_preserve_deleted_at_action"
81
+ last_response.body.should == '1'
82
+ end
83
+ end
84
+
85
+ describe :restore_all do
86
+ it "should emulate delete_all" do
87
+ get "/should_emulate_delete_all_action"
88
+ last_response.body.should == '1'
89
+ end
90
+ end
91
+
92
+ %w(delete delete_all destroy destroy_all).each do |type|
93
+ describe type do
94
+ it "should move records to archive tables" do
95
+ get "/should_move_records_to_archive_tables_action", :type => type
96
+ last_response.body.should == '1'
97
+ end
98
+
99
+ it "should move records back to original tables" do
100
+ get "/should_move_records_back_to_original_tables_action", :type => type
101
+ last_response.body.should == '1'
102
+ end
103
+ end
104
+
105
+ describe "#{type}!" do
106
+ it "should delete records without archiving" do
107
+ get "/should_delete_records_without_archiving_action", :type => type
108
+ last_response.body.should == '1'
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end