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 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
- Add to models
38
- -------------
36
+ class Application &lt; Sinatra::Base
37
+ include ActsAsArchive::Adapters::Sinatra
38
+ end
39
+ </pre>
39
40
 
40
- Add <code>acts\_as\_archive</code> to your models:
41
+ config/acts\_as\_archive.yml
42
+ ----------------------------
41
43
 
42
44
  <pre>
43
- class Article &lt; ActiveRecord::Base
44
- acts_as_archive
45
- end
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
- Next time you run <code>rake db:migrate</code>, your archive tables will be created automatically.
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 <code>acts\_as\_archive</code> model's relationship has the <code>:dependent</code> option and also uses <code>acts\_as\_archive</code>, that relationship will archive automatically.
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
- Add <code>::Archive</code> to your ActiveRecord class:
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
@@ -3,8 +3,8 @@ acts_as_archive:
3
3
  rspec: ~>1.0
4
4
  default:
5
5
  active_wrapper-solo: =0.4.4
6
- also_migrate: =0.3.4
6
+ also_migrate: =0.3.5
7
7
  externals: =1.0.2
8
8
  framework_fixture: =0.1.3
9
9
  mover: =0.3.6
10
- rack-test: =0.5.7
10
+ rack-test: =0.5.6
data/config/gemspec.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  name: acts_as_archive
2
- version: 0.3.4
2
+ version: 0.4.0
3
3
  authors:
4
4
  - Winton Welsh
5
5
  email: mail@wintoni.us
@@ -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 move(config, where, merge_options={})
45
- config[:to].each do |to|
46
- options = config[:options].dup.merge(merge_options)
47
- if options[:conditions]
48
- options[:conditions] += " AND #{where}"
49
- elsif where
50
- options[:conditions] = where
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(*args)
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
- if args.empty?
89
- class_eval <<-EVAL
90
- class Archive < ActiveRecord::Base
91
- set_table_name "archived_#{self.table_name}"
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
- args << self::Archive
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
- args.each do |klass|
98
- klass.class_eval <<-EVAL
99
- record_timestamps = #{options[:timestamps].inspect}
100
- acts_as_archive(#{self}, :archive => true)
101
- EVAL
102
- self.reflect_on_all_associations.each do |association|
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] = args
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)
@@ -0,0 +1,9 @@
1
+ if Rails.root.nil?
2
+ class ActsAsArchiveRailtie < Rails::Railtie
3
+ initializer "acts_as_archive" do
4
+ ActsAsArchive.load_from_yaml(Rails.root)
5
+ end
6
+ end
7
+ else
8
+ ActsAsArchive.load_from_yaml(Rails.root)
9
+ end
@@ -0,0 +1,12 @@
1
+ class ActsAsArchive
2
+ module Adapters
3
+ module Sinatra
4
+
5
+ def self.included(klass)
6
+ if klass.root
7
+ ActsAsArchive.load_from_yaml(klass.root)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ Record:
2
+ - class: Record::Archive
3
+ table: archived_records
@@ -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
@@ -5,6 +5,8 @@ gem 'mysql2'
5
5
  gem 'rails', '3.0.3'
6
6
  gem 'rspec'
7
7
 
8
+ gem 'also_migrate', :path => "../../../../vendor/also_migrate"
9
+ gem 'mover', :path => "../../../../vendor/mover"
8
10
  gem 'acts_as_archive', :path => "../../../../"
9
11
 
10
12
  # Bundle edge Rails instead:
@@ -8,6 +8,7 @@ class Application < Sinatra::Base
8
8
  set :raise_errors, true
9
9
  set :show_exceptions, false
10
10
 
11
+ include ActsAsArchive::Adapters::Sinatra
11
12
  include SpecHelper
12
13
 
13
14
  get '/pulse' do
@@ -11,6 +11,4 @@ class Record < ActiveRecord::Base
11
11
 
12
12
  has_one :has_one_through_through, :dependent => :destroy
13
13
  has_one :has_one_through, :dependent => :destroy, :through => :has_one_through_through
14
-
15
- acts_as_archive
16
14
  end
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
- version: 0.3.4
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-20 00:00:00 -08:00
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
- - 4
32
- version: 0.3.4
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