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

Fix empty template rendering for Rails 4.2.5.1 #1535

Merged
merged 1 commit into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example_app_generator/generate_stuff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def using_source_path(path)
"This is a template for a custom action.",
:force => true

file "app/views/errors/401.html.erb",
"This is a template for rendering an error page",
:force => true

# Use the absolute path so we can load it without active record too
apply File.join(DEFAULT_SOURCE_PATH, 'generate_action_mailer_specs.rb')
using_source_path(File.expand_path('..', __FILE__)) do
Expand Down
72 changes: 71 additions & 1 deletion features/controller_specs/anonymous_controller.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Feature: anonymous controller
end
```

Scenario: Specify error handling in `ApplicationController`
Scenario: Specify error handling in `ApplicationController` with redirect
Given a file named "spec/controllers/application_controller_spec.rb" with:
"""ruby
require "rails_helper"
Expand Down Expand Up @@ -66,6 +66,76 @@ Feature: anonymous controller
When I run `rspec spec`
Then the examples should all pass

Scenario: Specify error handling in `ApplicationController` with render
Given a file named "spec/controllers/application_controller_spec.rb" with:
"""ruby
require "rails_helper"

class ApplicationController < ActionController::Base
class AccessDenied < StandardError; end

rescue_from AccessDenied, :with => :access_denied

private

def access_denied
render "errors/401"
end
end

RSpec.describe ApplicationController, :type => :controller do
controller do
def index
raise ApplicationController::AccessDenied
end
end

describe "handling AccessDenied exceptions" do
it "renders the errors/401 template" do
get :index
expect(response).to render_template("errors/401")
end
end
end
"""
When I run `rspec spec`
Then the examples should all pass

Scenario: Specify error handling in `ApplicationController` with render :file
Given a file named "spec/controllers/application_controller_spec.rb" with:
"""ruby
require "rails_helper"

class ApplicationController < ActionController::Base
class AccessDenied < StandardError; end

rescue_from AccessDenied, :with => :access_denied

private

def access_denied
render :file => "errors/401"
end
end

RSpec.describe ApplicationController, :type => :controller do
controller do
def index
raise ApplicationController::AccessDenied
end
end

describe "handling AccessDenied exceptions" do
it "renders the errors/401 template" do
get :index
expect(response).to render_template("errors/401")
end
end
end
"""
When I run `rspec spec`
Then the examples should all pass

Scenario: Specify error handling in a subclass
Given a file named "spec/controllers/application_controller_subclass_spec.rb" with:
"""ruby
Expand Down
27 changes: 13 additions & 14 deletions lib/rspec/rails/view_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,11 @@ def render_views?
# with modified source
#
# @private
class EmptyTemplatePathSetDecorator < ::ActionView::Resolver
attr_reader :original_path_set

def initialize(original_path_set)
@original_path_set = original_path_set
end
class EmptyTemplateResolver < ::ActionView::FileSystemResolver
private

def find_all(*args)
original_path_set.find_all(*args).map do |template|
def find_templates(*args)
super.map do |template|
::ActionView::Template.new(
"",
template.identifier,
Expand Down Expand Up @@ -85,23 +81,26 @@ def append_view_path(new_path)
private

def _path_decorator(path)
EmptyTemplatePathSetDecorator.new(ActionView::PathSet.new(Array.wrap(path)))
EmptyTemplateResolver.new(path)
end
end

included do
before do
unless render_views?
@_empty_view_path_set_delegator = EmptyTemplatePathSetDecorator.new(controller.class.view_paths)
controller.class.view_paths = ::ActionView::PathSet.new.push(@_empty_view_path_set_delegator)
@_original_path_set = controller.class.view_paths

empty_template_path_set = @_original_path_set.map do |resolver|
EmptyTemplateResolver.new(resolver.to_s)
end

controller.class.view_paths = empty_template_path_set
controller.extend(EmptyTemplates)
end
end

after do
unless render_views?
controller.class.view_paths = @_empty_view_path_set_delegator.original_path_set
end
controller.class.view_paths = @_original_path_set unless render_views?
end
end
end
Expand Down