Skip to content

Commit

Permalink
Fix set_the_flash_matcher for Rails 4
Browse files Browse the repository at this point in the history
The "flashes" instance variable that lives within FlashHash has changed
in Rails 4. I have added a shim to get the proper name.

Additionally, the set_the_flash matcher will dup the flash hash for the
controller on which it operates. As #dup does a shallow copy we need to
make sure to also copy the instance variables within the flash hash when
we do this. The Rails 4 version of FlashHash has an extra @discard
variable unlike the Rails 3 version so we make sure to copy that.
  • Loading branch information
Derek Prior + Elliot Winkler authored and mcmire committed Aug 16, 2013
1 parent c4927b7 commit 9a333d1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
34 changes: 24 additions & 10 deletions lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb
Expand Up @@ -90,20 +90,34 @@ def flash_values
end

def flash
if @flash
@flash
else
@flash = @controller.flash.dup
used = @controller.flash.instance_variable_get(:@used).dup
@flash.instance_variable_set(:@used, used)
sweep_flash_if_necessary
@flash
@flash ||= copy_of_flash_from_controller
end

def copy_of_flash_from_controller
@controller.flash.dup.tap do |flash|
copy_flashes(@controller.flash, flash)
copy_discard_if_necessary(@controller.flash, flash)
sweep_flash_if_necessary(flash)
end
end

def copy_flashes(original_flash, new_flash)
flashes_ivar = Shoulda::Matchers::RailsShim.flashes_ivar
flashes = original_flash.instance_variable_get(flashes_ivar).dup
new_flash.instance_variable_set(flashes_ivar, flashes)
end

def copy_discard_if_necessary(original_flash, new_flash)
discard_ivar = :@discard
if original_flash.instance_variable_defined?(discard_ivar)
discard = original_flash.instance_variable_get(discard_ivar).dup
new_flash.instance_variable_set(discard_ivar, discard)
end
end

def sweep_flash_if_necessary
def sweep_flash_if_necessary(flash)
unless @options[:now]
@flash.sweep
flash.sweep
end
end

Expand Down
8 changes: 7 additions & 1 deletion lib/shoulda/matchers/rails_shim.rb
Expand Up @@ -10,7 +10,13 @@ def self.layouts_ivar
end
end

private
def self.flashes_ivar
if rails_major_version >= 4
:@flashes
else
:@used
end
end

def self.rails_major_version
Rails::VERSION::MAJOR
Expand Down

0 comments on commit 9a333d1

Please sign in to comment.