1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Delegate model generator description to orm hooked generator

Use orm hooked generator description for model generator help.
This commit is contained in:
Gannon McGibbon 2022-06-09 00:54:32 -05:00
parent 7c1165c8c8
commit fff53e4455
7 changed files with 63 additions and 1 deletions

View file

@ -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.

View file

@ -189,6 +189,13 @@ module Rails
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

View file

@ -9,6 +9,10 @@ module Rails
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

View file

@ -16,6 +16,12 @@ module Rails
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

View file

@ -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
@ -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
@ -38,3 +39,38 @@ class OrmTest < Rails::Generators::TestCase
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

View file

@ -13,6 +13,11 @@ class ResourceGeneratorTest < Rails::Generators::TestCase
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)