-
-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consolidate set_session and set_flash APIs
* Refactor so they both use SetSessionOrFlashMatcher internally * Remove `set_session['key']` in favor of `set_session('key')` * `set_flash['key'].to(nil)` no longer works if set_flash has never been set
- Loading branch information
Showing
10 changed files
with
580 additions
and
651 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActionController | ||
# @private | ||
class FlashStore | ||
def self.future | ||
new | ||
end | ||
|
||
def self.now | ||
new.use_now! | ||
end | ||
|
||
attr_accessor :controller | ||
|
||
def initialize | ||
@use_now = false | ||
end | ||
|
||
def name | ||
if @use_now | ||
'flash.now' | ||
else | ||
'flash' | ||
end | ||
end | ||
|
||
def has_key?(key) | ||
values_to_check.include?(key) | ||
end | ||
|
||
def has_value?(expected_value) | ||
values_to_check.values.any? do |actual_value| | ||
expected_value === actual_value | ||
end | ||
end | ||
|
||
def empty? | ||
flash.empty? | ||
end | ||
|
||
def use_now! | ||
@use_now = true | ||
self | ||
end | ||
|
||
private | ||
|
||
def 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 = original_flash.instance_variable_get('@flashes').dup | ||
new_flash.instance_variable_set('@flashes', flashes) | ||
end | ||
|
||
def copy_discard_if_necessary(original_flash, new_flash) | ||
discard = original_flash.instance_variable_get('@discard').dup | ||
new_flash.instance_variable_set('@discard', discard) | ||
end | ||
|
||
def sweep_flash_if_necessary(flash) | ||
unless @use_now | ||
flash.sweep | ||
end | ||
end | ||
|
||
def set_values | ||
flash.instance_variable_get('@flashes') | ||
end | ||
|
||
def keys_to_discard | ||
flash.instance_variable_get('@discard') | ||
end | ||
|
||
def values_to_check | ||
if @use_now | ||
set_values.slice(*keys_to_discard.to_a) | ||
else | ||
set_values.except(*keys_to_discard.to_a) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Shoulda | ||
module Matchers | ||
module ActionController | ||
# @private | ||
class SessionStore | ||
attr_accessor :controller | ||
|
||
def name | ||
'session' | ||
end | ||
|
||
def has_key?(key) | ||
session.key?(key) | ||
end | ||
|
||
def has_value?(expected_value) | ||
session.values.any? do |actual_value| | ||
expected_value === actual_value | ||
end | ||
end | ||
|
||
def empty? | ||
session.empty? | ||
end | ||
|
||
private | ||
|
||
def session | ||
controller.session | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.