-
Notifications
You must be signed in to change notification settings - Fork 69
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
Session strategy #32
Open
jasonblalock
wants to merge
2
commits into
pda:master
Choose a base branch
from
jasonblalock:session-strategy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Session strategy #32
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,9 +1,4 @@ | ||
module Flip | ||
class Engine < ::Rails::Engine | ||
|
||
initializer "flip.blarg" do | ||
ActionController::Base.send(:include, Flip::CookieStrategy::Loader) | ||
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,64 @@ | ||
# Uses session to determine feature state. | ||
module Flip | ||
class SessionStrategy < AbstractStrategy | ||
|
||
def description | ||
"Uses session cookie to apply only to your session." | ||
end | ||
|
||
def knows? definition | ||
session.key? session_name(definition) | ||
end | ||
|
||
def on? definition | ||
feature = session[session_name(definition)] | ||
feature_value = feature.is_a?(Hash) ? feature['value'] : feature | ||
feature_value === 'true' | ||
end | ||
|
||
def switchable? | ||
true | ||
end | ||
|
||
def switch! key, on | ||
session[session_name(key)] = on ? "true" : "false" | ||
end | ||
|
||
def delete! key | ||
session.delete session_name(key) | ||
end | ||
|
||
def self.session= session | ||
@session = session | ||
end | ||
|
||
def session_name(definition) | ||
definition = definition.key unless definition.is_a? Symbol | ||
"flip_#{definition}" | ||
end | ||
|
||
private | ||
|
||
def session | ||
result = self.class.instance_variable_get(:@session) || {} | ||
end | ||
|
||
# Include in ApplicationController to push cookies into CookieStrategy. | ||
# Users before_filter and after_filter rather than around_filter to | ||
# avoid pointlessly adding to stack depth. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: |
||
module Loader | ||
extend ActiveSupport::Concern | ||
included do | ||
before_filter :flip_session_strategy_before | ||
after_filter :flip_session_strategy_after | ||
end | ||
def flip_session_strategy_before | ||
SessionStrategy.session = session | ||
end | ||
def flip_session_strategy_after | ||
SessionStrategy.session = nil | ||
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,110 @@ | ||
require "spec_helper" | ||
|
||
class ControllerWithoutSessionStrategy; end | ||
class ControllerWithSessionStrategy | ||
def self.before_filter(_); end | ||
def self.after_filter(_); end | ||
def session; []; end | ||
include Flip::SessionStrategy::Loader | ||
end | ||
|
||
describe Flip::SessionStrategy do | ||
|
||
let(:session) do | ||
{ strategy.session_name(:one) => "true", | ||
strategy.session_name(:two) => "false" } | ||
end | ||
let(:strategy) do | ||
Flip::SessionStrategy.new.tap do |s| | ||
s.stub(:session) { session } | ||
end | ||
end | ||
|
||
its(:description) { should be_present } | ||
it { should be_switchable } | ||
describe "session interrogration" do | ||
context "enabled feature" do | ||
specify "#knows? is true" do | ||
strategy.knows?(:one).should be true | ||
end | ||
specify "#on? is true" do | ||
strategy.on?(:one).should be true | ||
end | ||
end | ||
context "disabled feature" do | ||
specify "#knows? is true" do | ||
strategy.knows?(:two).should be true | ||
end | ||
specify "#on? is false" do | ||
strategy.on?(:two).should be false | ||
end | ||
end | ||
context "feature with no session present" do | ||
specify "#knows? is false" do | ||
strategy.knows?(:three).should be false | ||
end | ||
specify "#on? is false" do | ||
strategy.on?(:three).should be false | ||
end | ||
end | ||
end | ||
|
||
describe "session manipulation" do | ||
it "can switch known features on" do | ||
strategy.switch! :one, true | ||
strategy.on?(:one).should be true | ||
end | ||
it "can switch unknown features on" do | ||
strategy.switch! :three, true | ||
strategy.on?(:three).should be true | ||
end | ||
it "can switch features off" do | ||
strategy.switch! :two, false | ||
strategy.on?(:two).should be false | ||
end | ||
it "can delete knowledge of a feature" do | ||
strategy.delete! :one | ||
strategy.on?(:one).should be false | ||
strategy.knows?(:one).should be false | ||
end | ||
end | ||
|
||
end | ||
|
||
describe Flip::SessionStrategy::Loader do | ||
|
||
it "adds filters when included in controller" do | ||
ControllerWithoutSessionStrategy.tap do |klass| | ||
klass.should_receive(:before_filter).with(:flip_session_strategy_before) | ||
klass.should_receive(:after_filter).with(:flip_session_strategy_after) | ||
klass.send :include, Flip::SessionStrategy::Loader | ||
end | ||
end | ||
|
||
describe "filter methods" do | ||
let(:strategy) { Flip::SessionStrategy.new } | ||
let(:controller) { ControllerWithSessionStrategy.new } | ||
describe "#flip_session_strategy_before" do | ||
it "passes controller session to SessionStrategy" do | ||
controller.should_receive(:session).and_return(strategy.session_name(:test) => "true") | ||
expect { | ||
controller.flip_session_strategy_before | ||
}.to change { | ||
[ strategy.knows?(:test), strategy.on?(:test) ] | ||
}.from([false, false]).to([true, true]) | ||
end | ||
end | ||
describe "#flip_session_strategy_after" do | ||
before do | ||
Flip::SessionStrategy.session = { strategy.session_name(:test) => "true" } | ||
end | ||
it "passes controller session to SessionStrategy" do | ||
expect { | ||
controller.flip_session_strategy_after | ||
}.to change { | ||
[ strategy.knows?(:test), strategy.on?(:test) ] | ||
}.from([true, true]).to([false, false]) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a copy-paste error in this string.
Perhaps this is better:
# Alternative: Use Flip::SessionStrategy to use Rails' encrypted sessions instead of cookies