Adds option to change components path (#961)

This commit is contained in:
lfalcao 2021-06-21 17:53:54 -03:00 коммит произвёл GitHub
Родитель 0a3fe2347d
Коммит eaf7ef0cf8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 78 добавлений и 49 удалений

Просмотреть файл

@ -7,6 +7,10 @@ title: Changelog
## main
* Adds support to change default components path (app/components) with `config.view_component.view_component_path`.
*lfalcao*
* Rename private instance variables (such as @variant) to reduce potential conflicts with subclasses.
*Joel Hawksley*

Просмотреть файл

@ -22,6 +22,8 @@ bin/rails generate component Example title content
## Options
Generated ViewComponents are added to `app/components` by default. Set `config.view_component.view_component_path` to use a different path.
### Override template engine
ViewComponent includes template generators for the `erb`, `haml`, and `slim` template engines and will default to the template engine specified in `config.generators.template_engine`.

Просмотреть файл

@ -0,0 +1,29 @@
# frozen_string_literal: true
module ViewComponent
module AbstractGenerator
def copy_view_file
unless options["inline"]
template "component.html.#{engine_name}", destination
end
end
private
def destination
if options["sidecar"]
File.join(component_path, class_path, "#{file_name}_component", "#{file_name}_component.html.#{engine_name}")
else
File.join(component_path, class_path, "#{file_name}_component.html.#{engine_name}")
end
end
def file_name
@_file_name ||= super.sub(/_component\z/i, "")
end
def component_path
Rails.application.config.view_component.view_component_path || "app/components"
end
end
end

Просмотреть файл

@ -10,7 +10,7 @@ module Rails
class_option :inline, type: :boolean, default: false
def create_component_file
template "component.rb", File.join("app/components", class_path, "#{file_name}_component.rb")
template "component.rb", File.join(component_path, class_path, "#{file_name}_component.rb")
end
hook_for :test_framework
@ -44,6 +44,10 @@ module Rails
def initialize_call_method_for_inline?
options["inline"]
end
def component_path
Rails.application.config.view_component.view_component_path || "app/components"
end
end
end
end

Просмотреть файл

@ -1,33 +1,25 @@
# frozen_string_literal: true
require "rails/generators/erb"
require "rails/generators/abstract_generator"
module Erb
module Generators
class ComponentGenerator < Base
include ViewComponent::AbstractGenerator
source_root File.expand_path("templates", __dir__)
class_option :sidecar, type: :boolean, default: false
class_option :inline, type: :boolean, default: false
def engine_name
"erb"
end
def copy_view_file
unless options["inline"]
template "component.html.erb", destination
end
super
end
private
def destination
if options["sidecar"]
File.join("app/components", class_path, "#{file_name}_component", "#{file_name}_component.html.erb")
else
File.join("app/components", class_path, "#{file_name}_component.html.erb")
end
end
def file_name
@_file_name ||= super.sub(/_component\z/i, "")
end
end
end
end

Просмотреть файл

@ -5,28 +5,19 @@ require "rails/generators/erb/component_generator"
module Haml
module Generators
class ComponentGenerator < Erb::Generators::ComponentGenerator
include ViewComponent::AbstractGenerator
source_root File.expand_path("templates", __dir__)
class_option :sidecar, type: :boolean, default: false
def engine_name
"haml"
end
def copy_view_file
if !options["inline"]
template "component.html.haml", destination
end
super
end
private
def destination
if options["sidecar"]
File.join("app/components", class_path, "#{file_name}_component", "#{file_name}_component.html.haml")
else
File.join("app/components", class_path, "#{file_name}_component.html.haml")
end
end
def file_name
@_file_name ||= super.sub(/_component\z/i, "")
end
end
end
end

Просмотреть файл

@ -5,28 +5,19 @@ require "rails/generators/erb/component_generator"
module Slim
module Generators
class ComponentGenerator < Erb::Generators::ComponentGenerator
include ViewComponent::AbstractGenerator
source_root File.expand_path("templates", __dir__)
class_option :sidecar, type: :boolean, default: false
def engine_name
"slim"
end
def copy_view_file
if !options["inline"]
template "component.html.slim", destination
end
super
end
private
def destination
if options["sidecar"]
File.join("app/components", class_path, "#{file_name}_component", "#{file_name}_component.html.slim")
else
File.join("app/components", class_path, "#{file_name}_component.html.slim")
end
end
def file_name
@_file_name ||= super.sub(/_component\z/i, "")
end
end
end
end

Просмотреть файл

@ -126,4 +126,13 @@ class ComponentGeneratorTest < Rails::Generators::TestCase
assert_file "app/components/user_component.html.haml"
end
def test_generating_components_with_custom_component_path
with_custom_component_path("app/parts") do
run_generator %w[user]
assert_file "app/parts/user_component.rb"
assert_file "app/parts/user_component.html.erb"
end
end
end

Просмотреть файл

@ -51,6 +51,13 @@ def with_preview_controller(new_value)
app.reloader.reload!
end
def with_custom_component_path(new_value)
old_value = Rails.application.config.view_component.view_component_path
Rails.application.config.view_component.view_component_path = new_value
yield
Rails.application.config.view_component.view_component_path = old_value
end
def with_new_cache
begin
old_cache = ViewComponent::CompileCache.cache