Permalink
Browse files

Show modal login dialog when anonymous tries to vote. Proper redirect…

…s after registration.
  • Loading branch information...
Antono Vasiljev
Antono Vasiljev committed Jul 14, 2012
1 parent 022739b commit 401f86a5742ebb94d171b040096fa9af8bc6d416
@@ -3,10 +3,13 @@ jQuery ->
$('.user .extra-info').toggle()
$('.extra-tools .upvote, .extra-tools .downvote').click (ev) ->
- $.ajax $(this).data('voteurl'),
- type: 'POST'
- dataType: 'json'
- data:
- direction: $(this).data('direction'),
- success: (data) ->
- $('.extra-tools .rating').html(data.rating)
+ if userLoggedIn
+ $.ajax $(this).data('voteurl'),
+ type: 'POST'
+ dataType: 'json'
+ data:
+ direction: $(this).data('direction'),
+ success: (data) ->
+ $('.extra-tools .rating').html(data.rating)
+ else
+ $('#login-modal').clone().removeClass('hidden').modal()
@@ -17,7 +17,6 @@ def logged_in?
def login_required
unless logged_in?
flash[:error] = "You must first log in or sign up before accessing this page."
- store_target_location
redirect_to login_session_url
end
end
@@ -27,8 +26,4 @@ def redirect_to_target_or_default(default)
session[:return_to] = nil
end
- def store_target_location
- session[:return_to] = request.fullpath
- end
-
end
@@ -1,30 +1,42 @@
class SessionsController < ApplicationController
+
def create
provider = params[:provider]
provider_uid_field = "#{provider}_uid"
provider_name_field = "#{provider}_name"
omniauth = request.env["omniauth.auth"]
+
user = User.where(provider_uid_field => omniauth['uid']).first
+ origin = request.env['omniauth.origin']
+ target = if origin == login_session_url
+ records_path
+ else
+ origin || records_path
+ end
+
if user
- flash[:notice] = "Signed in successfully."
+ flash[:notice] = "Signed in successfully"
session[:user_id] = user.id.to_s
- redirect_to_target_or_default records_path
+ redirect_to target
else
user_info = omniauth['info']
user = User.new(nickname: user_info['nickname'] || user_info['name'])
- user.update_attribute(provider_uid_field, omniauth['uid'])
+
+ user.update_attribute(provider_uid_field, omniauth['uid'])
user.update_attribute(provider_name_field, user_info['nickname'] || user_info['name'])
- user.update_attribute('email', user_info['email'])
- user.update_attribute('about', user_info['description'])
+
+ user.update_attribute('email', user_info['email'])
+ user.update_attribute('about', user_info['description'])
user.update_attribute('website', user_info['urls'].try(:values).try(:last))
if user.save
session[:user_id] = user.id.to_s
- flash[:notice] = "Signed in successfully."
+ flash[:notice] = "Signed in successfully"
+ session[:return_to] = target
redirect_to edit_user_path(user)
else
- flash[:notice] = "Failed"
+ flash[:notice] = "Authentication failed"
redirect_to root_url
end
end
@@ -21,7 +21,7 @@ def update
else
flash[:error] = "Heh. No Way, man :)"
end
- redirect_to @user
+ redirect_to_target_or_default @user
end
private
@@ -16,6 +16,8 @@
= auto_discovery_link_tag :atom,
dashboard_url(key: current_user.atom_key, format: :atom),
title: "Shelr.tv - new activity for #{current_user.nickname}"
+ %script
+ window.userLoggedIn = #{logged_in? ? 'true' : 'false'}
= yield :head
= yield :meta
@@ -12,9 +12,9 @@ Feature: Records
| goodbye world | ls.json | ntanyone |
| shelr manual | ls.json | antono |
| private record | ls.json | someone |
- And I signed in
Scenario: browsing records
+ Given I signed in
When I visit "/records" page
And I click link "hello world"
Then I should see player for "hello world"
@@ -23,6 +23,7 @@ Feature: Records
Then I should see player for "goodbye world"
Scenario: private records are hidden
+ Given I signed in
Given record "private record" is private
And I am not owner of "private record" record
When I visit "/records" page
@@ -33,19 +34,22 @@ Feature: Records
Then I should not see "private record"
Scenario: visiting private records without access key
+ Given I signed in
Given record "private record" is private
When I visit "private record" record page
Then I should not see "private record"
And I should see "No such record"
Scenario: visiting private record with access key
+ Given I signed in
Given record "hello world" is private
When I visit "hello world" record page with access key
Then I should see "hello world"
And I should see player for "hello world"
And I should not see "No such record"
Scenario: owner always can see his private records
+ Given I signed in
Given record "shelr manual" is private
And I am the owner of "shelr manual" record
When I visit "/records" page
@@ -54,11 +58,13 @@ Feature: Records
Then I should see player for "shelr manual"
Scenario: owner can edit own record
+ Given I signed in
Given I am the owner of "shelr manual" record
When I change "shelr manual" record title to "new shelr manual"
Then I should see "Record was succesfully updated."
Scenario: registered user can vote for records
+ Given I signed in
Given record "shelr manual" has 3 upvotes and 2 downvotes
When I visit "shelr manual" record page
Then I should see +1 button
@@ -73,3 +79,9 @@ Feature: Records
Then rating of "shelr manual" should be 0
When I click +1 button
Then rating of "shelr manual" should be 2
+
+ Scenario: anonymous user tries to vote for records
+ Given record "shelr manual" has 3 upvotes and 2 downvotes
+ When I visit "shelr manual" record page
+ When I click +1 button
+ Then I should see login modal dialog
@@ -8,6 +8,10 @@
click_button 'Save'
end
+step "I should see login modal dialog" do
+ page.should have_css '#login-modal'
+end
+
step "I'm on the :path page" do |path|
visit path
end

0 comments on commit 401f86a

Please sign in to comment.