has_archive 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/has_archive.rb +17 -5
- data/lib/has_archive/migration_exists_exception.rb +7 -0
- data/lib/has_archive/migration_manager.rb +58 -0
- data/lib/has_archive/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ea41cc24909aa00d12d670bb87b110c61b0c26d0
|
|
4
|
+
data.tar.gz: afdb00510af34d31e7e74520202af15fcd827010
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 152d2c9f84f6ffd3d2fdab88344bd4bda502760fb3e25e88154bc5fdd7c7841ab9d4f169df1471869874b00b126913b07ec8baaa88d9df5f6c0cd632e1d12c0b
|
|
7
|
+
data.tar.gz: 24f58131b74c3087aaa406eb3d3291885564e18304ec71017644ed3bab7baeb82a6381b9d3af11f75eda888198e91d69ce8ab4dd9652e949664dd5ce67e6a804
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# HasArchive
|
|
2
2
|
|
|
3
|
-
This project was born out of frustration with all the options I've found so far for archiving non-current ActiveRecord backed records, so I'm going to give it a go.
|
|
3
|
+
This project was born out of frustration with all the options I've found so far for archiving non-current ActiveRecord backed records, so I'm going to give it a go. The initial target will be Rails 4 (point two-ish) on Postgres, and if it goes somewhere, I will begin looking at what else we can support.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
data/lib/has_archive.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
require "has_archive/version"
|
|
2
|
-
require "has_archive/hook"
|
|
3
2
|
require "has_archive/railtie" if defined?(Rails)
|
|
3
|
+
require "has_archive/hook"
|
|
4
|
+
require "has_archive/migration_manager"
|
|
4
5
|
|
|
5
6
|
module HasArchive
|
|
6
7
|
def self.included(base)
|
|
@@ -15,15 +16,26 @@ module HasArchive
|
|
|
15
16
|
end
|
|
16
17
|
|
|
17
18
|
module ClassMethods
|
|
18
|
-
def
|
|
19
|
-
|
|
19
|
+
def archive
|
|
20
|
+
self::Archive
|
|
20
21
|
end
|
|
21
22
|
end
|
|
22
23
|
|
|
23
24
|
module InstanceMethods
|
|
24
25
|
def archive
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
archive = self.class::Archive.new(self.attributes)
|
|
27
|
+
archive.archived_at = Time.now
|
|
28
|
+
archive.save
|
|
29
|
+
self.destroy(for_real: true)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def destroy(for_real: false)
|
|
33
|
+
if !for_real && Rails.configuration.has_archive.override_destroy
|
|
34
|
+
# puts "destroy has been overridden..."
|
|
35
|
+
archive
|
|
36
|
+
else
|
|
37
|
+
super()
|
|
38
|
+
end
|
|
27
39
|
end
|
|
28
40
|
end
|
|
29
41
|
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require 'has_archive/migration_exists_exception'
|
|
2
|
+
|
|
3
|
+
module HasArchive
|
|
4
|
+
class MigrationManager
|
|
5
|
+
def self.create_archive_for(model)
|
|
6
|
+
@table_name = model.table_name
|
|
7
|
+
|
|
8
|
+
fail MigrationExistsException.new(@table_name) if Dir["db/migrate/*_create_archive_for_#{@table_name}.rb"].any?
|
|
9
|
+
|
|
10
|
+
template = <<-MIGRATION
|
|
11
|
+
class CreateArchiveFor#{@table_name.camelize} < ActiveRecord::Migration
|
|
12
|
+
def change
|
|
13
|
+
create_table :#{@table_name.singularize}_archives do |t|
|
|
14
|
+
#{columns.map {|c| build_column(c) }.join("\n") }
|
|
15
|
+
t.datetime :archived_at, null: false
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
#{indexes.map {|i| build_index(i) }.join("\n") }
|
|
19
|
+
end
|
|
20
|
+
end\n
|
|
21
|
+
MIGRATION
|
|
22
|
+
|
|
23
|
+
File.write("db/migrate/#{file_name('create_archive_for')}", template)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.update_archive_for(model)
|
|
27
|
+
@table_name = model.table_name
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def self.columns
|
|
33
|
+
ActiveRecord::Base.connection.columns(@table_name).select {|c| c.name != 'id' }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.indexes
|
|
37
|
+
ActiveRecord::Base.connection.indexes(@table_name)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def self.build_column(c)
|
|
41
|
+
limit = c.limit ? ", limit: #{c.limit}" : ''
|
|
42
|
+
" t.#{c.type}#{' ' * (12 - c.type.size)}:#{c.name}#{limit}"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.build_index(i)
|
|
46
|
+
# unique = i.unique ? ', unique: true' : ''
|
|
47
|
+
columns = i.columns.one? ? i.columns.first : i.columns
|
|
48
|
+
# " add_index :#{@table_name}, :#{columns}#{unique}"
|
|
49
|
+
" add_index :#{@table_name.singularize}_archives, :#{columns}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.file_name(action)
|
|
53
|
+
action = action.underscore.gsub(/\s+/, '_')
|
|
54
|
+
# TODO: figure out how number argument is actually supposed to be used
|
|
55
|
+
"#{ActiveRecord::Migration.next_migration_number(1)}_#{action}_#{@table_name}.rb"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
data/lib/has_archive/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: has_archive
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matt Morris
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-01-
|
|
11
|
+
date: 2016-01-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -70,6 +70,8 @@ files:
|
|
|
70
70
|
- has_archive.gemspec
|
|
71
71
|
- lib/has_archive.rb
|
|
72
72
|
- lib/has_archive/hook.rb
|
|
73
|
+
- lib/has_archive/migration_exists_exception.rb
|
|
74
|
+
- lib/has_archive/migration_manager.rb
|
|
73
75
|
- lib/has_archive/railtie.rb
|
|
74
76
|
- lib/has_archive/version.rb
|
|
75
77
|
homepage: http://github.com/matt-morris/has_archive
|
|
@@ -91,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
91
93
|
version: '0'
|
|
92
94
|
requirements: []
|
|
93
95
|
rubyforge_project:
|
|
94
|
-
rubygems_version: 2.
|
|
96
|
+
rubygems_version: 2.2.0
|
|
95
97
|
signing_key:
|
|
96
98
|
specification_version: 4
|
|
97
99
|
summary: Add archives to your ActiveRecord models
|