discard 1.3.0 → 2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 98ef1bd12f3edc82e8e50b429d39989473cf7b917225ce25068a60e09ef7bb06
4
- data.tar.gz: 7762b63af8cd7aeeb848f9778242a02a02be7069586943542916ea3ac12e6d51
3
+ metadata.gz: c6e47444b3cac75169be133384030483a8ef0a7821a0ee91cce049f63887ebc3
4
+ data.tar.gz: 28ce8969c023827f43f63a84725291569bde03a0b8e25a8f453a1eed6d8b9dfa
5
5
  SHA512:
6
- metadata.gz: a43ba463a29c4cd1e1b8c3a79a37b8d140925ef9fe2ee1d23b1aa347c86e746faa390a3a11c60818cf327fb81c0cacd8561a9005a3ddeb9ced016b64258c32ce
7
- data.tar.gz: 1a3516836d8a2f21ef1e0385d6b126bec5f5ca380071f473a80ac8d8bd5dc9cd275954fff9052b9964f5d0763cbbd0b2ba4e8da9317e042dbbdd5cb42836cf56
6
+ metadata.gz: 2562f3ccf49e8622634e375d2cdcb3dd88099e22c311ca0410a979e183d66650b1cc57532ab0ed7f432d3b48facb5dc77892c4403d9dba9ebad26f88222559ed
7
+ data.tar.gz: c3bb81d6967865a8b66552d78e452016fcce882eb327bb0bb9c08a36db42f09becb88e4f6c98194e651fb579daef28d2ae426b3b4feb54deb770df78437151c4
@@ -13,27 +13,42 @@ jobs:
13
13
  runs-on: ubuntu-latest
14
14
  name: Test on Rails ${{ matrix.rails_version }} and Ruby ${{ matrix.ruby_version }}
15
15
  strategy:
16
+ fail-fast: false
16
17
  matrix:
17
18
  include:
19
+ - rails_version: ~> 8.1.0
20
+ ruby_version: '4.0'
21
+ sqlite_version: ~> 2.0
22
+ - rails_version: ~> 8.1.0
23
+ ruby_version: '3.4'
24
+ sqlite_version: ~> 2.0
25
+ - rails_version: ~> 8.0.0
26
+ ruby_version: '3.4'
27
+ sqlite_version: ~> 2.0
28
+ - rails_version: ~> 8.0.0
29
+ ruby_version: '3.3'
30
+ sqlite_version: ~> 2.0
31
+ - rails_version: ~> 7.2.0
32
+ ruby_version: '3.3'
33
+ sqlite_version: ~> 2.0
34
+ - rails_version: ~> 7.2.0
35
+ ruby_version: '3.2'
36
+ sqlite_version: ~> 2.0
37
+ - rails_version: ~> 7.1.0
38
+ ruby_version: '3.3'
39
+ sqlite_version: ~> 1.0
18
40
  - rails_version: ~> 7.0.0
19
- ruby_version: 3.2
20
- - rails_version: ~> 7.0.0
21
- ruby_version: 3.1
22
- - rails_version: ~> 6.1.0
23
- ruby_version: '3.0'
24
- - rails_version: ~> 6.0.0
25
- ruby_version: 2.7
26
- - rails_version: ~> 5.2.0
27
- ruby_version: 2.6
28
- - rails_version: ~> 5.1.0
29
- ruby_version: 2.5
41
+ ruby_version: '3.2'
42
+ sqlite_version: ~> 1.0
30
43
  env:
31
44
  RAILS_VERSION: ${{ matrix.rails_version }}
45
+ SQLITE_VERSION: ${{ matrix.sqlite_version }}
32
46
  steps:
33
- - uses: actions/checkout@v3
47
+ - uses: actions/checkout@v6
34
48
  - uses: ruby/setup-ruby@v1
35
49
  with:
36
50
  ruby-version: ${{ matrix.ruby_version }}
37
- bundler-cache: true
51
+ - name: Bundle install
52
+ run: bundle install
38
53
  - name: Test
