discard 0.1.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
+ SHA1:
3
+ metadata.gz: '05822379b2886d4ccb77a0a4d949360995765ac9'
4
+ data.tar.gz: e91766d115a3a52bc92c7f8a11caaa8ae07b0025
5
+ SHA512:
6
+ metadata.gz: 8d056bd501e220739f73021bb2054fc0b127a3f33b96eee7b570d0e4c8f5b133d8fc5522c58747517cbe84b82097b2d408285ad256174ed5fac8460db7644c1f
7
+ data.tar.gz: f5012b9b5af4c812b52376635e64581c7ae7f403cfd4214b68769abdafc4d49ef2988b4e4874bcdbe57603b363b2f6c871ffbe4cd060c54d059711868deee555
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.7
5
+ - 2.3.4
6
+ - 2.4.1
7
+ env:
8
+ matrix:
9
+ - RAILS_VERSION='~> 4.2.0'
10
+ - RAILS_VERSION='~> 5.0.0'
11
+ - RAILS_VERSION='~> 5.1.x'
12
+ before_install: gem install bundler -v 1.14.6
@@ -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 john.hawthorn@gmail.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,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ rails_version = ENV['RAILS_VERSION']
4
+ gem 'activerecord', rails_version
5
+
6
+ # Specify your gem's dependencies in discard.gemspec
7
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 John Hawthorn
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,198 @@
1
+ # Discard [![Build Status](https://travis-ci.org/jhawthorn/discard.svg?branch=master)](https://travis-ci.org/jhawthorn/discard)
2
+
3
+ Soft deletes for ActiveRecord done right.
4
+
5
+ <img src="http://i.hawth.ca/u/ron-swanson-computer-trash.gif" width="800" />
6
+
7
+ ## What does this do?
8
+
9
+ A simple ActiveRecord mixin to add conventions for flagging records as discarded.
10
+
11
+ ## Why should I use this?
12
+
13
+ I've worked with and have helped maintain
14
+ [paranoia](https://github.com/rubysherpas/paranoia) for a while. I'm convinced
15
+ it does the wrong thing for most cases.
16
+
17
+ Paranoia and
18
+ [acts_as_paranoid](https://github.com/ActsAsParanoid/acts_as_paranoid) both
19
+ attempt to emulate deletes by setting a column and adding a default scope on the
20
+ model. This requires some ActiveRecord hackery, and leads to some surprising
21
+ and awkward behaviour.
22
+
23
+ * A default scope is added to hide soft-deleted records, which necessitates
24
+ adding `.with_deleted` to associations or anywhere soft-deleted records
25
+ should be found. :disappointed:
26
+ * Adding `belongs_to :child, -> { with_deleted }` helps, but doesn't work for
27
+ joins and eager-loading.
28
+ * `delete` is overridden (`really_delete` will actually delete the record) :unamused:
29
+ * `destroy` is overridden (`really_destroy` will actually delete the record) :pensive:
30
+ * `dependent: :destroy` associations are deleted when performing soft-destroys :scream:
31
+ * requiring any dependent records to also be `acts_as_paranoid` to avoid losing data. :grimacing:
32
+
33
+ There are some use cases where these behaviours make sense: if you really did
34
+ want to _almost_ delete the record. More often developers are just looking to
35
+ hide some records, or mark them as inactive.
36
+
37
+ Discard takes a different approach. It doesn't override any ActiveRecord
38
+ methods and instead simply provides convenience methods and scopes for
39
+ discarding (hiding), restoring, and querying records.
40
+
41
+ ## Installation
42
+
43
+ Add this line to your application's Gemfile:
44
+
45
+ ```ruby
46
+ gem 'discard'
47
+ ```
48
+
49
+ And then execute:
50
+
51
+ $ bundle
52
+
53
+ ## Usage
54
+
55
+ **Declare a record as discardable**
56
+
57
+ Declare the record as being discardable
58
+
59
+ ``` ruby
60
+ class Post < ActiveRecord::Base
61
+ include Discard::Model
62
+ end
63
+ ```
64
+
65
+
66
+ ``` ruby
67
+ class AddDiscardToPosts < ActiveRecord::Migration[5.0]
68
+ def up
69
+ add_column :posts, :discarded_at, :datetime
70
+ end
71
+ end
72
+ ```
73
+
74
+
75
+ **Discard a record**
76
+ ```
77
+ Post.all # => [#<Post id: 1, ...>]
78
+ Post.kept # => [#<Post id: 1, ...>]
79
+ Post.discarded # => []
80
+
81
+ @post = Post.first # => #<Post id: 1, ...>
82
+ @post.discard!
83
+ @post.discarded? # => true
84
+ @post.discarded_at # => 2017-04-18 18:49:49 -0700
85
+
86
+ Post.all # => [#<Post id: 1, ...>]
87
+ Post.kept # => []
88
+ Post.discarded # => [#<Post id: 1, ...>]
89
+ ```
90
+
91
+ **From a controller**
92
+
93
+ Controller actions need a small modification to discard records instead of deleting them. Just replace `destroy` with `discard`.
94
+
95
+ ``` ruby
96
+ def destroy
97
+ @post.discard
98
+ redirect_to users_url, notice: "Post removed"
99
+ end
100
+ ```
101
+
102
+ **Working with associations**
103
+
104
+ Under paranoia, soft deleting a record will destroy any `dependent: :destroy`
105
+ associations. Probably not what you want! This leads to all dependent records
106
+ also needing to be `acts_as_paranoid`, which makes restoring awkward: paranoia
107
+ handles this by restoring any records which have their deleted_at set to a
108
+ similar timestamp. Also, it doesn't always make sense to mark these records as
109
+ deleted, it depends on the application.
110
+
111
+ A better approach is to simply mark the one record as discarded, and use SQL
112
+ joins to restrict finding these if that's desired.
113
+
114
+ For example, in a blog comment system, with `Post`s and `Comment`s, you might
115
+ want to discard the records independently. A user's comment history could
116
+ include comments on deleted posts.
117
+
118
+ ``` ruby
119
+ Post.kept # SELECT * FROM posts WHERE discarded_at IS NULL
120
+ Comment.kept # SELECT * FROM comments WHERE discarded_at IS NULL
121
+ ```
122
+
123
+ Or you could decide that comments are dependent on their posts not being
124
+ discarded. Just override the `kept` scope on the Comment model.
125
+
126
+ ``` ruby
127
+ class Comment < ActiveRecord::Base
128
+ has_many :posts
129
+
130
+ include Discard::Model
131
+ scope :kept, -> { undiscarded.joins(:posts).merge(Post.kept) }
132
+ end
133
+
134
+ Comment.kept
135
+ # SELECT * FROM comments
136
+ # INNER JOIN posts ON comments.post_id = posts.id
137
+ # WHERE
138
+ # comments.discarded_at IS NULL AND
139
+ # posts.discarded_at IS NULL
140
+ ```
141
+
142
+ SQL databases are very good at this, and performance should not be an issue.
143
+
144
+ In both of these cases restoring either of these records will do right thing!
145
+
146
+
147
+ **Default scope**
148
+
149
+ It's usually undesirable to add a default scope. It will take more effort to
150
+ work around and will cause more headaches. If you know you need a default scope, it's easy to add yourself ❤.
151
+
152
+ ``` ruby
153
+ class Post < ActiveRecord::Base
154
+ include Discard::Model
155
+ default_scope -> { kept }
156
+ end
157
+
158
+ Post.all # Only kept posts
159
+ Post.with_discarded # All Posts
160
+ Post.with_discarded.discarded # Only discarded posts
161
+ ```
162
+
163
+ **Custom column**
164
+
165
+ If you're migrating from paranoia, you might want to continue using the same
166
+ column.
167
+
168
+ ``` ruby
169
+ class Post < ActiveRecord::Base
170
+ include Discard::Model
171
+ self.discard_column = :deleted_at
172
+ end
173
+ ```
174
+
175
+ ## Non-features
176
+
177
+ * Restoring records (this will probably be added)
178
+ * Discarding dependent records (this will likely be added)
179
+ * Callbacks (this will probably be added)
180
+ * Special handling of AR counter cache columns - The counter cache counts the total number of records, both kept and discarded.
181
+
182
+ ## Development
183
+
184
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
185
+
186
+ ## Contributing
187
+
188
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jhawthorn/discard.
189
+
190
+ ## License
191
+
192
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
193
+
194
+ ## Acknowledgments
195
+
196
+ * [Ben Morgan](https://github.com/BenMorganIO) who has done a great job maintaining paranoia
197
+ * [Ryan Bigg](http://github.com/radar), the original author of paranoia (and many things), as a simpler replacement of acts_as_paranoid
198
+ * All paranoia users and contributors
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
9
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "discard"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'discard/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "discard"
8
+ spec.version = Discard::VERSION
9
+ spec.authors = ["John Hawthorn"]
10
+ spec.email = ["john.hawthorn@gmail.com"]
11
+
12
+ spec.summary = %q{ActiveRecord soft-deletes done right}
13
+ spec.description = %q{Allows marking ActiveRecord objects as discarded, and provides scopes for filtering.}
14
+ spec.homepage = "https://github.com/jhawthorn/discard"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "activerecord", '>= 4.2', '< 6'
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.5.0"
28
+ spec.add_development_dependency "database_cleaner", "~> 1.5"
29
+ spec.add_development_dependency "with_model", "~> 2.0"
30
+ spec.add_development_dependency "sqlite3"
31
+ end
@@ -0,0 +1,4 @@
1
+ require "active_record"
2
+
3
+ require "discard/version"
4
+ require "discard/model"
@@ -0,0 +1,23 @@
1
+ module Discard
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :discard_column
7
+ self.discard_column = :discarded_at
8
+
9
+ scope :kept, ->{ undiscarded }
10
+ scope :undiscarded, ->{ where(discard_column => nil) }
11
+ scope :discarded, ->{ where.not(discard_column => nil) }
12
+ scope :with_discarded, ->{ unscope(where: discard_column) }
13
+ end
14
+
15
+ def discarded?
16
+ !!self[self.class.discard_column]
17
+ end
18
+
19
+ def discard
20
+ touch(self.class.discard_column) unless discarded?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Discard
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: discard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Hawthorn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-28 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: '4.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.14'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.14'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 3.5.0
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 3.5.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: database_cleaner
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.5'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.5'
89
+ - !ruby/object:Gem::Dependency
90
+ name: with_model
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '2.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '2.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: sqlite3
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: Allows marking ActiveRecord objects as discarded, and provides scopes
118
+ for filtering.
119
+ email:
120
+ - john.hawthorn@gmail.com
121
+ executables: []
122
+ extensions: []
123
+ extra_rdoc_files: []
124
+ files:
125
+ - ".gitignore"
126
+ - ".rspec"
127
+ - ".travis.yml"
128
+ - CODE_OF_CONDUCT.md
129
+ - Gemfile
130
+ - LICENSE.txt
131
+ - README.md
132
+ - Rakefile
133
+ - bin/console
134
+ - bin/setup
135
+ - discard.gemspec
136
+ - lib/discard.rb
137
+ - lib/discard/model.rb
138
+ - lib/discard/version.rb
139
+ homepage: https://github.com/jhawthorn/discard
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.6.11
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: ActiveRecord soft-deletes done right
163
+ test_files: []