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

Extract cell_selector method in scaffold index view spec generator #2777

Merged
merged 2 commits into from
Aug 10, 2024

Conversation

mjankowski
Copy link
Contributor

Basically same change as described in #2619 - re-attempting only because of last comment - #2619 (comment) - which seemed to indicate a desire to get the logic into the generator and out of the generated spec file.

I read the linked discussion and it wasn't totally clear to me which portions involved people talking slightly past each other (about CI vs developer env) and which were genuine issues (that I may have not resolved here, if they exist?) so apologies if that's the case, and I can close and/or attempt to resolve. From my current understanding this does seem safe -- in CI environments or dev environments for working on rspec-rails itself, you'd presumably either have a fresh/clean run and/or clobber first and it should be ok; and for local rails-app development, if you had a prior-version generated spec+view they'd work in conjunction with each other, and this would continue to be true if you switched versions and started generating new ones, I think?

I agree with one of the comments in the linked discussion about how you would likely change this value pretty early on, but also that having the rails version conditional in the generated spec is slightly confusing when you first see it.

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

There’s a lengthy discussion here about this. I support the change.

@@ -18,9 +18,12 @@

it "renders a list of <%= ns_table_name %>" do
render
cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td'
<% for attribute in output_attributes -%>
assert_select cell_selector, text: Regexp.new(<%= value_for(attribute) %>.to_s), count: 2
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I don't see why this needs to be a method, I would just do:

Suggested change
assert_select cell_selector, text: Regexp.new(<%= value_for(attribute) %>.to_s), count: 2
assert_select <%= Rails::VERSION::STRING >= '7' ? "'div>p'" : "'tr>td'" %>, text: Regexp.new(<%= value_for(attribute) %>.to_s), count: 2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I may have misinterpreted your comment in the linked issue proposing we add a method (I assumed you meant in the generated view spec ... maybe you meant an internal method in the scaffold class that holds this logic and generates the string into the view?)

In either case, there are some other specs which reference cell_selector directly ... they keep passing if we turn the local var into a method in the generated view spec, but they'd fail if we change to direct string output here (could be solved by also having this conditional logic inline in the scaffold spec, and/or adding a method like this with duplicated logic to the spec).

I chose the smallest-diff path, but could do another pass to keep it as just a generated string in the view spec output, with conditional logic either in a shared method (template file and scaffold spec) or repeated in those spots.

Relatedly ... another thing which will wind up fixing this issue is whenever Rails 6.1 is EOL, there will presumably be a sweep/cleanup rspec-rails to remove a bunch of this logic, drop old ruby/rails support, etc?

Copy link
Member

Choose a reason for hiding this comment

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

Do you mind this adjustment? It warrants an insta-merge.
Another alternative that's a minimal diff and an insta-merge would be:

- cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td'
+ cell_selector = <%= Rails::VERSION::STRING >= '7' ? "'div>p'" : "'tr>td'" %>

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 use that latter approach (leave cell_selector as local var) to reduce the number of other specs that need changing.

Copy link
Member

Choose a reason for hiding this comment

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

For clarity / future travellers, I originally meant a method in the generator not the spec, but this is fine as is 👍

@mjankowski mjankowski force-pushed the cell-selector-generate-values branch from 5ce7e31 to 84d504e Compare August 10, 2024 19:29
pirj added a commit to mjankowski/rspec-rails that referenced this pull request Aug 10, 2024
@pirj pirj force-pushed the cell-selector-generate-values branch from f478710 to f785f7a Compare August 10, 2024 21:45
@pirj pirj merged commit 4aad064 into rspec:main Aug 10, 2024
21 checks passed
@pirj
Copy link
Member

pirj commented Aug 10, 2024

Thank you, @mjankowski !

JonRowe pushed a commit that referenced this pull request Aug 15, 2024
…2777)

Move conditional `cell_selector` out of generated view spec
@JonRowe
Copy link
Member

JonRowe commented Aug 20, 2024

Released in 6.1.4

@mjankowski mjankowski deleted the cell-selector-generate-values branch September 2, 2024 19:58
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.

3 participants