Skip to content

Add class_path to allow generated components to reside in subdirectories #890

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

Merged
merged 2 commits into from
Apr 16, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ $ rails g react:component HelloWorld greeting:string

Your component is added to `app/javascript/components/` by default.

You can also generate your component in a subdirectory:

```
$ rails g react:component my_subdirectory/HelloWorld greeting:string
```

[Render it in a Rails view](#view-helper):

```erb
Expand Down Expand Up @@ -135,7 +141,7 @@ $ bundle exec rails webpacker:install:typescript
$ yarn add @types/react @types/react-dom
```

Doing this will allow React-Rails to support the .tsx extension.
Doing this will allow React-Rails to support the .tsx extension.

## Use with Asset Pipeline

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/react/component_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def create_component_file
target_dir = 'app/assets/javascripts/components'
end

file_path = File.join(target_dir, "#{new_file_name}.#{extension}")
file_path = File.join(target_dir, class_path, "#{new_file_name}.#{extension}")
template("component.#{template_extension}", file_path)
end

Expand Down
17 changes: 17 additions & 0 deletions test/generators/component_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ class ComponentGeneratorTest < Rails::Generators::TestCase
def filename
'app/javascript/components/GeneratedComponent.js'
end
def filename_with_subfolder
'app/javascript/components/generated_folder/GeneratedComponent.js'
end
else
def filename
'app/assets/javascripts/components/generated_component.js.jsx'
end
def filename_with_subfolder
'app/assets/javascripts/components/generated_folder/generated_component.js.jsx'
end
end

test 'creates the component file' do
Expand All @@ -27,6 +33,17 @@ def filename
end
end

test 'creates the component file in a subdirectory' do
puts WebpackerHelpers.available?
run_generator %w(generated_folder/GeneratedComponent)
assert_file filename_with_subfolder do |contents|
if WebpackerHelpers.available?
assert_match /^import React from "react"/, contents
assert_match /export default GeneratedComponent\n$/m, contents
end
end
end

test 'creates the component file with a node argument' do
run_generator %w(GeneratedComponent name)
assert_file filename, %r{name: PropTypes.node}
Expand Down