Skip to content

Commit

Permalink
Merge pull request #45315 from gmcgibbon/hook_generator_description
Browse files Browse the repository at this point in the history
Delegate model generator description to orm hooked generator
  • Loading branch information
gmcgibbon committed Jun 13, 2022
2 parents 3493407 + fff53e4 commit a39cd44
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 1 deletion.
4 changes: 4 additions & 0 deletions railties/CHANGELOG.md
@@ -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
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
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
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
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
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

0 comments on commit a39cd44

Please sign in to comment.