39
54
  run: bundle exec rake
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 4.0.1
data/CHANGELOG.md CHANGED
@@ -1,4 +1,15 @@
1
- ### Unreleased
1
+ ### Version 2.0.0
2
+ Release date: 2026-05-27
3
+
4
+ * Require ActiveRecord >= 7.0; drop support for Rails 6.x and earlier
5
+ * Wrap `#discard` / `#undiscard` in a transaction so callback exceptions roll back the DB write (#84, #77)
6
+
7
+ ### Version 1.4.0
8
+ Release date: 2024-11-05
9
+
10
+ * Support Rails 8.0 and 8.1 (#110, #111)
11
+
12
+ * More descriptive error messages (#108)
2
13
 
3
14
  ### Version 1.3.0
4
15
  Release date: 2023-08-17
data/Gemfile CHANGED
@@ -3,6 +3,10 @@ source 'https://rubygems.org'
3
3
  rails_version = ENV['RAILS_VERSION']
4
4
  gem 'activerecord', rails_version
5
5
 
6
+ if rails_version == '~> 6.1.0'
7
+ gem 'concurrent-ruby', '1.3.4'
8
+ end
9
+
6
10
  if sqlite_version = ENV['SQLITE_VERSION']
7
11
  gem 'sqlite3', sqlite_version
8
12
  end
data/README.md CHANGED
@@ -13,7 +13,7 @@ A simple ActiveRecord mixin to add conventions for flagging records as discarded
13
13
  Add this line to your application's Gemfile:
14
14
 
15
15
  ```ruby
16
- gem 'discard', '~> 1.2'
16
+ gem 'discard', '~> 2.0'
17
17
  ```
18
18
 
19
19
  And then execute:
@@ -33,6 +33,7 @@ end
33
33
  ```
34
34
 
35
35
  You can either generate a migration using:
36
+
36
37
  ```
37
38
  rails generate migration add_discarded_at_to_posts discarded_at:datetime:index
38
39
  ```
@@ -47,7 +48,6 @@ class AddDiscardToPosts < ActiveRecord::Migration[5.0]
47
48
  end
48
49
  ```
49
50
 
50
-
51
51
  #### Discard a record
52
52
 
53
53
  ```ruby
@@ -200,10 +200,15 @@ class Post < ActiveRecord::Base
200
200
  end
201
201
  ```
202
202
 
203
- *Warning:* Please note that callbacks for save and update are run when discarding/undiscarding a record
203
+ Some important things to note:
204
+
205
+ - Callbacks for save and update are run when discarding/undiscarding a record.
206
+ - Validations are not run during `#discard` or `#undiscard`. The column is updated via `update_attribute`, which skips validations.
207
+ - The discard column is also flipped *between* the `before_` and `after_` callbacks, so `before_discard` sees `discarded?` as `false` while `after_discard` sees it as `true` (and symmetrically for undiscard). Callbacks gated on `if: :discarded?` will fire accordingly, which is useful when a callback should only run on one side of the transition.
204
208
 
205
209
 
206
210
  #### Performance tuning
211
+
207
212
  `discard_all` and `undiscard_all` is intended to behave like `destroy_all` which has callbacks, validations, and does one query per record. If performance is a big concern, you may consider replacing it with:
208
213
 
209
214
  `scope.update_all(discarded_at: Time.current)`
@@ -269,7 +274,7 @@ You can find more information about the history and purpose of Discard in [this
269
274
 
270
275
  ## Development
271
276
 
272
- 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.
277
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
273
278
 
274
279
  ## Contributing
275
280
 
data/discard.gemspec CHANGED
@@ -22,10 +22,10 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ["lib"]
24
24
 
25
- spec.add_dependency "activerecord", ">= 4.2", "< 8"
25
+ spec.add_dependency "activerecord", ">= 7.0", "< 9.0"
26
26
  spec.add_development_dependency "bundler"
27
27
  spec.add_development_dependency "rake", ">= 10.0"
28
- spec.add_development_dependency "rspec", "~> 3.5.0"
28
+ spec.add_development_dependency "rspec", "~> 3.13"
29
29
  spec.add_development_dependency "database_cleaner", "~> 1.5"
30
30
  spec.add_development_dependency "with_model", "~> 2.0"
31
31
  spec.add_development_dependency "sqlite3"
data/lib/discard/model.rb CHANGED
@@ -117,8 +117,11 @@ module Discard
117
117
  # @return [Boolean] true if successful, otherwise false
118
118
  def discard
119
119
  return false if discarded?
120
- run_callbacks(:discard) do
121
- update_attribute(self.class.discard_column, Time.current)
120
+
121
+ with_transaction_returning_status do
122
+ run_callbacks(:discard) do
123
+ update_attribute(self.class.discard_column, Time.current)
124
+ end
122
125
  end
123
126
  end
124
127
 
@@ -139,12 +142,15 @@ module Discard
139
142
  # @return [Boolean] true if successful, otherwise false
140
143
  def undiscard
141
144
  return false unless discarded?
142
- run_callbacks(:undiscard) do
143
- update_attribute(self.class.discard_column, nil)
145
+
146
+ with_transaction_returning_status do
147
+ run_callbacks(:undiscard) do
148
+ update_attribute(self.class.discard_column, nil)
149
+ end
144
150
  end
145
151
  end
146
152
 
147
- # Discard the record in the database
153
+ # Undiscard the record in the database
148
154
  #
149
155
  # There's a series of callbacks associated with #undiscard!. If the
150
156
  # <tt>before_undiscard</tt> callback throws +:abort+ the action is cancelled
@@ -159,11 +165,23 @@ module Discard
159
165
  private
160
166
 
161
167
  def _raise_record_not_discarded
162
- raise ::Discard::RecordNotDiscarded.new("Failed to discard the record", self)
168
+ raise ::Discard::RecordNotDiscarded.new(discarded_fail_message, self)
163
169
  end
164
170
 
165
171
  def _raise_record_not_undiscarded
166
- raise ::Discard::RecordNotUndiscarded.new("Failed to undiscard the record", self)
172
+ raise ::Discard::RecordNotUndiscarded.new(undiscarded_fail_message, self)
173
+ end
174
+
175
+ def discarded_fail_message
176
+ return "A discarded record cannot be discarded" if discarded?
177
+
178
+ "Failed to discard the record"
179
+ end
180
+
181
+ def undiscarded_fail_message
182
+ return "An undiscarded record cannot be undiscarded" if undiscarded?
183
+
184
+ "Failed to undiscard the record"
167
185
  end
168
186
  end
169
187
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Discard
4
4
  # Discard version
5
- VERSION = "1.3.0".freeze
5
+ VERSION = "2.0.0".freeze
6
6
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: discard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Hawthorn
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2023-08-17 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activerecord
@@ -16,20 +15,20 @@ dependencies:
16
15
  requirements:
17
16
  - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '4.2'
18
+ version: '7.0'
20
19
  - - "<"
21
20
  - !ruby/object:Gem::Version
22
- version: '8'
21
+ version: '9.0'
23
22
  type: :runtime
24
23
  prerelease: false
25
24
  version_requirements: !ruby/object:Gem::Requirement
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- version: '4.2'
28
+ version: '7.0'
30
29
  - - "<"
31
30
  - !ruby/object:Gem::Version
32
- version: '8'
31
+ version: '9.0'
33
32
  - !ruby/object:Gem::Dependency
34
33
  name: bundler
35
34
  requirement: !ruby/object:Gem::Requirement
@@ -64,14 +63,14 @@ dependencies:
64
63
  requirements:
65
64
  - - "~>"
66
65
  - !ruby/object:Gem::Version
67
- version: 3.5.0
66
+ version: '3.13'
68
67
  type: :development
69
68
  prerelease: false
70
69
  version_requirements: !ruby/object:Gem::Requirement
71
70
  requirements:
72
71
  - - "~>"
73
72
  - !ruby/object:Gem::Version
74
- version: 3.5.0
73
+ version: '3.13'
75
74
  - !ruby/object:Gem::Dependency
76
75
  name: database_cleaner
77
76
  requirement: !ruby/object:Gem::Requirement
@@ -128,7 +127,7 @@ files:
128
127
  - ".github/workflows/test.yml"
129
128
  - ".gitignore"
130
129
  - ".rspec"
131
- - ".travis.yml"
130
+ - ".ruby-version"
132
131
  - ".yardopts"
133
132
  - CHANGELOG.md
134
133
  - CODE_OF_CONDUCT.md
@@ -147,7 +146,6 @@ homepage: https://github.com/jhawthorn/discard
147
146
  licenses:
148
147
  - MIT
149
148
  metadata: {}
150
- post_install_message:
151
149
  rdoc_options: []
152
150
  require_paths:
153
151
  - lib
@@ -162,8 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
160
  - !ruby/object:Gem::Version
163
161
  version: '0'
164
162
  requirements: []
165
- rubygems_version: 3.4.10
166
- signing_key:
163
+ rubygems_version: 3.6.9
167
164
  specification_version: 4
168
165
  summary: ActiveRecord soft-deletes done right
169
166
  test_files: []
data/.travis.yml DELETED
@@ -1,28 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
- rvm:
4
- - 2.2
5
- - 2.3
6
- - 2.4
7
- - 2.5
8
- - 2.6
9
- - 2.7
10
- env:
11
- matrix:
12
- - RAILS_VERSION='~> 4.2.0' SQLITE_VERSION='~> 1.3.6'
13
- - RAILS_VERSION='~> 5.0.0' SQLITE_VERSION='~> 1.3.6'
14
- - RAILS_VERSION='~> 5.1.0'
15
- - RAILS_VERSION='~> 5.2.0'
16
- - RAILS_VERSION='~> 6.0.0'
17
-
18
- matrix:
19
- exclude:
20
- - rvm: 2.2
21
- env: RAILS_VERSION='~> 6.0.0'
22
- - rvm: 2.3
23
- env: RAILS_VERSION='~> 6.0.0'
24
- - rvm: 2.4
25
- env: RAILS_VERSION='~> 6.0.0'
26
- # Rails 4.2 uses BigDecimal.new, which Ruby 2.7 removed
27
- - rvm: 2.7
28
- env: RAILS_VERSION='~> 4.2.0' SQLITE_VERSION='~> 1.3.6'