console_think_twice 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a6b253a3a4ae99d15459e9c03389d8939e9a9851372c2b7eadab198564ec7f1
4
- data.tar.gz: '09efc4fd77651863aff72c3f578dc27ce30074cd96c190bd6a8da882c7b42e58'
3
+ metadata.gz: 236302689b1695ddb978506ea4a90c043c8cf03edb179b35ff053655c275297a
4
+ data.tar.gz: 84aab44ed6323c6fbbaa6cc1b2ef6872320335cd4cb8dbf6e86efb03f509a879
5
5
  SHA512:
6
- metadata.gz: 3071ebb09d7533f2c733b487b94cfba26d895bf39ca9efd5a76718afdb703051f32c6fd34e1b4fdeccc7d171e7401a261315d2919ffde8542eb50a0c2ac9e5be
7
- data.tar.gz: 9c2f40efb4837679488438af2f4d46169d75cca4e3410802a349d871f59effdcda4484792c3596f2d776031d2c9ff1a13c03637d2be2312c350bc20dcbc5f49e
6
+ metadata.gz: 979ca9fbfa8533b0945653fc1207de763b4a80ddaac372acf37201d2ed0f4074c3686040d670d6b67ac3873ee408bf954935772e9b17e389e6a832a8cdd46817
7
+ data.tar.gz: 0e8e48c246db11faf5ab091e623789ef4b06253ddcf8e77bc8555df0e96310cb3e0835a79f849f8a565f73a65950617546e46f8ee91bec37e1ca29ad0e458580
data/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.3.0] - 2026-09-01
6
+
7
+ ### Added
8
+
9
+ - `config.ignored_classes` leaves the classes you name unguarded, for tables that are
10
+ cleared as a matter of routine — `SolidCache::Entry`, say. Takes names or classes,
11
+ covers subclasses, and compares names rather than resolving them, so a class that is not
12
+ loaded, or not in this app at all, can be listed.
13
+
5
14
  ## [0.2.0] - 2026-08-26
6
15
 
7
16
  ### Fixed
@@ -49,6 +58,7 @@ Initial release.
49
58
  - Non-interactive input refuses rather than assuming yes.
50
59
  - Configurable via `ConsoleThinkTwice.configure`, or turned off with `CONSOLE_THINK_TWICE=0`.
51
60
 
52
- [Unreleased]: https://github.com/agomezcampero/console-think-twice/compare/v0.2.0...HEAD
61
+ [Unreleased]: https://github.com/agomezcampero/console-think-twice/compare/v0.3.0...HEAD
62
+ [0.3.0]: https://github.com/agomezcampero/console-think-twice/compare/v0.2.0...v0.3.0
53
63
  [0.2.0]: https://github.com/agomezcampero/console-think-twice/compare/v0.1.0...v0.2.0
54
64
  [0.1.0]: https://github.com/agomezcampero/console-think-twice/releases/tag/v0.1.0
data/README.md CHANGED
@@ -76,6 +76,8 @@ are suppressed for the duration of a confirmed call, so you are asked exactly on
76
76
  `collection.delete(record)` — which unlinks the record rather than destroying it, unless the
77
77
  association says `dependent: :destroy`.
78
78
 
79
+ Classes listed in `config.ignored_classes` are not guarded either — see below.
80
+
79
81
  Active Record also destroys records itself, as one step of a call that is about something
80
82
  else: a record marked `_destroy` by `accepts_nested_attributes_for`, or the old record a
81
83
  `has_one` assignment discards. Those do not prompt. They are already covered by whatever you
@@ -97,6 +99,7 @@ ConsoleThinkTwice.configure do |config|
97
99
  config.enabled = Rails.env.production? # default: true, unless CONSOLE_THINK_TWICE is 0/false/no/off
98
100
  config.label = "production (europe)" # shown in the prompt; defaults to Rails.env,
99
101
  # set nil or false to leave it out
102
+ config.ignored_classes = %w[SolidCache::Entry] # left unguarded; default: none
100
103
  config.affirmative_answers = %w[y yes si] # default: %w[y yes]
101
104
  config.interactive = true # default: auto-detected from config.input.tty?
