Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delegate model generator description to orm hooked generator #45315

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Delegate model generator description to orm hooked generator.

*Gannon McGibbon*

* Execute `rails runner` scripts inside the executor.

Enables error reporting, query cache, etc.
Expand Down
7 changes: 7 additions & 0 deletions railties/lib/rails/generators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,13 @@ def self.hook_for(*names, &block)
class_option(name, defaults.merge!(options))
end

klass = self

singleton_class.define_method("#{name}_generator") do
value = class_options[name].default
Rails::Generators.find_by_namespace(klass.generator_name, value)
end

hooks[name] = [ in_base, as_hook ]
invoke_from_option(name, options, &block)
end
Expand Down
4 changes: 4 additions & 0 deletions railties/lib/rails/generators/rails/model/model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class ModelGenerator < NamedBase # :nodoc:

argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
hook_for :orm, required: true, desc: "ORM to be invoked"

class << self
delegate(:desc, to: :orm_generator)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class ResourceGenerator < ModelGenerator # :nodoc:
desc: "Actions for the resource controller"

hook_for :resource_route, required: true

class << self
def desc(description = nil)
ERB.new(File.read(usage_path)).result(binding)
end
end
end
end
end
38 changes: 37 additions & 1 deletion railties/test/generators/orm_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "generators/generators_test_helper"
require "rails/generators/rails/scaffold_controller/scaffold_controller_generator"
require "rails/generators/rails/model/model_generator"

# Mock out two ORMs
module ORMWithGenerators
Expand All @@ -17,7 +18,7 @@ module ORMWithoutGenerators
# No generators
end

class OrmTest < Rails::Generators::TestCase
class ScaffoldOrmTest < Rails::Generators::TestCase
include GeneratorsTestHelper
tests Rails::Generators::ScaffoldControllerGenerator

Expand All @@ -38,3 +39,38 @@ def test_orm_instance_returns_orm_class_instance_with_name
assert_equal "foo", orm_instance.name
end
end

module TestOrmWithDescription
module Generators
class ModelGenerator < Rails::Generators::ModelGenerator
def self.usage_path
true
end

def self.desc
"My description"
end
end
end
end

class ModelOrmTest < ActiveSupport::TestCase
def test_active_record_description_is_used_by_default
assert_match(/Description:.*/, Rails::Generators::ModelGenerator.desc)
end

def test_orm_description_is_used_when_configured
with_configured(:orm, :test_orm_with_description) do
assert_equal("My description", Rails::Generators::ModelGenerator.desc)
end
end

private
def with_configured(hook, value)
previous_default = Rails::Generators::ModelGenerator.class_options[hook].default
Rails::Generators::ModelGenerator.class_options[hook].instance_variable_set(:@default, value)
yield
ensure
Rails::Generators::ModelGenerator.class_options[hook].instance_variable_set(:@default, previous_default)
end
end
5 changes: 5 additions & 0 deletions railties/test/generators/resource_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def setup
Rails::Generators::ModelHelpers.skip_warn = false
end

def test_help_with_usage_description
content = run_generator ["--help"]
assert_match(/Generates a new resource/, content)
end

def test_help_with_inherited_options
content = run_generator ["--help"]
assert_match(/ActiveRecord options:/, content)
Expand Down