rails_statusable 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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/app/assets/stylesheets/rails_statusable/application.css +15 -0
- data/app/controllers/rails_statusable/application_controller.rb +4 -0
- data/app/helpers/rails_statusable/application_helper.rb +4 -0
- data/app/jobs/rails_statusable/application_job.rb +4 -0
- data/app/mailers/rails_statusable/application_mailer.rb +6 -0
- data/app/models/rails_statusable/application_record.rb +5 -0
- data/app/views/layouts/rails_statusable/application.html.erb +17 -0
- data/config/routes.rb +2 -0
- data/lib/generators/rails_statusable/install_generator.rb +29 -0
- data/lib/generators/rails_statusable/templates/migration.rb +6 -0
- data/lib/rails_statusable/configuration.rb +9 -0
- data/lib/rails_statusable/engine.rb +5 -0
- data/lib/rails_statusable/version.rb +3 -0
- data/lib/rails_statusable.rb +77 -0
- data/lib/tasks/rails_statusable_tasks.rake +4 -0
- metadata +76 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 96d51d3a333e9cce3825d523385659351d6db61274b2a86bd0bf8dd00ca9e5cc
|
|
4
|
+
data.tar.gz: 31866bfbcc30ffdbf408c5f7fafb5a00a641f16b73466465efb3d329dbaf1134
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ee37eac7ec574eb7f21c1bba64dbf2d3f78b0ee04857c7b17edbc856acba033b82426ff4b769186ef32fd4c1672697384b4cbcd68b5ca33ca246f482bdc24933
|
|
7
|
+
data.tar.gz: 522b0b535342d68867c3bc3bf8043ffd35dfa763e36f197eb8562173ee88ccf8b81d91dce2fdf66e8bed94455eea99ccd349dfddd313c464a30275d949ebc568
|
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,36 @@
|
|
|
1
|
+
# rails_statusable
|
|
2
|
+
> A Rails plugin for flexible, model-specific status fields with dynamic methods.
|
|
3
|
+
|
|
4
|
+
## Usage
|
|
5
|
+
```rb
|
|
6
|
+
class Post < ApplicationRecord
|
|
7
|
+
include RailsStatusable
|
|
8
|
+
statusable statuses: %w[draft published archived], default: 'draft'
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# install migration
|
|
12
|
+
rails generate rails_statusable:install
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
Add this line to your application's Gemfile:
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
gem "rails_statusable"
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
And then execute:
|
|
23
|
+
```bash
|
|
24
|
+
$ bundle
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Or install it yourself as:
|
|
28
|
+
```bash
|
|
29
|
+
$ gem install rails_statusable
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Resources
|
|
33
|
+
- https://chat.qwen.ai/c/6c5e5633-c893-4b29-982b-0d3a4f9f38b9
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
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,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
|
+
*/
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Rails statusable</title>
|
|
5
|
+
<%= csrf_meta_tags %>
|
|
6
|
+
<%= csp_meta_tag %>
|
|
7
|
+
|
|
8
|
+
<%= yield :head %>
|
|
9
|
+
|
|
10
|
+
<%= stylesheet_link_tag "rails_statusable/application", media: "all" %>
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
|
|
14
|
+
<%= yield %>
|
|
15
|
+
|
|
16
|
+
</body>
|
|
17
|
+
</html>
|
data/config/routes.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'rails/generators'
|
|
2
|
+
require 'rails/generators/active_record/migration'
|
|
3
|
+
|
|
4
|
+
module RailsStatusable
|
|
5
|
+
module Generators
|
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
|
7
|
+
include Rails::Generators::Migration
|
|
8
|
+
|
|
9
|
+
source_root File.expand_path('../templates', __FILE__)
|
|
10
|
+
desc "Generates a migration to add status column to specified table"
|
|
11
|
+
|
|
12
|
+
def create_migration_file
|
|
13
|
+
migration_template "migration.rb", "db/migrate/add_status_to_#{table_name}.rb"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def table_name
|
|
19
|
+
@table_name ||= ask("Enter the table name to add status field to (e.g., posts):")
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Implement the required method for Rails::Generators::Migration
|
|
23
|
+
def self.next_migration_number(dirname)
|
|
24
|
+
next_migration_number = current_migration_number(dirname) + 1
|
|
25
|
+
ActiveRecord::Migration.next_migration_number(next_migration_number)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
require "rails_statusable/engine"
|
|
2
|
+
|
|
3
|
+
module RailsStatusable
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
def configure(&block)
|
|
8
|
+
yield(configuration)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def configuration
|
|
12
|
+
@configuration ||= Configuration.new
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def config
|
|
16
|
+
configuration
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
included do
|
|
21
|
+
extend ClassMethods
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
module ClassMethods
|
|
25
|
+
def statusable(options = {})
|
|
26
|
+
class_attribute :status_config
|
|
27
|
+
self.status_config = {
|
|
28
|
+
field: options[:field] || :status,
|
|
29
|
+
statuses: options[:statuses] || RailsStatusable.config.default_statuses,
|
|
30
|
+
default: options[:default] || RailsStatusable.config.default_statuses.first
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
# Add validation for the status field
|
|
34
|
+
validates status_config[:field], inclusion: {
|
|
35
|
+
in: status_config[:statuses],
|
|
36
|
+
message: "%{value} is not a valid status"
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
# Set default value
|
|
40
|
+
after_initialize :set_default_status
|
|
41
|
+
|
|
42
|
+
# Generate dynamic methods for each status
|
|
43
|
+
status_config[:statuses].each do |status|
|
|
44
|
+
define_status_method(status)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Add scope methods for querying
|
|
48
|
+
status_config[:statuses].each do |status|
|
|
49
|
+
scope "by_#{status}", -> { where(status_config[:field] => status) }
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def define_status_method(status_name)
|
|
56
|
+
method_name = "#{status_name}?"
|
|
57
|
+
|
|
58
|
+
define_method(method_name) do
|
|
59
|
+
send(self.class.status_config[:field]).to_s.downcase == status_name.to_s.downcase
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Define setter method
|
|
63
|
+
setter_method_name = "#{status_name}!"
|
|
64
|
+
define_method(setter_method_name) do
|
|
65
|
+
send("#{self.class.status_config[:field]}=", status_name)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def set_default_status
|
|
73
|
+
if send(self.class.status_config[:field]).blank?
|
|
74
|
+
send("#{self.class.status_config[:field]}=", self.class.status_config[:default])
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rails_statusable
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- aric.zheng
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2025-11-07 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rails
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 6.0.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 6.0.0
|
|
26
|
+
description: Rails plugin that adds configurable status fields to models with dynamic
|
|
27
|
+
methods like draft?, published?, etc.
|
|
28
|
+
email:
|
|
29
|
+
- 1290657123@qq.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- MIT-LICENSE
|
|
35
|
+
- README.md
|
|
36
|
+
- Rakefile
|
|
37
|
+
- app/assets/stylesheets/rails_statusable/application.css
|
|
38
|
+
- app/controllers/rails_statusable/application_controller.rb
|
|
39
|
+
- app/helpers/rails_statusable/application_helper.rb
|
|
40
|
+
- app/jobs/rails_statusable/application_job.rb
|
|
41
|
+
- app/mailers/rails_statusable/application_mailer.rb
|
|
42
|
+
- app/models/rails_statusable/application_record.rb
|
|
43
|
+
- app/views/layouts/rails_statusable/application.html.erb
|
|
44
|
+
- config/routes.rb
|
|
45
|
+
- lib/generators/rails_statusable/install_generator.rb
|
|
46
|
+
- lib/generators/rails_statusable/templates/migration.rb
|
|
47
|
+
- lib/rails_statusable.rb
|
|
48
|
+
- lib/rails_statusable/configuration.rb
|
|
49
|
+
- lib/rails_statusable/engine.rb
|
|
50
|
+
- lib/rails_statusable/version.rb
|
|
51
|
+
- lib/tasks/rails_statusable_tasks.rake
|
|
52
|
+
homepage: https://github.com/afeiship/rails_statusable
|
|
53
|
+
licenses:
|
|
54
|
+
- MIT
|
|
55
|
+
metadata:
|
|
56
|
+
homepage_uri: https://github.com/afeiship/rails_statusable
|
|
57
|
+
source_code_uri: https://github.com/afeiship/rails_statusable
|
|
58
|
+
changelog_uri: https://github.com/afeiship/rails_statusable/blob/main/CHANGELOG.md
|
|
59
|
+
rdoc_options: []
|
|
60
|
+
require_paths:
|
|
61
|
+
- lib
|
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - ">="
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: 2.6.0
|
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
68
|
+
requirements:
|
|
69
|
+
- - ">="
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '0'
|
|
72
|
+
requirements: []
|
|
73
|
+
rubygems_version: 3.6.3
|
|
74
|
+
specification_version: 4
|
|
75
|
+
summary: A Rails plugin for flexible, model-specific status fields with dynamic methods.
|
|
76
|
+
test_files: []
|