Skip to content

Conversation

@reeganviljoen
Copy link
Collaborator

closes #1764

What are you trying to accomplish?

Add Rails Url helpers to previews, for instance, if resources :items is defined in routes.rb, I may want to use items_path in the component preview method

What approach did you choose and why?

I added include Rails.application.routes.url_helpers to preview.rb to add the helpers to the previews class

A similar way to achieve this is by using the preview template which will have the URL helpers loaded but that seems like overkill just to get access to a helper

Anything you want to highlight for special attention from reviewers?

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.

👏🏻


*Pasha Kalashnikov*

* Add URL helpers to previews
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
* Add URL helpers to previews
* Add support for URL helpers to previews.

@joelhawksley joelhawksley merged commit 6a3b591 into ViewComponent:main Jun 6, 2023
@joelhawksley
Copy link
Member

Brilliant! Thanks for the clean PR ❤️

@peteryates
Copy link
Contributor

peteryates commented Jun 29, 2023

This change is causing me a bit of a headache when trying to use ViewComponent outside of Rails, specifically for building the guide for GOV.UK Components.

See this PR.

@Spone
Copy link
Collaborator

Spone commented Jun 29, 2023

I think we can rework this to only include the helpers if Rails is defined. Can you have a look @reeganviljoen?

peteryates added a commit to x-govuk/govuk-components that referenced this pull request Jun 29, 2023
A recent change to ViewComponent[0] makes it call
Rails.application.routes.url_helpers which, until now, didn't exist.

We can initialize a fake Rails application so it's at least present,
despite being totally empty, which will allow the guide build to
function. We're very much in the minority in terms of how we're building
this guide Rails-free, so it's on us to make it work.

[0] ViewComponent/view_component#1765
peteryates added a commit to x-govuk/govuk-components that referenced this pull request Jun 29, 2023
A recent change to ViewComponent[0] makes it call
Rails.application.routes.url_helpers which, until now, didn't exist.

We can initialize a fake Rails application so it's at least present,
despite being totally empty, which will allow the guide build to
function. We're very much in the minority in terms of how we're building
this guide Rails-free, so it's on us to make it work.

[0] ViewComponent/view_component#1765
@peteryates
Copy link
Contributor

Actually the fix isn't too bad - less involved than I thought half an hour ago! I totally realise we're using ViewComponent in a really unintended way to build the our guide so am happy to work around this on our end.

Simply initialising a fake Rails app before kicking off the Nanoc build works for us.

Consider this not a problem, thank you @Spone and @reeganviljoen :)

@rmacklin
Copy link
Collaborator

rmacklin commented Jul 7, 2023

This seems to have broken the docs:build rake task:

$ bundle exec rake docs:build
Building YARD documentation.
[warn]: in YARD::Handlers::Ruby::MixinHandler: Undocumentable mixin: YARD::Parser::UndocumentableError for class ViewComponent::Preview
	in file 'lib/view_component/preview.rb':7:

	7: include Rails.application.routes.url_helpers

Files:          45
Modules:        42 (   33 undocumented)
Classes:        55 (   51 undocumented)
Constants:      39 (   38 undocumented)
Attributes:     31 (    2 undocumented)
Methods:       171 (   81 undocumented)
 39.35% documented
Converting YARD documentation to Markdown files.
rake aborted!
NoMethodError: undefined method `routes' for nil:NilClass
/Users/rmacklin/src/github.com/ViewComponent/view_component/lib/view_component/preview.rb:7:in `<class:Preview>'
/Users/rmacklin/src/github.com/ViewComponent/view_component/lib/view_component/preview.rb:6:in `<module:ViewComponent>'
/Users/rmacklin/src/github.com/ViewComponent/view_component/lib/view_component/preview.rb:5:in `<top (required)>'
/Users/rmacklin/src/github.com/ViewComponent/view_component/lib/view_component/base.rb:11:in `require'
/Users/rmacklin/src/github.com/ViewComponent/view_component/lib/view_component/base.rb:11:in `<top (required)>'
/Users/rmacklin/src/github.com/ViewComponent/view_component/Rakefile:81:in `require'
/Users/rmacklin/src/github.com/ViewComponent/view_component/Rakefile:81:in `block (2 levels) in <top (required)>'
/Users/rmacklin/.asdf/installs/ruby/2.7.5/bin/bundle:23:in `load'
/Users/rmacklin/.asdf/installs/ruby/2.7.5/bin/bundle:23:in `<main>'
Tasks: TOP => docs:build
(See full trace by running task with --trace)

I had to comment out the include Rails.application.routes.url_helpers line from preview.rb in order to successfully run the rake task.

We could change the task to work around this, but I think @Spone's suggested approach of only including the helpers when inside a Rails application makes sense.

@rmacklin
Copy link
Collaborator

rmacklin commented Jul 7, 2023

I've opened a PR to do so here: #1795

claudiob pushed a commit to claudiob/view_component that referenced this pull request Dec 22, 2023
* added url helpers to previews

* updated changelog

* fixed lint errors

* fixed lint errors

* fixed lint errors

* fixed lint errors
claudiob pushed a commit to claudiob/view_component that referenced this pull request Jan 3, 2024
* added url helpers to previews

* updated changelog

* fixed lint errors

* fixed lint errors

* fixed lint errors

* fixed lint errors
@reeganviljoen reeganviljoen deleted the rv_add_url_helpers_to_previews branch May 22, 2024 06:15
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.

Add Rails url helpers to Previews

5 participants