rails_toggleable 1.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0a484b1e8d762530af47c8df19f1de7a56e92a72de2ca6d546d8966b4f11e510
4
+ data.tar.gz: 69c29c03fa7f5ea4898fcf5689e24262a8cb3a6240bbd45f81f85f1f96078a4a
5
+ SHA512:
6
+ metadata.gz: 888a4275893725b3ae8b1012555a13a9940f36f4b72400318309c9e13666a43628ef50c6ecad84a587c199a747f2458e183f2f3495f40b368c2227c6925e8ac9
7
+ data.tar.gz: b3e40f8d4ab42c7d3add1f6065f6d82efbf35387e31da89f479fb8ad69661cc4e14927537249b0168ae1d68df329918204133730d9db799429917200d7d77b79
@@ -0,0 +1,12 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: weekly
7
+ open-pull-requests-limit: 10
8
+ - package-ecosystem: github-actions
9
+ directory: "/"
10
+ schedule:
11
+ interval: weekly
12
+ open-pull-requests-limit: 10
@@ -0,0 +1,69 @@
1
+ name: CI
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches: [ main ]
7
+
8
+ jobs:
9
+ lint:
10
+ runs-on: ubuntu-latest
11
+ env:
12
+ RUBY_VERSION: 3.3.6
13
+ RUBOCOP_CACHE_ROOT: tmp/rubocop
14
+ steps:
15
+ - name: Checkout code
16
+ uses: actions/checkout@v5
17
+
18
+ - name: Set up Ruby
19
+ uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ env.RUBY_VERSION }}
22
+ bundler-cache: true
23
+
24
+ - name: Prepare RuboCop cache
25
+ uses: actions/cache@v4
26
+ env:
27
+ DEPENDENCIES_HASH: ${{ hashFiles('**/.rubocop.yml', '**/.rubocop_todo.yml', 'Gemfile.lock') }}
28
+ with:
29
+ path: ${{ env.RUBOCOP_CACHE_ROOT }}
30
+ key: rubocop-${{ runner.os }}-${{ env.RUBY_VERSION }}-${{ env.DEPENDENCIES_HASH }}-${{ github.ref_name == github.event.repository.default_branch && github.run_id || 'default' }}
31
+ restore-keys: |
32
+ rubocop-${{ runner.os }}-${{ env.RUBY_VERSION }}-${{ env.DEPENDENCIES_HASH }}-
33
+
34
+ - name: Lint code for consistent style
35
+ run: bin/rubocop -f github
36
+
37
+ test:
38
+ runs-on: ubuntu-latest
39
+
40
+ # services:
41
+ # redis:
42
+ # image: valkey/valkey:8
43
+ # ports:
44
+ # - 6379:6379
45
+ # options: --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5
46
+ steps:
47
+ - name: Checkout code
48
+ uses: actions/checkout@v5
49
+
50
+ - name: Set up Ruby
51
+ uses: ruby/setup-ruby@v1
52
+ with:
53
+ ruby-version: 3.3.6
54
+ bundler-cache: true
55
+
56
+ - name: Run tests
57
+ env:
58
+ RAILS_ENV: test
59
+ # RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
60
+ # REDIS_URL: redis://localhost:6379/0
61
+ run: bin/rails db:test:prepare test
62
+
63
+ - name: Keep screenshots from failed system tests
64
+ uses: actions/upload-artifact@v4
65
+ if: failure()
66
+ with:
67
+ name: screenshots
68
+ path: ${{ github.workspace }}/tmp/screenshots
69
+ if-no-files-found: ignore
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /doc/
3
+ /log/*.log
4
+ /pkg/
5
+ /tmp/
6
+ /test/dummy/db/*.sqlite3
7
+ /test/dummy/db/*.sqlite3-*
8
+ /test/dummy/log/*.log
9
+ /test/dummy/storage/
10
+ /test/dummy/tmp/
11
+
12
+ /.idea
13
+ /*.gem
data/.release-it.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "git": {
3
+ "requireCleanWorkingDir": false
4
+ },
5
+ "hooks": {
6
+ "after:init": ["t2k", "npm run clean"],
7
+ "after:bump": [
8
+ "syncv -p lib/rails_toggleable/version.rb",
9
+ "npm run build",
10
+ "npm run pubpush"
11
+ ]
12
+ },
13
+ "github": {
14
+ "release": false,
15
+ "proxy": "http://127.0.0.1:9090",
16
+ "releaseName": "Release ${version}",
17
+ "skipChecks": true,
18
+ "releaseNotes": "git log --no-merges --pretty=format:\"* %s %h\" ${latestTag}...main"
19
+ },
20
+ "npm": {
21
+ "publish": false,
22
+ "skipChecks": true
23
+ }
24
+ }
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ # Omakase Ruby styling for Rails
2
+ inherit_gem: { rubocop-rails-omakase: rubocop.yml }
3
+
4
+ # Overwrite or add rules to create your own house style
5
+ #
6
+ # # Use `[a, [b, c]]` not `[ a, [ b, c ] ]`
7
+ # Layout/SpaceInsideArrayLiteralBrackets:
8
+ # Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails_toggleable.gemspec.
4
+ gemspec
5
+
6
+ gem "puma"
7
+
8
+ gem "sqlite3"
9
+
10
+ gem "propshaft"
11
+
12
+ # Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
13
+ gem "rubocop-rails-omakase", require: false
14
+
15
+ # Start debugger with binding.b [https://github.com/ruby/debug]
16
+ # gem "debug", ">= 1.0.0"
17
+
18
+ group :development do
19
+ gem 'awesome_print'
20
+ gem 'pry-rails'
21
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,266 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_toggleable (0.1.0)
5
+ rails (>= 6.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ action_text-trix (2.1.15)
11
+ railties
12
+ actioncable (8.1.1)
13
+ actionpack (= 8.1.1)
14
+ activesupport (= 8.1.1)
15
+ nio4r (~> 2.0)
16
+ websocket-driver (>= 0.6.1)
17
+ zeitwerk (~> 2.6)
18
+ actionmailbox (8.1.1)
19
+ actionpack (= 8.1.1)
20
+ activejob (= 8.1.1)
21
+ activerecord (= 8.1.1)
22
+ activestorage (= 8.1.1)
23
+ activesupport (= 8.1.1)
24
+ mail (>= 2.8.0)
25
+ actionmailer (8.1.1)
26
+ actionpack (= 8.1.1)
27
+ actionview (= 8.1.1)
28
+ activejob (= 8.1.1)
29
+ activesupport (= 8.1.1)
30
+ mail (>= 2.8.0)
31
+ rails-dom-testing (~> 2.2)
32
+ actionpack (8.1.1)
33
+ actionview (= 8.1.1)
34
+ activesupport (= 8.1.1)
35
+ nokogiri (>= 1.8.5)
36
+ rack (>= 2.2.4)
37
+ rack-session (>= 1.0.1)
38
+ rack-test (>= 0.6.3)
39
+ rails-dom-testing (~> 2.2)
40
+ rails-html-sanitizer (~> 1.6)
41
+ useragent (~> 0.16)
42
+ actiontext (8.1.1)
43
+ action_text-trix (~> 2.1.15)
44
+ actionpack (= 8.1.1)
45
+ activerecord (= 8.1.1)
46
+ activestorage (= 8.1.1)
47
+ activesupport (= 8.1.1)
48
+ globalid (>= 0.6.0)
49
+ nokogiri (>= 1.8.5)
50
+ actionview (8.1.1)
51
+ activesupport (= 8.1.1)
52
+ builder (~> 3.1)
53
+ erubi (~> 1.11)
54
+ rails-dom-testing (~> 2.2)
55
+ rails-html-sanitizer (~> 1.6)
56
+ activejob (8.1.1)
57
+ activesupport (= 8.1.1)
58
+ globalid (>= 0.3.6)
59
+ activemodel (8.1.1)
60
+ activesupport (= 8.1.1)
61
+ activerecord (8.1.1)
62
+ activemodel (= 8.1.1)
63
+ activesupport (= 8.1.1)
64
+ timeout (>= 0.4.0)
65
+ activestorage (8.1.1)
66
+ actionpack (= 8.1.1)
67
+ activejob (= 8.1.1)
68
+ activerecord (= 8.1.1)
69
+ activesupport (= 8.1.1)
70
+ marcel (~> 1.0)
71
+ activesupport (8.1.1)
72
+ base64
73
+ bigdecimal
74
+ concurrent-ruby (~> 1.0, >= 1.3.1)
75
+ connection_pool (>= 2.2.5)
76
+ drb
77
+ i18n (>= 1.6, < 2)
78
+ json
79
+ logger (>= 1.4.2)
80
+ minitest (>= 5.1)
81
+ securerandom (>= 0.3)
82
+ tzinfo (~> 2.0, >= 2.0.5)
83
+ uri (>= 0.13.1)
84
+ ast (2.4.3)
85
+ awesome_print (1.9.2)
86
+ base64 (0.3.0)
87
+ bigdecimal (3.3.1)
88
+ builder (3.3.0)
89
+ coderay (1.1.3)
90
+ concurrent-ruby (1.3.5)
91
+ connection_pool (2.5.4)
92
+ crass (1.0.6)
93
+ date (3.5.0)
94
+ drb (2.2.3)
95
+ erb (5.1.3)
96
+ erubi (1.13.1)
97
+ globalid (1.3.0)
98
+ activesupport (>= 6.1)
99
+ i18n (1.14.7)
100
+ concurrent-ruby (~> 1.0)
101
+ io-console (0.8.1)
102
+ irb (1.15.3)
103
+ pp (>= 0.6.0)
104
+ rdoc (>= 4.0.0)
105
+ reline (>= 0.4.2)
106
+ json (2.16.0)
107
+ language_server-protocol (3.17.0.5)
108
+ lint_roller (1.1.0)
109
+ logger (1.7.0)
110
+ loofah (2.24.1)
111
+ crass (~> 1.0.2)
112
+ nokogiri (>= 1.12.0)
113
+ mail (2.9.0)
114
+ logger
115
+ mini_mime (>= 0.1.1)
116
+ net-imap
117
+ net-pop
118
+ net-smtp
119
+ marcel (1.1.0)
120
+ method_source (1.1.0)
121
+ mini_mime (1.1.5)
122
+ minitest (5.26.0)
123
+ net-imap (0.5.12)
124
+ date
125
+ net-protocol
126
+ net-pop (0.1.2)
127
+ net-protocol
128
+ net-protocol (0.2.2)
129
+ timeout
130
+ net-smtp (0.5.1)
131
+ net-protocol
132
+ nio4r (2.7.5)
133
+ nokogiri (1.18.10-x86_64-darwin)
134
+ racc (~> 1.4)
135
+ parallel (1.27.0)
136
+ parser (3.3.10.0)
137
+ ast (~> 2.4.1)
138
+ racc
139
+ pp (0.6.3)
140
+ prettyprint
141
+ prettyprint (0.2.0)
142
+ prism (1.6.0)
143
+ propshaft (1.3.1)
144
+ actionpack (>= 7.0.0)
145
+ activesupport (>= 7.0.0)
146
+ rack
147
+ pry (0.15.2)
148
+ coderay (~> 1.1)
149
+ method_source (~> 1.0)
150
+ pry-rails (0.3.11)
151
+ pry (>= 0.13.0)
152
+ psych (5.2.6)
153
+ date
154
+ stringio
155
+ puma (7.1.0)
156
+ nio4r (~> 2.0)
157
+ racc (1.8.1)
158
+ rack (3.2.4)
159
+ rack-session (2.1.1)
160
+ base64 (>= 0.1.0)
161
+ rack (>= 3.0.0)
162
+ rack-test (2.2.0)
163
+ rack (>= 1.3)
164
+ rackup (2.2.1)
165
+ rack (>= 3)
166
+ rails (8.1.1)
167
+ actioncable (= 8.1.1)
168
+ actionmailbox (= 8.1.1)
169
+ actionmailer (= 8.1.1)
170
+ actionpack (= 8.1.1)
171
+ actiontext (= 8.1.1)
172
+ actionview (= 8.1.1)
173
+ activejob (= 8.1.1)
174
+ activemodel (= 8.1.1)
175
+ activerecord (= 8.1.1)
176
+ activestorage (= 8.1.1)
177
+ activesupport (= 8.1.1)
178
+ bundler (>= 1.15.0)
179
+ railties (= 8.1.1)
180
+ rails-dom-testing (2.3.0)
181
+ activesupport (>= 5.0.0)
182
+ minitest
183
+ nokogiri (>= 1.6)
184
+ rails-html-sanitizer (1.6.2)
185
+ loofah (~> 2.21)
186
+ nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
187
+ railties (8.1.1)
188
+ actionpack (= 8.1.1)
189
+ activesupport (= 8.1.1)
190
+ irb (~> 1.13)
191
+ rackup (>= 1.0.0)
192
+ rake (>= 12.2)
193
+ thor (~> 1.0, >= 1.2.2)
194
+ tsort (>= 0.2)
195
+ zeitwerk (~> 2.6)
196
+ rainbow (3.1.1)
197
+ rake (13.3.1)
198
+ rdoc (6.15.1)
199
+ erb
200
+ psych (>= 4.0.0)
201
+ tsort
202
+ regexp_parser (2.11.3)
203
+ reline (0.6.2)
204
+ io-console (~> 0.5)
205
+ rubocop (1.81.7)
206
+ json (~> 2.3)
207
+ language_server-protocol (~> 3.17.0.2)
208
+ lint_roller (~> 1.1.0)
209
+ parallel (~> 1.10)
210
+ parser (>= 3.3.0.2)
211
+ rainbow (>= 2.2.2, < 4.0)
212
+ regexp_parser (>= 2.9.3, < 3.0)
213
+ rubocop-ast (>= 1.47.1, < 2.0)
214
+ ruby-progressbar (~> 1.7)
215
+ unicode-display_width (>= 2.4.0, < 4.0)
216
+ rubocop-ast (1.47.1)
217
+ parser (>= 3.3.7.2)
218
+ prism (~> 1.4)
219
+ rubocop-performance (1.26.1)
220
+ lint_roller (~> 1.1)
221
+ rubocop (>= 1.75.0, < 2.0)
222
+ rubocop-ast (>= 1.47.1, < 2.0)
223
+ rubocop-rails (2.33.4)
224
+ activesupport (>= 4.2.0)
225
+ lint_roller (~> 1.1)
226
+ rack (>= 1.1)
227
+ rubocop (>= 1.75.0, < 2.0)
228
+ rubocop-ast (>= 1.44.0, < 2.0)
229
+ rubocop-rails-omakase (1.1.0)
230
+ rubocop (>= 1.72)
231
+ rubocop-performance (>= 1.24)
232
+ rubocop-rails (>= 2.30)
233
+ ruby-progressbar (1.13.0)
234
+ securerandom (0.4.1)
235
+ sqlite3 (2.8.0-x86_64-darwin)
236
+ stringio (3.1.7)
237
+ thor (1.4.0)
238
+ timeout (0.4.4)
239
+ tsort (0.2.0)
240
+ tzinfo (2.0.6)
241
+ concurrent-ruby (~> 1.0)
242
+ unicode-display_width (3.2.0)
243
+ unicode-emoji (~> 4.1)
244
+ unicode-emoji (4.1.0)
245
+ uri (1.1.1)
246
+ useragent (0.16.11)
247
+ websocket-driver (0.8.0)
248
+ base64
249
+ websocket-extensions (>= 0.1.0)
250
+ websocket-extensions (0.1.5)
251
+ zeitwerk (2.7.3)
252
+
253
+ PLATFORMS
254
+ x86_64-darwin
255
+
256
+ DEPENDENCIES
257
+ awesome_print
258
+ propshaft
259
+ pry-rails
260
+ puma
261
+ rails_toggleable!
262
+ rubocop-rails-omakase
263
+ sqlite3
264
+
265
+ BUNDLED WITH
266
+ 2.6.3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright aric.zheng
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # rails_toggleable
2
+ > A Rails plugin for configurable boolean toggle fields with dynamic methods like enable_*, disable_*, toggle_*.
3
+
4
+ ## Usage
5
+ ```sh
6
+ rails generate toggleable_field active --model User
7
+ rails generate toggleable_field published --model Post
8
+ ```
9
+
10
+ ## Installation
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem "rails_toggleable"
15
+ ```
16
+
17
+ And then execute:
18
+ ```bash
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+ ```bash
24
+ $ gem install rails_toggleable
25
+ ```
26
+
27
+ ## Contributing
28
+ Contribution directions go here.
29
+
30
+ ## License
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ require "bundler/gem_tasks"
File without changes
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
File without changes
@@ -0,0 +1,4 @@
1
+ module RailsToggleable
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RailsToggleable
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RailsToggleable
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module RailsToggleable
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: "from@example.com"
4
+ layout "mailer"
5
+ end
6
+ end
File without changes
@@ -0,0 +1,5 @@
1
+ module RailsToggleable
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Rails toggleable</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= yield :head %>
9
+
10
+ <%= stylesheet_link_tag "rails_toggleable/application", media: "all" %>
11
+ </head>
12
+ <body>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
data/bin/rails ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails gems
3
+ # installed from the root of your application.
4
+
5
+ ENGINE_ROOT = File.expand_path("..", __dir__)
6
+ ENGINE_PATH = File.expand_path("../lib/rails_toggleable/engine", __dir__)
7
+ APP_PATH = File.expand_path("../test/dummy/config/application", __dir__)
8
+
9
+ # Set up gems listed in the Gemfile.
10
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
11
+ require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
12
+
13
+ require "rails/all"
14
+ require "rails/engine/commands"
data/bin/rubocop ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require "bundler/setup"
4
+
5
+ # explicit rubocop config increases performance slightly while avoiding config confusion.
6
+ ARGV.unshift("--config", File.expand_path("../.rubocop.yml", __dir__))
7
+
8
+ load Gem.bin_path("rubocop", "rubocop")
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ RailsToggleable::Engine.routes.draw do
2
+ end
@@ -0,0 +1,74 @@
1
+ # lib/generators/toggleable_field/toggleable_field_generator.rb
2
+ class ToggleableFieldGenerator < Rails::Generators::NamedBase
3
+ class_option :model, type: :string, aliases: '-m', desc: "Model to add the field to"
4
+
5
+ def field_name
6
+ file_name
7
+ end
8
+
9
+ def model_name
10
+ options[:model] || file_name.singularize.classify
11
+ end
12
+
13
+ def table_name
14
+ model_name.underscore.pluralize
15
+ end
16
+
17
+ def create_migration_file
18
+ # Step 1: 使用 rails generate migration 创建一个带时间戳的空迁移文件
19
+ # 迁移类名通常为 AddFieldNameToTableName (e.g., AddActiveToUsers)
20
+ migration_class_name = "Add#{field_name.camelize}To#{table_name.camelize}"
21
+
22
+ # 调用 migration generator 创建骨架
23
+ generate "migration", migration_class_name
24
+
25
+ # Step 2: 找到刚刚生成的、带有最新时间戳的迁移文件
26
+ # Rails generators 将文件添加到 pending 操作中,我们可以从这里获取路径
27
+ # 更可靠的方法是搜索 db/migrate 目录下以该类名结尾的 .rb 文件
28
+ generated_files = Dir.glob(Rails.root.join("db/migrate/*_#{migration_class_name.underscore}.rb"))
29
+ if generated_files.empty?
30
+ abort "Error: Failed to find the generated migration file for #{migration_class_name}."
31
+ end
32
+
33
+ # 取最新的一个(理论上应该只有一个)
34
+ migration_file_path = generated_files.sort.last
35
+
36
+ # Step 3: 读取现有内容并插入我们的代码
37
+ content = <<~RUBY
38
+ class #{migration_class_name} < ActiveRecord::Migration[#{migration_version}]
39
+ def change
40
+ add_column :#{table_name}, :#{field_name}, :boolean, default: true
41
+ # Consider adding an index if you frequently query by this field
42
+ # add_index :#{table_name}, :#{field_name}
43
+ end
44
+ end
45
+ RUBY
46
+
47
+ # 写入新内容
48
+ File.open(migration_file_path, 'w') do |file|
49
+ file.write(content)
50
+ end
51
+
52
+ # Step 4: 向用户报告
53
+ log "\nUpdated migration file: #{migration_file_path.sub(Rails.root.to_s + '/', '')}"
54
+ end
55
+
56
+ private
57
+
58
+ def migration_version
59
+ "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}"
60
+ end
61
+
62
+ def show_readme
63
+ log "\n\n"
64
+ log "Next steps:"
65
+ log "1. Review the updated migration file in db/migrate/ for correctness."
66
+ log "2. Run `rails db:migrate` to apply the changes to your database."
67
+ log "3. Add `toggleable_field :#{field_name}, default: true` to your #{model_name} model."
68
+ log "\nExample in app/models/#{model_name.underscore}.rb:"
69
+ log " class #{model_name} < ApplicationRecord"
70
+ log " toggleable_field :#{field_name}, default: true # or false, as needed"
71
+ log " end"
72
+ log "\n"
73
+ end
74
+ end
@@ -0,0 +1,16 @@
1
+ # lib/rails_toggleable/engine.rb
2
+ require 'rails'
3
+ require_relative 'toggleable_model_methods'
4
+
5
+ module RailsToggleable
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace RailsToggleable
8
+
9
+ # 将 ToggleableModelMethods 混入 ActiveRecord::Base 的实例
10
+ initializer "rails_toggleable.active_record" do
11
+ ActiveSupport.on_load :active_record do
12
+ include RailsToggleable::ToggleableModelMethods
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,37 @@
1
+ # lib/rails_toggleable/toggleable_model_methods.rb
2
+ module RailsToggleable
3
+ module ToggleableModelMethods
4
+ # 使用 included 回调来修改包含此模块的模型类
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def toggleable_field(field_name, options = {})
11
+ field_sym = field_name.to_sym
12
+ field_str = field_name.to_s
13
+ default_value = options.fetch(:default, true)
14
+
15
+ scope field_sym, -> { where(field_sym => true) }
16
+ scope :"not_#{field_sym}", -> { where(field_sym => false) }
17
+
18
+ define_method "#{field_str}?" do
19
+ self.send(field_sym)
20
+ end
21
+
22
+ define_method "enable_#{field_str}!" do
23
+ update!(field_sym => true)
24
+ end
25
+
26
+ define_method "disable_#{field_str}!" do
27
+ update!(field_sym => false)
28
+ end
29
+
30
+ define_method "toggle_#{field_str}!" do
31
+ update!(field_sym => !self.send(field_sym))
32
+ end
33
+ end
34
+ end
35
+ # 实例方法可以放在这里,但当前没有
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module RailsToggleable
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ # lib/rails_toggleable.rb
2
+ require "rails_toggleable/engine"
3
+ require "rails_toggleable/version"
4
+
5
+ module RailsToggleable
6
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_toggleable do
3
+ # # Task goes here
4
+ # end
data/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "rails_toggleable",
3
+ "version": "1.0.1",
4
+ "private": true,
5
+ "description": "A Rails plugin for configurable boolean toggle fields with dynamic methods like enable_*, disable_*, toggle_*.",
6
+ "keywords": [
7
+ "rails",
8
+ "boolean",
9
+ "disable",
10
+ "enable",
11
+ "plugin",
12
+ "toggle"
13
+ ],
14
+ "author": "afeiship",
15
+ "scripts": {
16
+ "dev": "cd test/dummy && rails server",
17
+ "clean": "rm -rf *.gem",
18
+ "build": "gem build *.gemspec",
19
+ "pubpush": "gem push *.gem",
20
+ "release": "release-it"
21
+ }
22
+ }
@@ -0,0 +1,30 @@
1
+ # rails_toggleable.gemspec
2
+ $:.push File.expand_path("lib", __dir__)
3
+
4
+ require "rails_toggleable/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_toggleable"
8
+ spec.version = RailsToggleable::VERSION
9
+ spec.authors = ["aric.zheng"]
10
+ spec.email = ["1290657123@qq.com"]
11
+
12
+ spec.summary = "A Rails plugin for configurable boolean toggle fields."
13
+ spec.description = "Provides an easy way to add toggleable boolean fields (e.g., active, enabled) to Rails models with dynamic methods and scopes."
14
+ spec.homepage = "https://github.com/afeiship/rails_toggleable"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 2.7.0"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
21
+
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_dependency "rails", ">= 6.0.0"
30
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_toggleable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - aric.zheng
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ description: Provides an easy way to add toggleable boolean fields (e.g., active,
28
+ enabled) to Rails models with dynamic methods and scopes.
29
+ email:
30
+ - 1290657123@qq.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".github/disabled/dependabot.yml"
36
+ - ".github/disabled/workflows/ci.yml"
37
+ - ".gitignore"
38
+ - ".release-it.json"
39
+ - ".rubocop.yml"
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - MIT-LICENSE
43
+ - README.md
44
+ - Rakefile
45
+ - app/assets/images/rails_toggleable/.keep
46
+ - app/assets/stylesheets/rails_toggleable/application.css
47
+ - app/controllers/concerns/.keep
48
+ - app/controllers/rails_toggleable/application_controller.rb
49
+ - app/helpers/rails_toggleable/application_helper.rb
50
+ - app/jobs/rails_toggleable/application_job.rb
51
+ - app/mailers/rails_toggleable/application_mailer.rb
52
+ - app/models/concerns/.keep
53
+ - app/models/rails_toggleable/application_record.rb
54
+ - app/views/layouts/rails_toggleable/application.html.erb
55
+ - bin/rails
56
+ - bin/rubocop
57
+ - config/routes.rb
58
+ - lib/generators/toggleable_field/toggleable_field_generator.rb
59
+ - lib/rails_toggleable.rb
60
+ - lib/rails_toggleable/engine.rb
61
+ - lib/rails_toggleable/toggleable_model_methods.rb
62
+ - lib/rails_toggleable/version.rb
63
+ - lib/tasks/rails_toggleable_tasks.rake
64
+ - package.json
65
+ - rails_toggleable.gemspec
66
+ homepage: https://github.com/afeiship/rails_toggleable
67
+ licenses:
68
+ - MIT
69
+ metadata:
70
+ homepage_uri: https://github.com/afeiship/rails_toggleable
71
+ source_code_uri: https://github.com/afeiship/rails_toggleable
72
+ changelog_uri: https://github.com/afeiship/rails_toggleable/blob/main/CHANGELOG.md
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 2.7.0
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.5.22
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: A Rails plugin for configurable boolean toggle fields.
92
+ test_files: []