From c4927b73f23fc33e8a7118685016a67d5c7219a5 Mon Sep 17 00:00:00 2001 From: Derek Prior Date: Fri, 10 May 2013 14:10:46 -0400 Subject: [PATCH] Fix render_with_layout_matcher on Rails 4. Rails 4 moved the layouts ivar from `@layouts` to `@_layouts`. This change introduces what I have (for now) called the 'RailsShim'. The idea is that this single class will house the rail version workarounds, which can then be reused through the code. This paid off here because both the spec and the implementation need access to the layouts ivar. --- lib/shoulda/matchers.rb | 1 + .../render_with_layout_matcher.rb | 2 +- lib/shoulda/matchers/rails_shim.rb | 20 +++++++++++++++++++ .../render_with_layout_matcher_spec.rb | 9 +++++++-- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 lib/shoulda/matchers/rails_shim.rb diff --git a/lib/shoulda/matchers.rb b/lib/shoulda/matchers.rb index 95074008..24ca8dc3 100644 --- a/lib/shoulda/matchers.rb +++ b/lib/shoulda/matchers.rb @@ -1,5 +1,6 @@ require 'shoulda/matchers/version' require 'shoulda/matchers/assertion_error' +require 'shoulda/matchers/rails_shim' if defined?(RSpec) require 'shoulda/matchers/integrations/rspec' diff --git a/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb b/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb index 2ddae0de..5f4a84e1 100644 --- a/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +++ b/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb @@ -71,7 +71,7 @@ module Shoulda # :nodoc: def recorded_layouts if @context - @context.instance_variable_get('@layouts') + @context.instance_variable_get(Shoulda::Matchers::RailsShim.layouts_ivar) else {} end diff --git a/lib/shoulda/matchers/rails_shim.rb b/lib/shoulda/matchers/rails_shim.rb new file mode 100644 index 00000000..1130ddf4 --- /dev/null +++ b/lib/shoulda/matchers/rails_shim.rb @@ -0,0 +1,20 @@ +module Shoulda # :nodoc: + module Matchers + class RailsShim # :nodoc: + + def self.layouts_ivar + if rails_major_version >= 4 + '@_layouts' + else + '@layouts' + end + end + + private + + def self.rails_major_version + Rails::VERSION::MAJOR + end + end + end +end diff --git a/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb b/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb index d65af2c7..6b4a580c 100644 --- a/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb +++ b/spec/shoulda/matchers/action_controller/render_with_layout_matcher_spec.rb @@ -47,8 +47,13 @@ describe Shoulda::Matchers::ActionController::RenderWithLayoutMatcher do end def set_in_context_layout(layout) - @layouts = Hash.new(0) - @layouts[layout] = 1 + layouts = Hash.new(0) + layouts[layout] = 1 + self.instance_variable_set(layouts_ivar, layouts) + end + + def layouts_ivar + Shoulda::Matchers::RailsShim.layouts_ivar end def controller_without_layout