rails-code-generator 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.
Files changed (34) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +8 -0
  4. data/CHANGELOG.md +18 -0
  5. data/LICENSE.txt +21 -0
  6. data/README.md +98 -0
  7. data/Rakefile +12 -0
  8. data/lib/generators/rails_code_generator/resources/resources_generator.rb +40 -0
  9. data/lib/generators/rails_code_generator/resources/templates/bulk/build.rb.tt +38 -0
  10. data/lib/generators/rails_code_generator/resources/templates/bulk/controller.rb.tt +62 -0
  11. data/lib/generators/rails_code_generator/resources/templates/bulk/create_spec.rb.tt +24 -0
  12. data/lib/generators/rails_code_generator/resources/templates/bulk/create_validator.rb.tt +11 -0
  13. data/lib/generators/rails_code_generator/resources/templates/bulk/destroy_spec.rb.tt +17 -0
  14. data/lib/generators/rails_code_generator/resources/templates/bulk/save.rb.tt +24 -0
  15. data/lib/generators/rails_code_generator/resources/templates/bulk/update.rb.tt +43 -0
  16. data/lib/generators/rails_code_generator/resources/templates/bulk/update_spec.rb.tt +26 -0
  17. data/lib/generators/rails_code_generator/resources/templates/bulk/update_validator.rb.tt +11 -0
  18. data/lib/generators/rails_code_generator/resources/templates/shared/index_spec.rb.tt +16 -0
  19. data/lib/generators/rails_code_generator/resources/templates/shared/show_spec.rb.tt +16 -0
  20. data/lib/generators/rails_code_generator/resources/templates/single/build.rb.tt +28 -0
  21. data/lib/generators/rails_code_generator/resources/templates/single/controller.rb.tt +54 -0
  22. data/lib/generators/rails_code_generator/resources/templates/single/create_spec.rb.tt +24 -0
  23. data/lib/generators/rails_code_generator/resources/templates/single/create_validator.rb.tt +11 -0
  24. data/lib/generators/rails_code_generator/resources/templates/single/destroy_spec.rb.tt +16 -0
  25. data/lib/generators/rails_code_generator/resources/templates/single/save.rb.tt +23 -0
  26. data/lib/generators/rails_code_generator/resources/templates/single/update.rb.tt +31 -0
  27. data/lib/generators/rails_code_generator/resources/templates/single/update_spec.rb.tt +26 -0
  28. data/lib/generators/rails_code_generator/resources/templates/single/update_validator.rb.tt +11 -0
  29. data/lib/rails/code/generator/naming.rb +37 -0
  30. data/lib/rails/code/generator/railtie.rb +13 -0
  31. data/lib/rails/code/generator/version.rb +9 -0
  32. data/lib/rails/code/generator.rb +14 -0
  33. data/sig/rails/code/generator.rbs +13 -0
  34. metadata +93 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ae730a9fa958ca9c87a6208933bf17a6ae68abcb59ffa7ca8a5a38e11433d619
4
+ data.tar.gz: 7961f74bcae4ef3f11a5881f0e9443ece5efac17e08839f7c5cc4530ca2765d3
5
+ SHA512:
6
+ metadata.gz: 9e11301d2a0d69a8f36d8be68f2474a6579b5e3a71349169cd04f44b3f56dda8eb5258e680963355c73bee12f8b4b190ec32f9143535cf0de8935d7912336040
7
+ data.tar.gz: 72dab9e9cef3b342762ca8478a46e0c0ec778f2d4804e8c79995b4863bed5e6c5e13bbed8e39d4f44fa473c6a017caabe0b4e37d04459bd83db705e7bf0c0a20
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ### Added
11
+
12
+ - `rails_code_generator:resources` generator with `--bulk` flag for controller, services, validators, and request specs
13
+
14
+ ## [0.1.0] - 2026-07-31
15
+
16
+ ### Added
17
+
18
+ - Initial gem setup with Rails generator boilerplate
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Michael Sweat
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.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # rails-code-generator
2
+
3
+ Custom Rails generators for scaffolding and code generation in Rails applications.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "rails-code-generator", git: "https://github.com/msweat254/rails-code-generator.git"
11
+ ```
12
+
13
+ Or install directly from GitHub:
14
+
15
+ ```bash
16
+ gem install rails-code-generator --source https://rubygems.org
17
+ ```
18
+
19
+ Once published to RubyGems.org:
20
+
21
+ ```bash
22
+ bundle add rails-code-generator
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Generators are invoked from within a Rails application.
28
+
29
+ ### `rails_code_generator:resources`
30
+
31
+ Generates a full resource stack (controller, services, validators, and request specs) from Ion Solar backend conventions.
32
+
33
+ ```bash
34
+ bin/rails generate rails_code_generator:resources PricingConfig
35
+ bin/rails generate rails_code_generator:resources PricingConfig --bulk
36
+ ```
37
+
38
+ **Options**
39
+
40
+ | Flag | Default | Description |
41
+ |------|---------|-------------|
42
+ | `--bulk` | `false` | Generate bulk create/update/destroy patterns |
43
+
44
+ **Generated files** (for `PricingConfig`):
45
+
46
+ | Path | Single | Bulk |
47
+ |------|--------|------|
48
+ | `app/controllers/pricing_configs_controller.rb` | yes | yes |
49
+ | `app/services/pricing_configs/build.rb` | yes | yes |
50
+ | `app/services/pricing_configs/save.rb` | yes | yes |
51
+ | `app/services/pricing_configs/update.rb` | yes | yes |
52
+ | `app/validators/pricing_configs/create_validator.rb` | yes | yes |
53
+ | `app/validators/pricing_configs/update_validator.rb` | yes | yes |
54
+ | `spec/requests/pricing_configs/index_spec.rb` | yes | yes |
55
+ | `spec/requests/pricing_configs/show_spec.rb` | yes | yes |
56
+ | `spec/requests/pricing_configs/create_spec.rb` | yes | yes |
57
+ | `spec/requests/pricing_configs/update_spec.rb` | yes | yes |
58
+ | `spec/requests/pricing_configs/destroy_spec.rb` | yes | yes |
59
+
60
+ **Bulk vs single differences**
61
+
62
+ - Controller, services, and validators use single or bulk snippet patterns
63
+ - Request specs: bulk create/update use `pricing_configs: {}` param key (plural); bulk destroy passes `params: { ids: ids }` instead of an id in the path
64
+ - `PERMITTED_ATTRIBUTES` and validator fields are generated as stubs — fill them in after running the generator
65
+
66
+ Other generators:
67
+
68
+ ```bash
69
+ bin/rails generate rails_code_generator:<generator_name>
70
+ ```
71
+
72
+ ## Development
73
+
74
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt.
75
+
76
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
77
+
78
+ ### Generator structure
79
+
80
+ Generators live under `lib/generators/rails_code_generator/`. Each generator follows the standard Rails generator layout:
81
+
82
+ ```
83
+ lib/generators/rails_code_generator/
84
+ my_thing/
85
+ my_thing_generator.rb
86
+ templates/
87
+ ...
88
+ ```
89
+
90
+ Invoked as `bin/rails generate rails_code_generator:my_thing`.
91
+
92
+ ## Contributing
93
+
94
+ Bug reports and pull requests are welcome on GitHub at [msweat254/rails-code-generator](https://github.com/msweat254/rails-code-generator).
95
+
96
+ ## License
97
+
98
+ 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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+ require "rails/generators/named_base"
5
+ require "rails/code/generator/naming"
6
+
7
+ module RailsCodeGenerator
8
+ class ResourcesGenerator < Rails::Generators::NamedBase
9
+ include Rails::Code::Generator::Naming
10
+
11
+ class_option :bulk, type: :boolean, default: false, desc: "Generate bulk endpoints"
12
+
13
+ source_root File.expand_path("templates", __dir__)
14
+
15
+ def create_controller
16
+ template "#{template_mode}/controller.rb", File.join("app/controllers", "#{table_name}_controller.rb")
17
+ end
18
+
19
+ def create_services
20
+ template "#{template_mode}/build.rb", File.join("app/services", table_name, "build.rb")
21
+ template "#{template_mode}/save.rb", File.join("app/services", table_name, "save.rb")
22
+ template "#{template_mode}/update.rb", File.join("app/services", table_name, "update.rb")
23
+ end
24
+
25
+ def create_validators
26
+ template "#{template_mode}/create_validator.rb",
27
+ File.join("app/validators", table_name, "create_validator.rb")
28
+ template "#{template_mode}/update_validator.rb",
29
+ File.join("app/validators", table_name, "update_validator.rb")
30
+ end
31
+
32
+ def create_request_specs
33
+ template "shared/index_spec.rb", File.join("spec/requests", table_name, "index_spec.rb")
34
+ template "shared/show_spec.rb", File.join("spec/requests", table_name, "show_spec.rb")
35
+ template "#{template_mode}/create_spec.rb", File.join("spec/requests", table_name, "create_spec.rb")
36
+ template "#{template_mode}/update_spec.rb", File.join("spec/requests", table_name, "update_spec.rb")
37
+ template "#{template_mode}/destroy_spec.rb", File.join("spec/requests", table_name, "destroy_spec.rb")
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class Build
5
+ PERMITTED_ATTRIBUTES = [
6
+ ].freeze
7
+
8
+ def self.call params
9
+ return if params.blank?
10
+
11
+ new(params).call
12
+ end
13
+
14
+ def initialize params
15
+ self.params = ServiceHelpers::CleanBuildParams.call(
16
+ params: params,
17
+ permitted_attributes: PERMITTED_ATTRIBUTES,
18
+ )
19
+ end
20
+
21
+ def call
22
+ <%= table_name %> = params.map do |param|
23
+ ::<%= class_name %>.new param
24
+ end
25
+
26
+ Current.set_previous_and_current_entities(
27
+ previous_entities: [],
28
+ current_entities: <%= table_name %>,
29
+ )
30
+
31
+ <%= table_name %>
32
+ end
33
+
34
+ private
35
+
36
+ attr_accessor :params
37
+ end
38
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= resource_controller_class %> < ApplicationController
4
+ def index
5
+ <%= table_name %> = if params[:ids]
6
+ <%= class_name %>.where id: params[:ids]
7
+ else
8
+ <%= class_name %>.all
9
+ end
10
+
11
+ render json: <%= resource_normalizer_class %>.new(<%= table_name %>, params)
12
+ end
13
+
14
+ def show
15
+ <%= singular_name %> = <%= class_name %>.find params[:id]
16
+
17
+ render json: <%= resource_normalizer_class %>.new(<%= singular_name %>)
18
+ end
19
+
20
+ def create
21
+ validator = <%= resource_module_name %>::CreateValidator.call params
22
+ errors = ValidationErrorFormatter.call validator
23
+
24
+ if errors.empty?
25
+ new_<%= table_name %> = <%= resource_module_name %>::Build.call <%= singular_name %>_params
26
+ <%= table_name %> = <%= resource_module_name %>::Save.call <%= table_name %>: new_<%= table_name %>
27
+ Activities::Save.call "<%= class_name %>"
28
+ end
29
+
30
+ render json: <%= resource_normalizer_class %>.new(<%= table_name %>, { errors: errors })
31
+ end
32
+
33
+ def update
34
+ validator = <%= resource_module_name %>::UpdateValidator.call params
35
+ errors = ValidationErrorFormatter.call validator
36
+
37
+ if errors.empty?
38
+ updated_<%= table_name %> = <%= resource_module_name %>::Update.call <%= singular_name %>_params
39
+
40
+ <%= table_name %> = <%= resource_module_name %>::Save.call(
41
+ <%= table_name %>: updated_<%= table_name %>[:<%= table_name %>],
42
+ columns_to_update: updated_<%= table_name %>[:columns_to_update],
43
+ )
44
+ Activities::Save.call "<%= class_name %>"
45
+ end
46
+
47
+ render json: <%= resource_normalizer_class %>.new(<%= table_name %>, { errors: errors })
48
+ end
49
+
50
+ def destroy
51
+ <%= table_name %> = <%= class_name %>.where(id: params[:ids])&.destroy_all
52
+ Activities::Save.call "<%= class_name %>"
53
+
54
+ render json: <%= resource_normalizer_class %>.new(<%= table_name %>)
55
+ end
56
+
57
+ private
58
+
59
+ def <%= singular_name %>_params
60
+ params.require :<%= table_name %>
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ RSpec.describe "[POST] /<%= route_path %>", type: :request do
6
+ Given(:path) { "/<%= route_path %>" }
7
+ Given(:user) { create :user }
8
+
9
+ context "with valid params" do
10
+ Given(:params) {
11
+ {
12
+ <%= table_name %>: {
13
+ # TODO: add attributes
14
+ },
15
+ }.to_json
16
+ }
17
+
18
+ When { post path, params: params, headers: api_auth_headers(user) }
19
+
20
+ Then { expect(response).to be_successful }
21
+ And { expect(response_body[:errors]).to be_empty }
22
+ And { expect(response_body[:data]).not_to be_empty }
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class CreateValidator < BaseValidator
5
+ params do
6
+ required(:<%= table_name %>).maybe :hash do
7
+ # TODO: add attributes
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ RSpec.describe "[DELETE] /<%= route_path %>", type: :request do
6
+ Given(:path) { "/<%= route_path %>" }
7
+ Given(:user) { create :user }
8
+ Given(:<%= singular_name %>) { create :<%= singular_name %> }
9
+ Given(:ids) { [<%= singular_name %>.id] }
10
+
11
+ context "with authenticated user" do
12
+ When { delete path, params: { ids: ids }, headers: api_auth_headers(user) }
13
+
14
+ Then { expect(response_body[:errors]).to be_empty }
15
+ And { expect(response).to be_successful }
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class Save
5
+ attr_accessor :<%= table_name %>, :columns_to_update
6
+
7
+ def self.call <%= table_name %>:, columns_to_update: nil
8
+ return if <%= table_name %>.blank?
9
+
10
+ new(<%= table_name %>, columns_to_update).call
11
+ end
12
+
13
+ def initialize <%= table_name %>, columns_to_update
14
+ self.<%= table_name %> = <%= table_name %>
15
+ self.columns_to_update = columns_to_update
16
+ end
17
+
18
+ def call
19
+ <%= class_name %>.import <%= table_name %>.to_a, on_duplicate_key_update: columns_to_update
20
+
21
+ <%= table_name %>
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class Update
5
+ PERMITTED_ATTRIBUTES = [
6
+ :id,
7
+ ].freeze
8
+
9
+ def self.call params
10
+ return if params.blank?
11
+
12
+ new(params).call
13
+ end
14
+
15
+ def initialize params
16
+ self.params = ServiceHelpers::CleanUpdateParams.call(
17
+ params: params,
18
+ permitted_attributes: PERMITTED_ATTRIBUTES,
19
+ )
20
+ end
21
+
22
+ def call
23
+ <%= table_name %> = <%= class_name %>.where id: params.keys
24
+
25
+ previous_<%= table_name %> = <%= table_name %>.map(&:dup)
26
+
27
+ <%= table_name %>.each do |<%= singular_name %>|
28
+ <%= singular_name %>.assign_attributes params[<%= singular_name %>.id]
29
+ end
30
+
31
+ Current.set_previous_and_current_entities(
32
+ previous_entities: previous_<%= table_name %>,
33
+ current_entities: <%= table_name %>,
34
+ )
35
+
36
+ { <%= table_name %>: <%= table_name %>, columns_to_update: PERMITTED_ATTRIBUTES - [:id] }
37
+ end
38
+
39
+ private
40
+
41
+ attr_accessor :params
42
+ end
43
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ RSpec.describe "[PATCH] /<%= route_path %>/:id", type: :request do
6
+ Given(:path) { "/<%= route_path %>/" + <%= singular_name %>.id.to_s }
7
+ Given(:<%= singular_name %>) { create :<%= singular_name %> }
8
+ Given(:user) { create :user }
9
+
10
+ context "with authenticated user & valid params" do
11
+ Given(:params) {
12
+ {
13
+ <%= table_name %>: {
14
+ name: "Changed <%= singular_name %> name",
15
+ },
16
+ }.to_json
17
+ }
18
+
19
+ When { patch path, params: params, headers: api_auth_headers(user) }
20
+
21
+ Then { expect(response).to be_successful }
22
+ And { expect(response_body[:data]).not_to be_empty }
23
+ And { expect(response_body[:errors]).to be_empty }
24
+ And { expect(response_body.dig(:data, :name)).to eq "Changed <%= singular_name %> name" }
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class UpdateValidator < BaseValidator
5
+ params do
6
+ required(:<%= table_name %>).maybe :hash do
7
+ # TODO: add attributes
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ RSpec.describe "[GET] /<%= route_path %>", type: :request do
6
+ Given(:path) { "/<%= route_path %>" }
7
+ Given(:user) { create :user }
8
+ Given(:<%= singular_name %>) { create :<%= singular_name %> }
9
+
10
+ context "with authenticated user and valid params" do
11
+ When { get path, headers: api_auth_headers(user) }
12
+
13
+ Then { expect(response_body[:data]).not_to be_empty }
14
+ And { expect(response_body[:errors]).to be_empty }
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ RSpec.describe "[GET] /<%= route_path %>/:id", type: :request do
6
+ Given(:path) { "/<%= route_path %>/#{<%= singular_name %>.id}" }
7
+ Given(:user) { create :user }
8
+ Given(:<%= singular_name %>) { create :<%= singular_name %> }
9
+
10
+ context "with authenticated user and valid params" do
11
+ When { get path, headers: api_auth_headers(user) }
12
+
13
+ Then { expect(response_body[:data]).not_to be_empty }
14
+ And { expect(response_body[:errors]).to be_empty }
15
+ end
16
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class Build
5
+ PERMITTED_ATTRIBUTES = [
6
+ ].freeze
7
+
8
+ def self.call params
9
+ new(params).call
10
+ end
11
+
12
+ def initialize params
13
+ self.params = if params.is_a? ActionController::Parameters
14
+ params.permit(*PERMITTED_ATTRIBUTES)
15
+ else
16
+ params.slice(*PERMITTED_ATTRIBUTES)
17
+ end
18
+ end
19
+
20
+ def call
21
+ self.<%= singular_name %> = ::<%= class_name %>.new params
22
+ end
23
+
24
+ private
25
+
26
+ attr_accessor :<%= singular_name %>, :params
27
+ end
28
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= resource_controller_class %> < ApplicationController
4
+ def index
5
+ <%= table_name %> = if params[:ids].present?
6
+ <%= class_name %>.where ids: params[:ids]
7
+ else
8
+ <%= class_name %>.all
9
+ end
10
+
11
+ render json: <%= resource_normalizer_class %>.new(<%= table_name %>, params)
12
+ end
13
+
14
+ def show
15
+ <%= singular_name %> = <%= class_name %>.find params[:id]
16
+
17
+ render json: <%= resource_normalizer_class %>.new(<%= singular_name %>)
18
+ end
19
+
20
+ def create
21
+ validator = <%= resource_module_name %>::CreateValidator.call params
22
+ errors = ValidationErrorFormatter.call validator
23
+ validated_params = validator.to_h
24
+
25
+ if errors.empty?
26
+ <%= singular_name %> = <%= resource_module_name %>::Build.call validated_params
27
+ <%= singular_name %> = <%= resource_module_name %>::Save.call <%= singular_name %>
28
+ end
29
+
30
+ render json: <%= resource_normalizer_class %>.new(<%= singular_name %>)
31
+ end
32
+
33
+ def update
34
+ validator = <%= resource_module_name %>::UpdateValidator.call params
35
+ errors = ValidationErrorFormatter.call validator
36
+ validated_params = validator.to_h
37
+
38
+ if errors.empty?
39
+ <%= singular_name %> = <%= class_name %>.find params[:id]
40
+ <%= singular_name %> = <%= resource_module_name %>::Update.call <%= singular_name %>, validated_params
41
+ <%= singular_name %> = <%= resource_module_name %>::Save.call <%= singular_name %>
42
+ end
43
+
44
+ render json: <%= resource_normalizer_class %>.new(<%= singular_name %>)
45
+ end
46
+
47
+ def destroy
48
+ <%= singular_name %> = <%= class_name %>.find params[:id]
49
+
50
+ <%= singular_name %>.destroy!
51
+
52
+ render json: { message: "<%= class_name %> deleted successfully" }
53
+ end
54
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ RSpec.describe "[POST] /<%= route_path %>", type: :request do
6
+ Given(:path) { "/<%= route_path %>" }
7
+ Given(:user) { create :user }
8
+
9
+ context "with valid params" do
10
+ Given(:params) {
11
+ {
12
+ <%= singular_name %>: {
13
+ # TODO: add attributes
14
+ },
15
+ }.to_json
16
+ }
17
+
18
+ When { post path, params: params, headers: api_auth_headers(user) }
19
+
20
+ Then { expect(response).to be_successful }
21
+ And { expect(response_body[:errors]).to be_empty }
22
+ And { expect(response_body[:data]).not_to be_empty }
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class CreateValidator < BaseValidator
5
+ params do
6
+ required(:<%= singular_name %>).maybe :hash do
7
+ # TODO: add attributes
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ RSpec.describe "[DELETE] /<%= route_path %>/:id", type: :request do
6
+ Given(:path) { "/<%= route_path %>/#{<%= singular_name %>.id}" }
7
+ Given(:user) { create :user }
8
+ Given(:<%= singular_name %>) { create :<%= singular_name %> }
9
+
10
+ context "with authenticated user" do
11
+ When { delete path, headers: api_auth_headers(user) }
12
+
13
+ Then { expect(response_body[:errors]).to be_empty }
14
+ And { expect(response).to be_successful }
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class Save
5
+ attr_accessor :<%= singular_name %>
6
+
7
+ def self.call <%= singular_name %>
8
+ return if <%= singular_name %>.nil?
9
+
10
+ new(<%= singular_name %>).call
11
+ end
12
+
13
+ def initialize <%= singular_name %>
14
+ self.<%= singular_name %> = <%= singular_name %>
15
+ end
16
+
17
+ def call
18
+ <%= singular_name %>.save!
19
+
20
+ <%= singular_name %>
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class Update
5
+ PERMITTED_ATTRIBUTES = [
6
+ ].freeze
7
+
8
+ def self.call <%= singular_name %>, params
9
+ new(<%= singular_name %>, params).call
10
+ end
11
+
12
+ def initialize <%= singular_name %>, params
13
+ self.<%= singular_name %> = <%= singular_name %>
14
+ self.params = if params.is_a? ActionController::Parameters
15
+ params.permit(*PERMITTED_ATTRIBUTES)
16
+ else
17
+ params.slice(*PERMITTED_ATTRIBUTES)
18
+ end
19
+ end
20
+
21
+ def call
22
+ <%= singular_name %>.assign_attributes params
23
+
24
+ <%= singular_name %>
25
+ end
26
+
27
+ private
28
+
29
+ attr_accessor :<%= singular_name %>, :params
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ RSpec.describe "[PATCH] /<%= route_path %>/:id", type: :request do
6
+ Given(:path) { "/<%= route_path %>/#{<%= singular_name %>.id}" }
7
+ Given(:<%= singular_name %>) { create :<%= singular_name %> }
8
+ Given(:user) { create :user }
9
+
10
+ context "with authenticated user & valid params" do
11
+ Given(:params) {
12
+ {
13
+ <%= singular_name %>: {
14
+ name: "Changed <%= singular_name %> name",
15
+ },
16
+ }.to_json
17
+ }
18
+
19
+ When { patch path, params: params, headers: api_auth_headers(user) }
20
+
21
+ Then { expect(response).to be_successful }
22
+ And { expect(response_body[:data]).not_to be_empty }
23
+ And { expect(response_body[:errors]).to be_empty }
24
+ And { expect(response_body.dig(:data, :name)).to eq "Changed <%= singular_name %> name" }
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module <%= resource_module_name %>
4
+ class UpdateValidator < BaseValidator
5
+ params do
6
+ required(:<%= singular_name %>).maybe :hash do
7
+ # TODO: add attributes
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Code
5
+ module Generator
6
+ module Naming
7
+ def singular_name
8
+ file_name
9
+ end
10
+
11
+ def plural_class_name
12
+ table_name.camelize
13
+ end
14
+
15
+ def resource_controller_class
16
+ "#{plural_class_name}Controller"
17
+ end
18
+
19
+ def resource_normalizer_class
20
+ "#{plural_class_name}Normalizer"
21
+ end
22
+
23
+ def route_path
24
+ table_name
25
+ end
26
+
27
+ def resource_module_name
28
+ plural_class_name
29
+ end
30
+
31
+ def template_mode
32
+ options[:bulk] ? "bulk" : "single"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Code
5
+ module Generator
6
+ class Railtie < ::Rails::Railtie
7
+ generators do
8
+ # Register generators here as they are added.
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Code
5
+ module Generator
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "generator/version"
4
+ require_relative "generator/naming"
5
+
6
+ module Rails
7
+ module Code
8
+ module Generator
9
+ class Error < StandardError; end
10
+ end
11
+ end
12
+ end
13
+
14
+ require_relative "generator/railtie" if defined?(::Rails::Railtie)
@@ -0,0 +1,13 @@
1
+ module Rails
2
+ module Code
3
+ module Generator
4
+ VERSION: String
5
+
6
+ class Error < StandardError
7
+ end
8
+
9
+ class Railtie < ::Rails::Railtie
10
+ end
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-code-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Sweat
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2026-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ description: A collection of Rails generators that automate common code generation
28
+ tasks in Rails applications.
29
+ email:
30
+ - michael.sweat@ionsolar.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - CHANGELOG.md
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/generators/rails_code_generator/resources/resources_generator.rb
42
+ - lib/generators/rails_code_generator/resources/templates/bulk/build.rb.tt
43
+ - lib/generators/rails_code_generator/resources/templates/bulk/controller.rb.tt
44
+ - lib/generators/rails_code_generator/resources/templates/bulk/create_spec.rb.tt
45
+ - lib/generators/rails_code_generator/resources/templates/bulk/create_validator.rb.tt
46
+ - lib/generators/rails_code_generator/resources/templates/bulk/destroy_spec.rb.tt
47
+ - lib/generators/rails_code_generator/resources/templates/bulk/save.rb.tt
48
+ - lib/generators/rails_code_generator/resources/templates/bulk/update.rb.tt
49
+ - lib/generators/rails_code_generator/resources/templates/bulk/update_spec.rb.tt
50
+ - lib/generators/rails_code_generator/resources/templates/bulk/update_validator.rb.tt
51
+ - lib/generators/rails_code_generator/resources/templates/shared/index_spec.rb.tt
52
+ - lib/generators/rails_code_generator/resources/templates/shared/show_spec.rb.tt
53
+ - lib/generators/rails_code_generator/resources/templates/single/build.rb.tt
54
+ - lib/generators/rails_code_generator/resources/templates/single/controller.rb.tt
55
+ - lib/generators/rails_code_generator/resources/templates/single/create_spec.rb.tt
56
+ - lib/generators/rails_code_generator/resources/templates/single/create_validator.rb.tt
57
+ - lib/generators/rails_code_generator/resources/templates/single/destroy_spec.rb.tt
58
+ - lib/generators/rails_code_generator/resources/templates/single/save.rb.tt
59
+ - lib/generators/rails_code_generator/resources/templates/single/update.rb.tt
60
+ - lib/generators/rails_code_generator/resources/templates/single/update_spec.rb.tt
61
+ - lib/generators/rails_code_generator/resources/templates/single/update_validator.rb.tt
62
+ - lib/rails/code/generator.rb
63
+ - lib/rails/code/generator/naming.rb
64
+ - lib/rails/code/generator/railtie.rb
65
+ - lib/rails/code/generator/version.rb
66
+ - sig/rails/code/generator.rbs
67
+ homepage: https://github.com/msweat254/rails-code-generator
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://github.com/msweat254/rails-code-generator
72
+ source_code_uri: https://github.com/msweat254/rails-code-generator
73
+ changelog_uri: https://github.com/msweat254/rails-code-generator/blob/main/CHANGELOG.md
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.3.7
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Custom Rails generators for scaffolding and code generation
93
+ test_files: []