Skip to content
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

Add support for using view components in rails engine #1951

Conversation

tkowalewski
Copy link
Contributor

@tkowalewski tkowalewski commented Jan 3, 2024

What are you trying to accomplish?

I'm trying to add support for generating view components in the module (engine) namespace in rails engine.
To do it we need to wrap templates code with module_namespacing (provided by rails)

How to test

  1. Create new rails engine
rails plugin new engine_with_test --mountable
  1. Update gemspec (engine_with_test.gemspec)
    Update spec.homepage
    Update spec.summary
    Update spec.description
    Update spec.metadata["source_code_uri"]
    Update spec.metadata["changelog_uri"]

  2. Add view_component to Gemfile

gem "view_component", github: "tkowalewski/view_component", branch: "component_template_with_module_namespacing"
  1. Install gems
bundle install
  1. Generate example component
bin/rails generate component Example message --preview
  1. Update component template (app/components/engine_with_test/example_component.html.erb)
<span><%= @message %></span>
  1. Uncomment test (test/components/engine_with_test/example_component_test.rb)

  2. Run tests

bin/rails test
  1. Preview component
bin/rails server

and go to http://localhost:3000/rails/view_components

What approach did you choose and why?

There is only one solution to deliver view components for rails engine with module (engine) namespace: Wrap templates code with module_namespacing :)

Anything you want to highlight for special attention from reviewers?

  • I had to prepare example rails engine for tests (test/test_engine)
  • New rake test task (engine_test) was added to Rakefile
  • ViewComponent configuration has been extended to provide preview paths for rails engine

@tkowalewski tkowalewski mentioned this pull request Jan 3, 2024
@boardfish boardfish linked an issue Jan 4, 2024 that may be closed by this pull request
@boardfish
Copy link
Collaborator

👋 Just a quick nudge to say #1308 means you'll need to rebase this branch for checks to pass.

@tkowalewski tkowalewski force-pushed the component_template_with_module_namespacing branch 2 times, most recently from 2c551bf to 3ba0a7c Compare January 10, 2024 17:43
@tkowalewski tkowalewski force-pushed the component_template_with_module_namespacing branch 2 times, most recently from 18c846c to 1910a72 Compare March 14, 2024 19:25
@tkowalewski tkowalewski force-pushed the component_template_with_module_namespacing branch from 1910a72 to 5c16496 Compare May 14, 2024 18:47
@tkowalewski tkowalewski changed the title WIP: Add support for using view components in rails engine Add support for using view components in rails engine May 17, 2024
@tkowalewski tkowalewski marked this pull request as ready for review May 17, 2024 09:23
@tkowalewski
Copy link
Contributor Author

@boardfish @Spone

There is problem with tests for main release of rails :|
Any plans to add support for rails 8.0.0.alpha?

@reeganviljoen
Copy link
Collaborator

@tkowalewski we have fixed the rails main issues, and added support for ruby head as a bonus aswell

@joelhawksley
Copy link
Member

@tkowalewski would you be up for fixing the failing tests on this PR? It seems like you're pretty close ❤️

@tkowalewski
Copy link
Contributor Author

@joelhawksley Ready :)

Rakefile Outdated
end

Rake::TestTask.new(:engine_test) do |t|
t.libs << "test/dummy"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.libs << "test/dummy"
t.libs << "test/engine"

👋🏻 I have a slight aversion to using the term dummy, might you be willing to change the naming to engine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, engine is something like keyword for rails I think.
If we want to rename test engine to engine - we will have weird naming I think. For eg. test engine will look like:

module Engine
  class Engine < ::Rails::Engine
    isolate_namespace Engine
  end
end

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test_engine?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to TestEngine

docs/CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Member

@joelhawksley joelhawksley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! ❤️

@joelhawksley joelhawksley merged commit 80c7c8e into ViewComponent:main Aug 27, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rails Engine support
4 participants