forked from thoughtbot/clearance
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds SignedInConstraint and SignedOutConstraint, useful from the Rails router. For example, to redirect admins to their admin dashboard as the home page: constraints(SignedInConstraint.new {|user| user.admin?}) do root :to => 'admins/dashboard#index' end
- Loading branch information
1 parent
e084e6b
commit 3746806
Showing
12 changed files
with
160 additions
and
5 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
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,2 @@ | ||
require 'clearance/constraints/signed_in' | ||
require 'clearance/constraints/signed_out' |
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,28 @@ | ||
module Clearance | ||
module Constraints | ||
class SignedIn | ||
def initialize(&block) | ||
@block = block || lambda { |user| true } | ||
end | ||
|
||
def matches?(request) | ||
@request = request | ||
signed_in? && current_user_fulfills_additional_requirements? | ||
end | ||
|
||
private | ||
|
||
def signed_in? | ||
@request.env[:clearance].signed_in? | ||
end | ||
|
||
def current_user_fulfills_additional_requirements? | ||
@block.call(current_user) | ||
end | ||
|
||
def current_user | ||
@request.env[:clearance].current_user | ||
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,9 @@ | ||
module Clearance | ||
module Constraints | ||
class SignedOut | ||
def matches?(request) | ||
request.env[:clearance].signed_out? | ||
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
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,52 @@ | ||
require 'spec_helper' | ||
|
||
describe Clearance::Constraints::SignedIn do | ||
it 'returns true when user is signed in' do | ||
user = create(:user) | ||
|
||
signed_in_constraint = Clearance::Constraints::SignedIn.new | ||
signed_in_constraint.matches?(request_with_remember_token(user.remember_token)).should be_true | ||
end | ||
|
||
it 'returns false when user is not signed in' do | ||
signed_in_constraint = Clearance::Constraints::SignedIn.new | ||
signed_in_constraint.matches?(request_without_remember_token).should be_false | ||
end | ||
|
||
it 'yields a signed-in user to a provided block' do | ||
user = create(:user, :email => 'before@example.com') | ||
signed_in_constraint = Clearance::Constraints::SignedIn.new do |user| | ||
user.update_attribute(:email, 'after@example.com') | ||
end | ||
|
||
signed_in_constraint.matches?(request_with_remember_token(user.remember_token)) | ||
user.reload.email.should == 'after@example.com' | ||
end | ||
|
||
it 'does not yield a user if they are not signed in' do | ||
user = create(:user, :email => 'before@example.com') | ||
|
||
signed_in_constraint = Clearance::Constraints::SignedIn.new do |user| | ||
user.update_attribute(:email, 'after@example.com') | ||
end | ||
|
||
signed_in_constraint.matches?(request_without_remember_token) | ||
user.reload.email.should == 'before@example.com' | ||
end | ||
|
||
it 'matches if the user-provided block returns true' do | ||
user = create(:user) | ||
|
||
signed_in_constraint = Clearance::Constraints::SignedIn.new { |user| true } | ||
|
||
signed_in_constraint.matches?(request_with_remember_token(user.remember_token)).should be_true | ||
end | ||
|
||
it 'does not match if the user-provided block returns false' do | ||
user = create(:user) | ||
|
||
signed_in_constraint = Clearance::Constraints::SignedIn.new { |user| false } | ||
|
||
signed_in_constraint.matches?(request_with_remember_token(user.remember_token)).should be_false | ||
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,15 @@ | ||
require 'spec_helper' | ||
|
||
describe Clearance::Constraints::SignedOut do | ||
it 'returns true when user is signed out' do | ||
signed_out_constraint = Clearance::Constraints::SignedOut.new | ||
signed_out_constraint.matches?(request_without_remember_token).should be_true | ||
end | ||
|
||
it 'returns false when user is not signed out' do | ||
user = create(:user) | ||
|
||
signed_out_constraint = Clearance::Constraints::SignedOut.new | ||
signed_out_constraint.matches?(request_with_remember_token(user.remember_token)).should be_false | ||
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
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,17 @@ | ||
module RememberTokenHelpers | ||
def request_with_remember_token(remember_token) | ||
cookies = {'action_dispatch.cookies' => { | ||
Clearance::Session::REMEMBER_TOKEN_COOKIE => remember_token | ||
}} | ||
env = { :clearance => Clearance::Session.new(cookies) } | ||
Rack::Request.new(env) | ||
end | ||
|
||
def request_without_remember_token | ||
request_with_remember_token(nil) | ||
end | ||
end | ||
|
||
RSpec.configure do |config| | ||
config.include RememberTokenHelpers | ||
end |