-
Notifications
You must be signed in to change notification settings - Fork 25
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
Prepare for ViewComponents #1749
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d1866dc
preparation for ViewComponents
nimmolo 914e51b
Update application_helper.rb
nimmolo 5194dfd
Whole bunch of rubocop
nimmolo 285805e
Update application.rb
nimmolo 8c9b273
Merge branch 'main' into nimmo-view-component
nimmolo 0097e34
Merge branch 'main' into nimmo-view-component
nimmolo f0bcf6a
Merge branch 'main' into nimmo-view-component
nimmolo 8aad4f8
Update Gemfile.lock
nimmolo 1eed66f
Merge branch 'main' into nimmo-view-component
nimmolo 1458edf
update viewcomponent
nimmolo fb0606a
Merge branch 'main' into nimmo-view-component
nimmolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationViewComponent < ViewComponentContrib::Base | ||
extend Dry::Initializer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationViewComponentPreview < ViewComponentContrib::Preview::Base | ||
self.abstract_class = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
ActiveSupport.on_load(:view_component) do | ||
# Extend your preview controller to support authentication and other | ||
# application-specific stuff | ||
# | ||
# Rails.application.config.to_prepare do | ||
# ViewComponentsController.class_eval do | ||
# include Authenticated | ||
# end | ||
# end | ||
# | ||
# Make it possible to store previews in sidecar folders | ||
# See https://github.com/palkan/view_component-contrib#organizing-components-or-sidecar-pattern-extended | ||
ViewComponent::Preview.extend(ViewComponentContrib::Preview::Sidecarable) | ||
# Enable `self.abstract_class = true` to exclude previews from the list | ||
ViewComponent::Preview.extend(ViewComponentContrib::Preview::Abstract) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Description: | ||
============ | ||
Creates a new view component, test and preview files. | ||
Pass the component name, either CamelCased or under_scored, and an optional list of attributes as arguments. | ||
|
||
Example: | ||
======== | ||
bin/rails generate view_component Profile name age | ||
|
||
creates a Profile component and test: | ||
Component: app/views/components/profile/component.rb | ||
Template: app/views/components/profile/component.html.erb | ||
Test: test/views/components/profile_component_test.rb | ||
System Test: test/system/views/components/profile_component_test.rb | ||
Preview: app/views/components/profile/component_preview.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div>Add <%= class_name %> template here</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class <%= class_name %>::Component < <%= parent_class %> | ||
with_collection_parameter :<%= singular_name %> | ||
<%- if initialize_signature -%> | ||
<%= initialize_signature %> | ||
<%- end -%> | ||
end |
13 changes: 13 additions & 0 deletions
13
lib/generators/view_component/templates/component_system_test.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
require "application_system_test_case" | ||
|
||
class <%= class_name %>::ComponentSystemTest < ApplicationSystemTestCase | ||
def test_default_preview | ||
visit("/rails/view_components<%= File.join(class_path, file_name) %>/default") | ||
|
||
# assert_text "Hello!" | ||
# click_on("Click me!") | ||
# assert_text "Good-bye!" | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
lib/generators/view_component/templates/component_test.rb.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class <%= class_name %>::ComponentTest < ViewComponent::TestCase | ||
def test_renders | ||
component = build_component | ||
|
||
render_inline(component) | ||
|
||
assert_selector "div" | ||
end | ||
|
||
private | ||
|
||
def build_component(**options) | ||
<%= class_name %>::Component.new(**options) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class <%= class_name %>::Preview < <%= preview_parent_class %> | ||
# You can specify the container class for the default template | ||
# self.container_class = "w-1/2 border border-gray-300" | ||
|
||
def default | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# frozen_string_literal: true | ||
|
||
# Based on https://github.com/github/view_component/blob/master/lib/rails/generators/component/component_generator.rb | ||
class ViewComponentGenerator < Rails::Generators::NamedBase | ||
source_root File.expand_path("templates", __dir__) | ||
|
||
class_option :skip_test, type: :boolean, default: false | ||
class_option :skip_system_test, type: :boolean, default: false | ||
class_option :skip_preview, type: :boolean, default: false | ||
|
||
argument :attributes, type: :array, default: [], banner: "attribute" | ||
|
||
def create_component_file | ||
template("component.rb", | ||
File.join("app/views/components", class_path, file_name, | ||
"component.rb")) | ||
end | ||
|
||
def create_template_file | ||
template("component.html.erb", | ||
File.join("app/views/components", class_path, file_name, | ||
"component.html.erb")) | ||
end | ||
|
||
def create_test_file | ||
return if options[:skip_test] | ||
|
||
template("component_test.rb", | ||
File.join("test/views/components", class_path, | ||
"#{file_name}_test.rb")) | ||
end | ||
|
||
def create_system_test_file | ||
return if options[:skip_system_test] | ||
|
||
template("component_system_test.rb", | ||
File.join("test/system/views/components", class_path, | ||
"#{file_name}_test.rb")) | ||
end | ||
|
||
def create_preview_file | ||
return if options[:skip_preview] | ||
|
||
template("preview.rb", | ||
File.join("app/views/components", class_path, file_name, | ||
"preview.rb")) | ||
end | ||
|
||
private | ||
|
||
def parent_class | ||
"ApplicationViewComponent" | ||
end | ||
|
||
def preview_parent_class | ||
"ApplicationViewComponentPreview" | ||
end | ||
|
||
def initialize_signature | ||
return if attributes.blank? | ||
|
||
attributes.map { |attr| "option :#{attr.name}" }.join("\n ") | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depending on how you and others feel about all-arguments forwarding,
the following should work here. Your choice: