acts_as_archivable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cff6426e1832c13129e9e437e184bf50eca1d458
4
+ data.tar.gz: 23055f73081f54aef61bee39dfe902a8e54f119e
5
+ SHA512:
6
+ metadata.gz: c2bedb6b66769f7f8e2282c17addb49e35e9a3a47293ddbcf1ea9a9ec4c70e6d82973371efda718bf3bae7f7e43d19c93b7f3cf9ab59951f32c1f00dd75d9a73
7
+ data.tar.gz: 4b1e6c118df59bca4e9aa4e324ee567f48bc54ae4cdbef01111daad825fd2b143e5e5a60ce9e2e6bfe8c9b94da64f441df0dc5b3983b40fcc4abf78c7f48e10c
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_archivable.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec-core'
@@ -0,0 +1,52 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), then you will want to move
18
+ ## the Guardfile to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ # Note: The cmd option is now required due to the increasing number of ways
27
+ # rspec may be run, below are examples of the most common uses.
28
+ # * bundler: 'bundle exec rspec'
29
+ # * bundler binstubs: 'bin/rspec'
30
+ # * spring: 'bin/rspec' (This will use spring if running and you have
31
+ # installed the spring binstubs per the docs)
32
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
33
+ # * 'just' rspec: 'rspec'
34
+
35
+ guard :rspec, cmd: 'bundle exec rspec' do
36
+ require 'guard/rspec/dsl'
37
+ dsl = Guard::RSpec::Dsl.new(self)
38
+
39
+ # Feel free to open issues for suggestions and improvements
40
+
41
+ # RSpec files
42
+ rspec = dsl.rspec
43
+ watch(rspec.spec_helper) { rspec.spec_dir }
44
+ watch(rspec.spec_support) { rspec.spec_dir }
45
+ watch(rspec.spec_files)
46
+
47
+ watch(%r{^lib/acts_as_archivable/(.+)/(.+)\.rb$}) { |m| "spec/#{m[1]}/#{m[2]}_spec.rb" }
48
+
49
+ # Ruby files
50
+ ruby = dsl.ruby
51
+ dsl.watch_spec_files_for(ruby.lib_files)
52
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jon Collier
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # ActsAsArchivable
2
+
3
+ ActsAsArchivable makes it super easy to move database records to an archive table.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'acts_as_archivable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## WARNING
18
+ This gem contains an `eval` call! `lib/acts_as_archivable/migrating.rb` calls `eval(archived_table_schema)` in order to create the archived version of a given table.
19
+
20
+ ## Usage
21
+ ### Setup
22
+
23
+ class Comment < ActiveRecord::Base
24
+ acts_as_archivable months_to_keep: 2
25
+ end
26
+
27
+ ### Scopes
28
+
29
+ # all records older than 2 months
30
+ Comment.to_be_archived
31
+
32
+
33
+ ### Moving Rows
34
+
35
+ # Creates an ArchivedComment, destroys this comment
36
+ @comment.move_to_archive
37
+
38
+ # Move appropriate rows to the archive
39
+ Comment.to_be_archived.find_each(&:move_to_archive)
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'acts_as_archivable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'acts_as_archivable'
8
+ spec.version = ActsAsArchivable::VERSION
9
+ spec.authors = ['Jon Collier']
10
+ spec.email = ['jcollier@atlantistech.com']
11
+ spec.summary = 'ActsAsArchivable makes it super easy '\
12
+ 'to move database records to an archive table.'
13
+ spec.description = 'There has never been a better time '\
14
+ 'to move database records to an archive table.'
15
+ spec.homepage = ''
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'activerecord'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.7'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'guard'
29
+ spec.add_development_dependency 'guard-rspec'
30
+ end
@@ -0,0 +1,29 @@
1
+ require 'active_record'
2
+ require 'active_record/version'
3
+ require 'active_support/core_ext/module'
4
+
5
+ require 'acts_as_archivable/version'
6
+ require 'acts_as_archivable/naming'
7
+ require 'acts_as_archivable/schema'
8
+ require 'acts_as_archivable/migrating'
9
+ require 'acts_as_archivable/instance_methods'
10
+
11
+ module ActsAsArchivable
12
+ def acts_as_archivable(options = {})
13
+ extend ActsAsArchivable::Naming
14
+ extend ActsAsArchivable::Schema
15
+ extend ActsAsArchivable::Migrating
16
+
17
+ options[:months_to_keep] ||= 2
18
+
19
+ scope :to_be_archived, -> { where('created_at < ?', options[:months_to_keep].months.ago) }
20
+
21
+ Object.const_set(archived_class_name, Class.new(self))
22
+ archived_class.table_name = archived_table_name
23
+ end
24
+ end
25
+
26
+ ActiveSupport.on_load(:active_record) do
27
+ extend ActsAsArchivable
28
+ include ActsAsArchivable::InstanceMethods
29
+ end
@@ -0,0 +1,9 @@
1
+ module ActsAsArchivable
2
+ module InstanceMethods
3
+ def move_to_archive
4
+ archived_object = self.class.archived_class.create(attributes)
5
+ destroy
6
+ archived_object
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module ActsAsArchivable
2
+ module Migrating
3
+ def create_archived_table
4
+ return if ActiveRecord::Migration.table_exists?(archived_table_name)
5
+ eval(archived_table_schema)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,15 @@
1
+ module ActsAsArchivable
2
+ module Naming
3
+ def archived_table_name
4
+ "archived_#{table_name}"
5
+ end
6
+
7
+ def archived_class_name
8
+ "Archived#{self}"
9
+ end
10
+
11
+ def archived_class
12
+ archived_class_name.constantize
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ module ActsAsArchivable
2
+ module Schema
3
+ def table_schema
4
+ schema = ActiveRecord::SchemaDumper
5
+ .send(:new, ActiveRecord::Base.connection)
6
+ .send(:table, table_name, Tempfile.new('temporary_schema'))
7
+ schema.rewind
8
+ schema.read
9
+ end
10
+
11
+ def archived_table_schema
12
+ table_schema
13
+ .gsub(/#{table_name}/, archived_table_name)
14
+ .gsub(/(create_table|add_index)/, 'ActiveRecord::Migration.\1')
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsArchivable
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsArchivable::Migrating do
4
+ before(:all) do
5
+ class PageRequest < ActiveRecord::Base
6
+ acts_as_archivable
7
+ end
8
+ end
9
+ subject! { PageRequest }
10
+
11
+ it { is_expected.to respond_to :create_archived_table }
12
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsArchivable::Naming do
4
+ before(:all) do
5
+ class PageRequest < ActiveRecord::Base
6
+ acts_as_archivable
7
+ end
8
+ end
9
+ subject! { PageRequest }
10
+
11
+ it 'identifies archived class name' do
12
+ expect(subject.archived_class_name).to eq 'ArchivedPageRequest'
13
+ end
14
+
15
+ it 'identifies archived table name' do
16
+ expect(subject.archived_table_name).to eq 'archived_page_requests'
17
+ end
18
+
19
+ it 'identifies archived class' do
20
+ expect(subject.archived_class).to eq ArchivedPageRequest
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActsAsArchivable::Schema do
4
+ before(:all) do
5
+ class PageRequest < ActiveRecord::Base
6
+ acts_as_archivable
7
+ end
8
+ end
9
+ subject! { PageRequest }
10
+ let!(:schema) do
11
+ " create_table \"page_requests\", force: :cascade do |t|\n t.integer \"monitored_page_id\", limit: 4\n t.integer \"status\", limit: 4\n t.float \"response_time\", limit: 24\n t.datetime \"created_at\", null: false\n t.datetime \"updated_at\", null: false\n t.string \"proxy\", limit: 255\n end\n\n add_index \"page_requests\", [\"proxy\"], name: \"index_page_requests_on_proxy\", using: :btree\n\n"
12
+ end
13
+
14
+ it { is_expected.to respond_to :table_schema }
15
+ it { is_expected.to respond_to :archived_table_schema }
16
+
17
+ it 'formats an archived schema' do
18
+ allow(subject).to receive(:table_schema).and_return schema
19
+ expect(subject.archived_table_schema).to include 'ActiveRecord::Migration.create_table "archived_page_requests"'
20
+ expect(subject.archived_table_schema).to include 'ActiveRecord::Migration.add_index "archived_page_requests"'
21
+ puts subject.archived_table_schema
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ require 'pry'
2
+ require 'acts_as_archivable'
3
+
4
+ RSpec.configure do |config|
5
+ # config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+
9
+ config.expect_with :rspec do |_c|
10
+ # c.syntax = :should
11
+ end
12
+
13
+ # config.mock_framework = :mocha
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_archivable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Collier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: There has never been a better time to move database records to an archive
98
+ table.
99
+ email:
100
+ - jcollier@atlantistech.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - Guardfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - acts_as_archivable.gemspec
112
+ - lib/acts_as_archivable.rb
113
+ - lib/acts_as_archivable/instance_methods.rb
114
+ - lib/acts_as_archivable/migrating.rb
115
+ - lib/acts_as_archivable/naming.rb
116
+ - lib/acts_as_archivable/schema.rb
117
+ - lib/acts_as_archivable/version.rb
118
+ - spec/migrating_spec.rb
119
+ - spec/naming_spec.rb
120
+ - spec/schema_spec.rb
121
+ - spec/spec_helper.rb
122
+ homepage: ''
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: ActsAsArchivable makes it super easy to move database records to an archive
146
+ table.
147
+ test_files:
148
+ - spec/migrating_spec.rb
149
+ - spec/naming_spec.rb
150
+ - spec/schema_spec.rb
151
+ - spec/spec_helper.rb