102
105
  config.input = $stdin
@@ -104,6 +107,20 @@ ConsoleThinkTwice.configure do |config|
104
107
  end
105
108
  ```
106
109
 
110
+ ### Classes to leave alone
111
+
112
+ Some tables are cleared as a matter of routine — job records, cache entries, whatever your
113
+ app treats as scratch. Being asked about those is noise, and noise teaches you to answer `y`
114
+ without reading. List them and the guard stays out of the way:
115
+
116
+ ```ruby
117
+ config.ignored_classes = %w[SolidCache::Entry SolidQueue::Job]
118
+ ```
119
+
120
+ Classes work as well as names, and a subclass of a listed class is ignored too. Names are
121
+ compared rather than resolved, so listing a class that this app does not have, or has not
122
+ autoloaded yet, is fine.
123
+
107
124
  Set `CONSOLE_THINK_TWICE=0` in the environment to turn the guard off without touching
108
125
  code. `ConsoleThinkTwice.disable!` turns it off for the rest of the current session and
109
126
  `ConsoleThinkTwice.enable!` turns it back on; setting `config.enabled` takes effect straight
@@ -21,6 +21,27 @@ module ConsoleThinkTwice
21
21
  @output || $stdout
22
22
  end
23
23
 
24
+ # Classes the guard leaves alone. Bookkeeping tables that are cleared as a matter of
25
+ # routine — `SolidCache::Entry`, say — are noise to be asked about, and being asked
26
+ # about them teaches you to answer y without reading. Takes names or classes, and a
27
+ # subclass of a listed class is ignored too.
28
+ #
29
+ # Names are compared rather than resolved, so a class can be listed before it is
30
+ # autoloaded, or listed in an initializer shared by apps that do not all have it.
31
+ def ignored_classes=(classes)
32
+ @ignored_classes = Array(classes).map(&:to_s)
33
+ end
34
+
35
+ def ignored_classes
36
+ @ignored_classes ||= []
37
+ end
38
+
39
+ def ignores?(model)
40
+ return false if ignored_classes.empty?
41
+
42
+ inheritance_chain(model).any? { |name| ignored_classes.include?(name) }
43
+ end
44
+
24
45
  def enabled?
25
46
  return @enabled unless @enabled.nil?
26
47
 
@@ -43,5 +64,15 @@ module ConsoleThinkTwice
43
64
 
44
65
  ::Rails.env.to_s if defined?(::Rails) && ::Rails.respond_to?(:env)
45
66
  end
67
+
68
+ private
69
+
70
+ # The model's own name and the names of the models it inherits from, so that ignoring a
71
+ # class ignores its subclasses. Stops at Active Record itself, which every model shares.
72
+ def inheritance_chain(model)
73
+ Enumerator.produce(model, &:superclass)
74
+ .take_while { |klass| klass && klass != ::ActiveRecord::Base }
75
+ .map(&:name)
76
+ end
46
77
  end
47
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ConsoleThinkTwice
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -82,7 +82,7 @@ module ConsoleThinkTwice
82
82
  # saves a COUNT and is the only way to size a call like `books.destroy(a, b)` that names
83
83
  # its records rather than describing them.
84
84
  def guard(target, action:, force:, count: nil)
85
- return yield if !active? || suppressed?
85
+ return yield if !active? || suppressed? || ignored?(target)
86
86
 
87
87
  # Active Record's own work is confirmed by whatever the user typed to set it off, so it
88
88
  # runs suppressed rather than merely unguarded: `destroy!` re-enters as `destroy`, and
@@ -101,6 +101,10 @@ module ConsoleThinkTwice
101
101
 
102
102
  private
103
103
 
104
+ def ignored?(target)
105
+ configuration.ignores?(model_of(target))
106
+ end
107
+
104
108
  # True when Active Record itself made this call as one step of an operation already under
105
109
  # way: a record being removed by `accepts_nested_attributes_for`, a `has_one` being
106
110
  # replaced, a collection destroying the records it was handed. Those run inside the
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console_think_twice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Agustin Gomez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-08-27 00:00:00.000000000 Z
11
+ date: 2026-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord