Skip to content

Rework to be a proper Rails plugin#181

Draft
OutlawAndy wants to merge 2 commits intomasterfrom
proper-rails-plugin
Draft

Rework to be a proper Rails plugin#181
OutlawAndy wants to merge 2 commits intomasterfrom
proper-rails-plugin

Conversation

@OutlawAndy
Copy link
Member

Why?

  1. This gem eager loads all generator classes during host application boot. This is unnecessary and actually goes against Rails best practices. I've recently learned that the proper way to supply generators to a Rails app requires a Railtie / Engine to notify the host application of their location on the file system and allow the host app to lazy load them if/when needed.
  2. This gem introduces 2 top-level namespaces (Rolemodel & RolemodelRails) which is a general anti-pattern for RubyGems.
  3. Naming convention for plugins like this one are to use a - rather than a _. e.g. rspec-rails, slim-rails, rubocop-rails, etc. This ties back into #2 because rolemodel-rails translates to Rolemodel::Rails, while rolemodel_rails translates to RolemodelRails.

What Changed

What changed in this PR?

  • rename to rolemodel-rails
  • add an Engine file to properly notify the host app about the generators
  • don't eager load generators
  • fix test setup which does need all constants that are tested to be eager loaded.

Pre-merge checklist

  • Update relevant READMEs
  • Run bin/bump_version or bin/bump_version --patch

@OutlawAndy OutlawAndy self-assigned this Mar 1, 2026
@OutlawAndy OutlawAndy force-pushed the proper-rails-plugin branch from 6748ef3 to d486dd2 Compare March 1, 2026 23:29
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Reworks the gem into a proper Rails plugin by renaming it to rolemodel-rails, introducing a Rails Engine for generator registration, and removing generator eager-loading during host app boot.

Changes:

  • Renames the gem from rolemodel_rails to rolemodel-rails and collapses the public namespace to Rolemodel.
  • Adds a Rolemodel::Engine to register generator support without eager-loading all generators at boot.
  • Updates specs, example apps, and tooling scripts to use the new gem name and loading behavior.

Reviewed changes

Copilot reviewed 16 out of 24 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
spec/support/helpers/example_app.rb Updates Gemfile cleanup to remove rolemodel-rails entries in the test app.
spec/spec_helper.rb Updates spec bootstrapping to load Rails + the gem and eagerly require generator classes for tests.
spec/rolemodel_rails_spec.rb Updates version constant expectations to the new Rolemodel namespace.
spec/generators/rolemodel/github_generator_spec.rb Updates expected template text to reference the new version file path.
rolemodel-rails.gemspec Renames the gem, updates metadata, and points version loading at Rolemodel::VERSION.
lib/rolemodel_rails.rb Removes the old entrypoint that eagerly loaded generators.
lib/rolemodel/version.rb Moves version constant under Rolemodel module.
lib/rolemodel/engine.rb Adds Rails Engine for generator registration.
lib/rolemodel-rails.rb Adds new gem entrypoint that defines version constants and requires version/engine.
lib/generators/rolemodel/base_generator.rb Introduces a standalone BaseGenerator (previously defined in lib/generators/rolemodel.rb).
lib/generators/rolemodel.rb Removes the old generator aggregator that loaded all generators eagerly.
example_rails8/Gemfile / Gemfile.lock Updates example app to depend on rolemodel-rails.
example_rails7/Gemfile / Gemfile.lock Updates example app to depend on rolemodel-rails.
bin/recreate_legacy_example Updates bundle add invocation to rolemodel-rails.
bin/recreate_current_example Updates bundle add invocation to rolemodel-rails.
bin/console Updates require to rolemodel-rails.
bin/bump_version Updates paths and gemspec name used by the version bumper.
README.md Updates documentation to reference rolemodel-rails.
Gemfile / Gemfile.lock Updates development dependency name to rolemodel-rails.
.gitignore Adds .DS_Store ignore.
.github/workflows/ci.yml Updates runner and GitHub Action versions used in CI.
Comments suppressed due to low confidence (1)

rolemodel-rails.gemspec:5

  • rolemodel-rails.gemspec no longer has # frozen_string_literal: true at the top. This repo consistently uses frozen string literals in Ruby files, and removing it may introduce RuboCop/style inconsistencies.

Suggestion: add the frozen-string-literal magic comment back at the top of the gemspec (before any require).

end

require 'rolemodel/version'
require 'rolemodel/engine'
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib/rolemodel-rails.rb requires rolemodel/engine, and Rolemodel::Engine inherits from ::Rails::Engine. As written, requiring this gem outside a Rails-booted process (e.g., bin/console) can raise NameError: uninitialized constant Rails.

Suggestion: ensure the Rails engine base is loaded before defining/loading the engine (e.g., require rails/engine / rails before requiring rolemodel/engine, or guard the engine load until Rails is available).

Suggested change
require 'rolemodel/engine'
begin
require 'rails/engine'
rescue LoadError
end
require 'rolemodel/engine' if defined?(Rails::Engine)

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +7
module Rolemodel
NODE_VERSION = '24.12.0'
RUBY_VERSION = '4.0.1'
end

require 'rolemodel/version'
require 'rolemodel/engine'
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new file is missing # frozen_string_literal: true at the top, which is used throughout the repo for Ruby files.

Suggestion: add the frozen-string-literal magic comment as the first line of the file.

Copilot uses AI. Check for mistakes.
Comment on lines +1 to +3
require 'rails/generators/base'
require 'rails/generators/bundle_helper'
require_relative 'replace_content_helper'
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new generator base class file is missing # frozen_string_literal: true at the top. Most generator code in lib/generators/** uses frozen string literals, and omitting it can create style inconsistencies.

Suggestion: add the frozen-string-literal magic comment as the first line of this file.

Copilot uses AI. Check for mistakes.
Comment on lines 28 to +31
gemfile_path = File.join(destination_root, 'Gemfile')
gemfile = File.open(gemfile_path)

File.write(gemfile_path, gemfile.grep_v(/rolemodel_rails/).join)
File.write(gemfile_path, gemfile.grep_v(/rolemodel-rails/).join)
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gemfile = File.open(gemfile_path) leaves the file handle unclosed (it’s not opened with a block and isn’t explicitly closed). Over repeated generator specs this can leak descriptors and cause intermittent failures.

Suggestion: use File.read/File.foreach or open the file with a block so it’s closed deterministically.

Copilot uses AI. Check for mistakes.
Comment on lines +1 to 5
require 'rails'
require 'bundler/setup'
require 'generator_spec'
require 'rolemodel-rails'
require_relative 'support/helpers'
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spec/spec_helper.rb doesn’t have # frozen_string_literal: true at the top, while other spec/support files do. Since this file is being edited anyway, it’s a good place to align with the repo’s frozen-string-literal convention.

Suggestion: add the magic comment as the first line of this file.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants