lite-archive 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eab433e896044aa98e3b783ad2cff008d0dd3e4f673cf3334690449c33d295e7
4
+ data.tar.gz: f54b9c5798ce6e4b79deaeb26c4246a39efb5e849a876831e0a850b9f9323cb0
5
+ SHA512:
6
+ metadata.gz: 34db46befa8329a1343249d3dbd24cdb6010e636fa0cfe2a1d291eb1c0a24513cf74d792b9389d0a99296dd0735102a62fabfe7ffefa83ae46b04eb79f2f0d3d
7
+ data.tar.gz: 3ca9d5a415a4074eef9763b04eb87093c941da6f35ed6da29de9f91c59e9c54829a8604685999a89dcfebe63ce8c592d88d9fb8023ea5e2e60bce78e88890180
@@ -0,0 +1,19 @@
1
+ speedups:
2
+ block_vs_symbol_to_proc: true
3
+ each_with_index_vs_while: false
4
+ fetch_with_argument_vs_block: true
5
+ for_loop_vs_each: true
6
+ getter_vs_attr_reader: true
7
+ gsub_vs_tr: true
8
+ hash_merge_bang_vs_hash_brackets: true
9
+ keys_each_vs_each_key: true
10
+ map_flatten_vs_flat_map: true
11
+ module_eval: true
12
+ proc_call_vs_yield: true
13
+ rescue_vs_respond_to: true
14
+ reverse_each_vs_reverse_each: true
15
+ select_first_vs_detect: true
16
+ select_last_vs_reverse_detect: true
17
+ setter_vs_attr_writer: true
18
+ shuffle_first_vs_sample: true
19
+ sort_vs_sort_by: true
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --backtrace
2
+ --color
3
+ --format progress
4
+ --order random
@@ -0,0 +1,28 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+ AllCops:
5
+ TargetRubyVersion: 2.6
6
+ DisplayCopNames: true
7
+ DisplayStyleGuide: true
8
+ LineLength:
9
+ Max: 100
10
+ Layout/EmptyLinesAroundBlockBody:
11
+ Exclude:
12
+ - 'spec/**/**/*'
13
+ Layout/EmptyLinesAroundClassBody:
14
+ EnforcedStyle: empty_lines_except_namespace
15
+ Layout/EmptyLinesAroundModuleBody:
16
+ EnforcedStyle: empty_lines_except_namespace
17
+ Metrics/BlockLength:
18
+ Exclude:
19
+ - 'spec/**/**/*'
20
+ - '*.gemspec'
21
+ Metrics/ModuleLength:
22
+ Enabled: false
23
+ RSpec/MultipleExpectations:
24
+ Enabled: false
25
+ Style/Documentation:
26
+ Enabled: false
27
+ Style/ExpandPathArguments:
28
+ Enabled: false
@@ -0,0 +1,24 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5
6
+ - 2.6
7
+ - ruby-head
8
+ matrix:
9
+ fast_finish: true
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+ before_install:
13
+ - gem update --system
14
+ - gem install bundler
15
+ install:
16
+ - bundle install --jobs=3 --retry=3
17
+ script:
18
+ - bundle exec rspec
19
+ - bundle exec rubocop
20
+ - bundle exec fasterer
21
+ notifications:
22
+ email: false
23
+ slack:
24
+ secure: VdEizS3D0v5cSQBgPDMooq5mGwf2sYQALnt0MF3dr/hLK36IeLI6FR5GkiC+qWXW88QnDm2i1HbgeM5SlSUZfU9EOGrnGIzCUe690q7XTI+N35P/Op59Ma1fOV2ojEjg2wwJzUVtfS6fYMzI0mfNAN8gg9ASN1fHtCnh5VK9AECC4931b0TxdwrJECRE+hzDSRGcmjqX+Nam0jj6GzJt0blBMcHpJAd+Ari/gGcb5v3+rBdzPLY79/HECZvzPORFDOqtgWKHv7gWQ44HEyGwJXp0fzBb1G1CCAwxMldqv7Jz4lPaADulFaAJOPMaNjTYIajhhjW8nG4zAav45cFhctpW/9YujYHBe9PnhmFFSopBGTJzXYJnAmTzrDKupZOxrLvx4ZWR+DRs7F6v73rPlKYpnmaEmy+wbp4Awju7Shc64veuCohw7sVMivUH634u6Yd5ezh22nyTb541i1RPwy0T7E9nRBXTz5sFnymQGUuvUfBPpRwDJUVxsBO86xofHHzY7YgKo2haiZP07wRX0LVxrnVyD31vMY+oTMAlvHK2WzGnX757gcXbwtLEb3qD8WYEi0mGVHSVo3CvYggv3mTCpgR2U3t+Wl9oxFDbNNQulaomSheBWVBcncYeftBLdZcLFTBtx/WbT/rppS4XxYrVkfGraJ17P6kGnhJCbmo=
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.0.0] - 2019-07-01
10
+ ### Added
11
+ - Initial project version
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at j.gomez@drexed.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in lite-archive.gemspec
6
+ gemspec
@@ -0,0 +1,141 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lite-archive (1.0.0)
5
+ activerecord
6
+ activesupport
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (5.2.3)
12
+ actionview (= 5.2.3)
13
+ activesupport (= 5.2.3)
14
+ rack (~> 2.0)
15
+ rack-test (>= 0.6.3)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
18
+ actionview (5.2.3)
19
+ activesupport (= 5.2.3)
20
+ builder (~> 3.1)
21
+ erubi (~> 1.4)
22
+ rails-dom-testing (~> 2.0)
23
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
24
+ activemodel (5.2.3)
25
+ activesupport (= 5.2.3)
26
+ activerecord (5.2.3)
27
+ activemodel (= 5.2.3)
28
+ activesupport (= 5.2.3)
29
+ arel (>= 9.0)
30
+ activesupport (5.2.3)
31
+ concurrent-ruby (~> 1.0, >= 1.0.2)
32
+ i18n (>= 0.7, < 2)
33
+ minitest (~> 5.1)
34
+ tzinfo (~> 1.1)
35
+ arel (9.0.0)
36
+ ast (2.4.0)
37
+ builder (3.2.3)
38
+ colorize (0.8.1)
39
+ concurrent-ruby (1.1.5)
40
+ crass (1.0.4)
41
+ database_cleaner (1.7.0)
42
+ diff-lcs (1.3)
43
+ erubi (1.8.0)
44
+ fasterer (0.5.1)
45
+ colorize (~> 0.7)
46
+ ruby_parser (>= 3.13.0)
47
+ generator_spec (0.9.4)
48
+ activesupport (>= 3.0.0)
49
+ railties (>= 3.0.0)
50
+ i18n (1.6.0)
51
+ concurrent-ruby (~> 1.0)
52
+ jaro_winkler (1.5.3)
53
+ loofah (2.2.3)
54
+ crass (~> 1.0.2)
55
+ nokogiri (>= 1.5.9)
56
+ method_source (0.9.2)
57
+ mini_portile2 (2.4.0)
58
+ minitest (5.11.3)
59
+ nokogiri (1.10.3)
60
+ mini_portile2 (~> 2.4.0)
61
+ parallel (1.17.0)
62
+ parser (2.6.3.0)
63
+ ast (~> 2.4.0)
64
+ rack (2.0.7)
65
+ rack-test (1.1.0)
66
+ rack (>= 1.0, < 3)
67
+ rails-dom-testing (2.0.3)
68
+ activesupport (>= 4.2.0)
69
+ nokogiri (>= 1.6)
70
+ rails-html-sanitizer (1.0.4)
71
+ loofah (~> 2.2, >= 2.2.2)
72
+ railties (5.2.3)
73
+ actionpack (= 5.2.3)
74
+ activesupport (= 5.2.3)
75
+ method_source
76
+ rake (>= 0.8.7)
77
+ thor (>= 0.19.0, < 2.0)
78
+ rainbow (3.0.0)
79
+ rake (12.3.2)
80
+ rspec (3.8.0)
81
+ rspec-core (~> 3.8.0)
82
+ rspec-expectations (~> 3.8.0)
83
+ rspec-mocks (~> 3.8.0)
84
+ rspec-core (3.8.2)
85
+ rspec-support (~> 3.8.0)
86
+ rspec-expectations (3.8.4)
87
+ diff-lcs (>= 1.2.0, < 2.0)
88
+ rspec-support (~> 3.8.0)
89
+ rspec-mocks (3.8.1)
90
+ diff-lcs (>= 1.2.0, < 2.0)
91
+ rspec-support (~> 3.8.0)
92
+ rspec-rails (3.8.2)
93
+ actionpack (>= 3.0)
94
+ activesupport (>= 3.0)
95
+ railties (>= 3.0)
96
+ rspec-core (~> 3.8.0)
97
+ rspec-expectations (~> 3.8.0)
98
+ rspec-mocks (~> 3.8.0)
99
+ rspec-support (~> 3.8.0)
100
+ rspec-support (3.8.2)
101
+ rubocop (0.72.0)
102
+ jaro_winkler (~> 1.5.1)
103
+ parallel (~> 1.10)
104
+ parser (>= 2.6)
105
+ rainbow (>= 2.2.2, < 4.0)
106
+ ruby-progressbar (~> 1.7)
107
+ unicode-display_width (>= 1.4.0, < 1.7)
108
+ rubocop-performance (1.4.0)
109
+ rubocop (>= 0.71.0)
110
+ rubocop-rspec (1.33.0)
111
+ rubocop (>= 0.60.0)
112
+ ruby-progressbar (1.10.1)
113
+ ruby_parser (3.13.1)
114
+ sexp_processor (~> 4.9)
115
+ sexp_processor (4.12.1)
116
+ sqlite3 (1.4.1)
117
+ thor (0.20.3)
118
+ thread_safe (0.3.6)
119
+ tzinfo (1.2.5)
120
+ thread_safe (~> 0.1)
121
+ unicode-display_width (1.6.0)
122
+
123
+ PLATFORMS
124
+ ruby
125
+
126
+ DEPENDENCIES
127
+ bundler
128
+ database_cleaner
129
+ fasterer
130
+ generator_spec
131
+ lite-archive!
132
+ rake
133
+ rspec
134
+ rspec-rails
135
+ rubocop
136
+ rubocop-performance
137
+ rubocop-rspec
138
+ sqlite3
139
+
140
+ BUNDLED WITH
141
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Juan Gomez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,126 @@
1
+ # Lite::Archive
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/lite-archive.svg)](http://badge.fury.io/rb/lite-archive)
4
+ [![Build Status](https://travis-ci.org/drexed/lite-archive.svg?branch=master)](https://travis-ci.org/drexed/lite-archive)
5
+
6
+ Lite::Archive is a library for archiving (soft-delete) database records.
7
+
8
+ **NOTE:** If you are coming from `ActiveArchive`, please read the [port](#port) section.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'lite-archive'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install lite-archive
25
+
26
+ ## Table of Contents
27
+
28
+ * [Configurations](#configurations)
29
+ * [Usage](#usage)
30
+ * [Methods](#methods)
31
+ * [Scopes](#scopes)
32
+ * [Callbacks](#callbacks)
33
+ * [Port](#port)
34
+
35
+ ## Configurations
36
+
37
+ `rails g lite:archive:install` will generate the following file:
38
+ `../config/initalizers/lite-archive.rb`
39
+
40
+ ```ruby
41
+ Lite::Archive.configure do |config|
42
+ config.all_records_archivable = false
43
+ end
44
+ ```
45
+
46
+ ## Usage
47
+
48
+ An `archived_at` column must be added to tables where you do not want records destroyed.
49
+ If the table does not have this column, records will be destroy instead of archived.
50
+
51
+ ```ruby
52
+ class AddArchivedAtColumns < ActiveRecord::Migration
53
+ def change
54
+ # Adds archived_at automatically (if all_records_archivable is set to true)
55
+ t.timestamp
56
+
57
+ # Adds archived_at timestamp
58
+ t.timestamp archive: true
59
+
60
+ # Does NOT add archived_at timestamp
61
+ t.timestamp archive: false
62
+
63
+ # Manual column (null constraint must be false)
64
+ add_column :your_model, :archived_at, :datetime
65
+ end
66
+ end
67
+ ```
68
+
69
+ ## Methods
70
+
71
+ ```ruby
72
+ user = User.first
73
+ user.archive #=> Archives the User record and its dependents
74
+ user.unarchive #=> Unarchives the User record and its dependents
75
+ user.to_archival #=> Returns the User archival state locale string (ex: Archived)
76
+
77
+ User.archive_all #=> Archives all User records and their dependents
78
+ User.unarchive_all #=> Unarchives all User record and their dependents
79
+ ```
80
+
81
+ ## Scopes
82
+
83
+ ```ruby
84
+ User.archived.all #=> Returns only archived records
85
+ User.unarchived.all #=> Returns only unarchived records
86
+ ```
87
+
88
+ ## Callbacks
89
+
90
+ ```ruby
91
+ class User < ActiveRecord::Base
92
+ # ... omitted ...
93
+
94
+ before_archive :do_something
95
+ after_archive :do_something
96
+
97
+ before_unarchived :do_something
98
+ after_unarchive :do_something
99
+
100
+ # ... omitted ...
101
+ end
102
+ ```
103
+
104
+ ## Port
105
+
106
+ `Lite::Archive` is compatible port of [ActiveArchive](https://github.com/drexed/active_archive).
107
+
108
+ Switching is as easy as renaming `ActiveArchive` to `Lite::Archive`.
109
+
110
+ ## Development
111
+
112
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
113
+
114
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
115
+
116
+ ## Contributing
117
+
118
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lite-archive. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
119
+
120
+ ## License
121
+
122
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
123
+
124
+ ## Code of Conduct
125
+
126
+ Everyone interacting in the Lite::Archive project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lite-archive/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-leap-day
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'lite/archive'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ en:
2
+ lite:
3
+ archive:
4
+ archival:
5
+ archived: 'Archived'
6
+ unarchived: 'Unarchived'
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Lite
6
+ module Archive
7
+ class InstallGenerator < Rails::Generators::Base
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def copy_initializer_file
12
+ copy_file('install.rb', 'config/initializers/lite-archive.rb')
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Lite::Archive.configure do |config|
4
+ config.all_records_archivable = false
5
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'active_support'
5
+
6
+ require 'lite/archive/version'
7
+ require 'lite/archive/railtie' if defined?(Rails)
8
+
9
+ %w[configuration table_definition methods scopes base].each do |name|
10
+ require "lite/archive/#{name}"
11
+ end
12
+
13
+ require 'generators/lite/archive/install_generator'
@@ -0,0 +1,158 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Archive
5
+ module Base
6
+
7
+ def self.included(base)
8
+ base.extend Methods
9
+ base.extend Scopes
10
+
11
+ base.instance_eval do
12
+ %i[archive unarchive].each { |name| define_model_callbacks name, only: %i[before after] }
13
+ end
14
+ end
15
+
16
+ def archival?
17
+ return destroyed? if unarchivable?
18
+
19
+ (will_save_change_to_archived_at? || saved_change_to_archived_at?) && archived?
20
+ end
21
+
22
+ def archivable?
23
+ respond_to?(:archived_at)
24
+ end
25
+
26
+ def archived?
27
+ archivable? ? !archived_at.nil? : destroyed?
28
+ end
29
+
30
+ def archive
31
+ return destroy if unarchivable?
32
+
33
+ with_transaction_returning_status do
34
+ run_callbacks :archive do
35
+ mark_as_archived
36
+ mark_relections_as_archived
37
+ self
38
+ end
39
+ end
40
+ end
41
+
42
+ def unarchival?
43
+ return !destroyed? if unarchivable?
44
+
45
+ (will_save_change_to_archived_at? || saved_change_to_archived_at?) && unarchived?
46
+ end
47
+
48
+ def unarchived?
49
+ !archived?
50
+ end
51
+
52
+ def unarchivable?
53
+ !archivable?
54
+ end
55
+
56
+ def unarchive
57
+ return self if unarchivable?
58
+
59
+ with_transaction_returning_status do
60
+ run_callbacks :unarchive do
61
+ mark_as_unarchived
62
+ mark_relections_as_unarchived
63
+ self
64
+ end
65
+ end
66
+ end
67
+
68
+ def to_archival
69
+ I18n.t("lite.archive.archival.#{:un unless archived?}archived")
70
+ end
71
+
72
+ private
73
+
74
+ def dependent_destroy?(reflection)
75
+ reflection.options[:dependent] == :destroy
76
+ end
77
+
78
+ def mark_as_archived
79
+ ts = timestamp
80
+
81
+ self.updated_at = ts if updatable?
82
+ self.archived_at = ts
83
+ save(validate: false)
84
+ end
85
+
86
+ def mark_as_unarchived
87
+ self.updated_at = timestamp if updatable?
88
+ self.archived_at = nil
89
+ save(validate: false)
90
+ end
91
+
92
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength
93
+ def mark_relections_as_archived
94
+ self.class.reflections.each do |table_name, reflection|
95
+ next unless dependent_destroy?(reflection)
96
+
97
+ dependents = reflection_dependents(table_name)
98
+ next if dependents.nil?
99
+
100
+ action = case [reflection_marco(reflection), archivable?]
101
+ when ['one', true] then :archive
102
+ when ['one', false] then :destroy
103
+ when ['many', true] then :archive_all
104
+ when ['many', false] then :destroy_all
105
+ end
106
+
107
+ dependents.send(action)
108
+ end
109
+ end
110
+ # rubocop:enable Metrics/CyclomaticComplexity
111
+
112
+ def mark_relections_as_unarchived
113
+ self.class.reflections.each do |table_name, reflection|
114
+ next unless dependent_destroy?(reflection)
115
+
116
+ klass = relection_klass(reflection.class_name)
117
+ next unless klass.archivable?
118
+
119
+ dependents = reflection_dependents(table_name)
120
+ next if dependents.nil?
121
+
122
+ action = case reflection_marco(reflection)
123
+ when 'one' then :unarchive
124
+ when 'many' then :unarchive_all
125
+ end
126
+
127
+ dependents.send(action)
128
+ end
129
+ end
130
+ # rubocop:enable Metrics/MethodLength
131
+
132
+ def reflection_dependents(table_name)
133
+ send(table_name)
134
+ end
135
+
136
+ def relection_klass(table_name)
137
+ table_name.classify.constantize
138
+ end
139
+
140
+ def reflection_marco(reflection)
141
+ reflection.macro.to_s.gsub('has_', '')
142
+ end
143
+
144
+ def timestamp
145
+ Time.respond_to?(:current) ? Time.current : Time.now
146
+ end
147
+
148
+ def updatable?
149
+ respond_to?(:updated_at)
150
+ end
151
+
152
+ end
153
+ end
154
+ end
155
+
156
+ ActiveSupport.on_load(:active_record) do
157
+ ActiveRecord::Base.include(Lite::Archive::Base)
158
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Archive
5
+
6
+ class Configuration
7
+
8
+ attr_accessor :all_records_archivable
9
+
10
+ def initialize
11
+ @all_records_archivable = false
12
+ end
13
+
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def self.configuration=(config)
21
+ @configuration = config
22
+ end
23
+
24
+ def self.configure
25
+ yield(configuration)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Archive
5
+ module Methods
6
+
7
+ def archivable?
8
+ columns.detect { |col| col.name == 'archived_at' }
9
+ end
10
+
11
+ def archive_all(conditions = nil)
12
+ records(conditions, action: :archive)
13
+ end
14
+
15
+ def unarchive_all(conditions = nil)
16
+ records(conditions, action: :unarchive)
17
+ end
18
+
19
+ private
20
+
21
+ def records(conditions = nil, action:)
22
+ where(conditions).find_each(&action)
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Archive
5
+ class Railtie < ::Rails::Railtie
6
+
7
+ initializer 'lite-archive' do |app|
8
+ Lite::Archive::Railtie.instance_eval do
9
+ [app.config.i18n.available_locales].flatten.each do |locale|
10
+ path = File.expand_path("../../../config/locales/#{locale}.yml", __FILE__)
11
+ next if !File.file?(path) || I18n.load_path.include?(path)
12
+
13
+ I18n.load_path << path
14
+ end
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Archive
5
+ module Scopes
6
+
7
+ def archived
8
+ where.not(archived_at: nil)
9
+ end
10
+
11
+ def unarchived
12
+ where(archived_at: nil)
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Archive
5
+ module TableDefinition
6
+
7
+ def timestamps(*args)
8
+ options = args.extract_options!
9
+
10
+ column(:created_at, :datetime, options)
11
+ column(:updated_at, :datetime, options)
12
+
13
+ return unless Lite::Archive.configuration.all_records_archivable == true
14
+ return if options[:archive] == false
15
+
16
+ options[:null] = true
17
+ column(:archived_at, :datetime, options)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+
24
+ ActiveRecord::ConnectionAdapters::TableDefinition.prepend(Lite::Archive::TableDefinition)
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Archive
5
+
6
+ VERSION ||= '1.0.0'
7
+
8
+ end
9
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'lite/archive/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lite-archive'
9
+ spec.version = Lite::Archive::VERSION
10
+ spec.authors = ['Juan Gomez']
11
+ spec.email = %w[j.gomez@drexed.com]
12
+
13
+ spec.summary = 'Archive (soft-delete) ActiveRecord database records'
14
+ spec.homepage = 'http://drexed.github.io/lite-archive'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata.merge(
21
+ 'allowed_push_host' => 'https://rubygems.org',
22
+ 'changelog_uri' => 'https://github.com/drexed/lite-archive/blob/master/CHANGELOG.md',
23
+ 'homepage_uri' => spec.homepage,
24
+ 'source_code_uri' => 'https://github.com/drexed/lite-archive'
25
+ )
26
+ else
27
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
28
+ 'public gem pushes.'
29
+ end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = %w[lib]
39
+
40
+ spec.add_runtime_dependency 'activerecord'
41
+ spec.add_runtime_dependency 'activesupport'
42
+
43
+ spec.add_development_dependency 'bundler'
44
+ spec.add_development_dependency 'database_cleaner'
45
+ spec.add_development_dependency 'fasterer'
46
+ spec.add_development_dependency 'generator_spec'
47
+ spec.add_development_dependency 'rake'
48
+ spec.add_development_dependency 'rspec'
49
+ spec.add_development_dependency 'rspec-rails'
50
+ spec.add_development_dependency 'rubocop'
51
+ spec.add_development_dependency 'rubocop-performance'
52
+ spec.add_development_dependency 'rubocop-rspec'
53
+ spec.add_development_dependency 'sqlite3'
54
+ end
metadata ADDED
@@ -0,0 +1,252 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lite-archive
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juan Gomez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-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: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: database_cleaner
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: fasterer
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: generator_spec
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
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec-rails
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-performance
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: sqlite3
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ description:
196
+ email:
197
+ - j.gomez@drexed.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".fasterer.yml"
203
+ - ".gitignore"
204
+ - ".rspec"
205
+ - ".rubocop.yml"
206
+ - ".travis.yml"
207
+ - CHANGELOG.md
208
+ - CODE_OF_CONDUCT.md
209
+ - Gemfile
210
+ - Gemfile.lock
211
+ - LICENSE.txt
212
+ - README.md
213
+ - Rakefile
214
+ - _config.yml
215
+ - bin/console
216
+ - bin/setup
217
+ - config/locales/en.yml
218
+ - lib/generators/lite/archive/install_generator.rb
219
+ - lib/generators/lite/archive/templates/install.rb
220
+ - lib/lite/archive.rb
221
+ - lib/lite/archive/base.rb
222
+ - lib/lite/archive/configuration.rb
223
+ - lib/lite/archive/methods.rb
224
+ - lib/lite/archive/railtie.rb
225
+ - lib/lite/archive/scopes.rb
226
+ - lib/lite/archive/table_definition.rb
227
+ - lib/lite/archive/version.rb
228
+ - lite-archive.gemspec
229
+ homepage: http://drexed.github.io/lite-archive
230
+ licenses:
231
+ - MIT
232
+ metadata: {}
233
+ post_install_message:
234
+ rdoc_options: []
235
+ require_paths:
236
+ - lib
237
+ required_ruby_version: !ruby/object:Gem::Requirement
238
+ requirements:
239
+ - - ">="
240
+ - !ruby/object:Gem::Version
241
+ version: '0'
242
+ required_rubygems_version: !ruby/object:Gem::Requirement
243
+ requirements:
244
+ - - ">="
245
+ - !ruby/object:Gem::Version
246
+ version: '0'
247
+ requirements: []
248
+ rubygems_version: 3.0.4
249
+ signing_key:
250
+ specification_version: 4
251
+ summary: Archive (soft-delete) ActiveRecord database records
252
+ test_files: []