Skip to content

Delegate properly to custom Resolver instances #1580

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 3 commits into from
Mar 28, 2016

Conversation

bquorning
Copy link
Contributor

Let EmptyTemplateResolver create instances of either EmptyTemplateResolver::ResolverDecorator or EmptyTemplateResolver::FileSystemResolver, depending on whether the given path argument is an ActionView::Resolver instance or not.

The new EmptyTemplateResolver::ResolverDecorator class simply delegates all method calls to its given resolver, except for #find_all and #find_all_anywhere which it overrides to return templates that render with EmptyTemplateHandler.

Supersedes #1568 and hopefully also #1557.

@bquorning
Copy link
Contributor Author

Example of code that this PR will fix: https://gist.github.com/bquorning/6bf534e4b145b1738bb2. It’s a bit contrived, since I didn’t want to create tables and fetch custom templates from the database.

@bquorning
Copy link
Contributor Author

cc @JonRowe and @pixeltrix

@JonRowe
Copy link
Member

JonRowe commented Mar 22, 2016

Needs specs demonstrating what this actually solves vs #1557, in fact ideally we need smoke specs for both "rendering" views and "not rendering" views via this code path.

@bquorning
Copy link
Contributor Author

See my comment #1557 (comment) for why #1557 doesn’t work.

I agree that it would be nice to have specs for this (as we discussed in #1551), but they are pretty hard to write. Any suggestions are welcome.

@JonRowe
Copy link
Member

JonRowe commented Mar 22, 2016

Any suggestions are welcome.

Take the scenario you posted that supposedly doesn't work and put it into one of the smoke test apps.

@bquorning
Copy link
Contributor Author

I added a smoke spec that passes for this branch, and fails on both master and #1557. Suggestions for cleaning up the spec are much welcome.

@bquorning bquorning force-pushed the empty-template-resolver-builder-2 branch 2 times, most recently from 73ffdd0 to 82de536 Compare March 24, 2016 14:03
@bquorning bquorning closed this Mar 24, 2016
@bquorning bquorning reopened this Mar 24, 2016
# Version 3 of mime-types 3 requires Ruby 2.0
if RUBY_VERSION < '2.0.0'
gem 'mime-types', '< 3'
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

https://travis-ci.org/rspec/rspec-rails/builds/118239747 failed to bundle because v2.6.4 of mail is out, which allows bundling with mime-types < 4 instead of, as earlier, mime-types < 3.

@bquorning bquorning force-pushed the empty-template-resolver-builder-2 branch 4 times, most recently from 7f05849 to 72a5331 Compare March 24, 2016 21:22
@bquorning
Copy link
Contributor Author

@JonRowe, @pixeltrix This PR has a green build and is ready for review.

get :index

expect(response).to render_template(:baz)
end
Copy link
Member

Choose a reason for hiding this comment

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

Great start, we just need to check that the views are empty (as render_views hasn't been used) and ideally have a second set of tests that checks that the views can be rendered when specified. Appreciate your work to get this towards the line!

@bquorning
Copy link
Contributor Author

@JonRowe You were right, there was a bug in the view rendering code (inside ResolverDecorator, see the last commit).

How do the added specs look?

(I’ll need to squash the commits before the PR is mergeable)

@bquorning bquorning force-pushed the empty-template-resolver-builder-2 branch from ca741db to c4e02b8 Compare March 25, 2016 19:59
Let `EmptyTemplateResolver` creating instances of either
`EmptyTemplateResolver::ResolverDecorator` or
`EmptyTemplateResolver::FileSystemResolver`, depending on whether the given path
is an `ActionView::Resolver` instance or not.

The new `EmptyTemplateResolver::ResolverDecorator` class simply delegates all
method calls to its given resolver, checks the return values, and changes
collections of `ActionView::Template` instances into templates that render with
`EmptyTemplateHandler`.

The alternative was to implement both `#find_all` and `#find_all_anywhere`. The
latter is only present since Rails 4.2.5.1, and the former has different arity
in different Rails versions.
@bquorning bquorning force-pushed the empty-template-resolver-builder-2 branch from 27174d2 to c4bc440 Compare March 26, 2016 20:59

def method_missing(name, *args, &block)
result = @resolver.send(name, *args, &block)
nullify_templates(result)
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 realize that inspecting return values’ type isn't particularly good design. The alternative is to override #find_all and #find_all_anywhere depending on Rails version – as far as I recall #find_all changed arity from 5 to 6 at some point.

@bquorning
Copy link
Contributor Author

@JonRowe We have specs, a green build and squashed commits. Wanna review?

@JonRowe JonRowe merged commit 5094a47 into rspec:master Mar 28, 2016
@JonRowe
Copy link
Member

JonRowe commented Mar 28, 2016

👍 Thanks

JonRowe added a commit that referenced this pull request Mar 28, 2016
@bquorning bquorning deleted the empty-template-resolver-builder-2 branch March 28, 2016 06:54
sebjacobs pushed a commit to futurelearn/rspec-rails that referenced this pull request Mar 15, 2019
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.

2 participants