Permalink
Browse files

Unintrusive login workflow

This was done to enable submitting a comment when logged out.
  • Loading branch information...
1 parent 578912a commit 4cc03616a79777d0adebecdd30f8262d2054e01d GIT_AUTHOR_NAME committed with veezus Apr 20, 2009
@@ -81,22 +81,22 @@ def url_for_news_item(news_item)
end
def store_comment_for_non_logged_in_user
- title, body, news_item_id = params_for_comment(params)
- if title && body && news_item_id
- session[:return_to] = url_for_news_item(NewsItem.find_by_id(params[:news_item_id]))
+ title, body, commentable_id = params_for_comment(params)
+ if title && body && commentable_id
+ session[:return_to] = url_for_news_item(NewsItem.find_by_id(params[:commentable_id]))
session[:title] = title
session[:body] = body
- session[:news_item_id] = news_item_id
+ session[:news_item_id] = commentable_id
end
end
def params_for_comment(comment_params)
comment_params.symbolize_keys!
if comment_params[:comment]
comment_params[:comment].symbolize_keys!
- [comment_params[:comment][:title], comment_params[:comment][:body], comment_params[:pitch_id]]
+ [comment_params[:comment][:title], comment_params[:comment][:body], comment_params[:commentable_id]]
else
- [comment_params[:title], comment_params[:body], comment_params[:news_item_id]]
+ [comment_params[:title], comment_params[:body], comment_params[:commentable_id]]
end
end
@@ -108,4 +108,26 @@ def handle_comment_for_non_logged_in_user
session[:body] = nil
end
end
+
+ layout :application_except_xhr
+ def application_except_xhr
+ request.xhr? ? false : "application"
+ end
+
+ def set_ajax_flash(type, message)
+ if request.xhr?
+ headers["X-Flash-#{type.to_s.capitalize}"] = message
+ else
+ flash[type] = message
+ end
+ end
+
+ def flash_and_redirect(type, message, url = root_path)
+ set_ajax_flash(type, message)
+ if request.xhr?
+ render :nothing => true
+ else
+ redirect_to url
+ end
+ end
end
@@ -1,6 +1,6 @@
class CommentsController < ApplicationController
skip_before_filter :verify_authenticity_token
- before_filter :login_required, :except => :create
+ before_filter :login_required
resources_controller_for :comments, :only => [:create, :index]
@@ -5,10 +5,9 @@ class Myspot::DonationsController < ApplicationController
response_for :create do |format|
if resource_saved?
update_balance_cookie
- format.js { render :text => "document.location = '#{edit_myspot_donations_amounts_url}';" }
format.html { redirect_to edit_myspot_donations_amounts_path }
else
- format.js { render :action => "new"}
+ format.html { render :text => "TODO" }
end
end
@@ -5,10 +5,9 @@ class PledgesController < ApplicationController
response_for :create do |format|
if resource_saved?
- format.js { render :text => "document.location = '#{search_news_items_path(:news_item_type=>'tips', :sort_by=>'desc')}';" }
format.html { redirect_to search_news_items_path(:news_item_type=>'tips', :sort_by=>'desc') }
else
- format.js { render :action => "new"}
+ format.html { render :text => resource.errors[:base] }
end
end
@@ -4,9 +4,8 @@ def new
store_news_item_for_non_logged_in_user
store_comment_for_non_logged_in_user
store_location(params[:return_to] || root_path)
- respond_to do |format|
- format.html
- format.js { render :partial => "header_form", :layout => false }
+ if request.xhr?
+ render :partial => "header_form"
end
end
@@ -18,11 +17,20 @@ def create
update_balance_cookie
handle_first_donation_for_non_logged_in_user
handle_first_pledge_for_non_logged_in_user
- handle_comment_for_non_logged_in_user
- redirect_back_or_default('/')
+
+ if request.xhr?
+ render :nothing => true
+ else
+ redirect_back_or_default('/')
+ end
else
@user = User.new
- render :action => 'new'
+ if request.xhr?
+ set_ajax_flash(:error, 'Invalid email or password.')
+ render :status => :unprocessable_entity, :nothing => true
+ else
+ render 'new'
+ end
end
end
@@ -10,13 +10,19 @@ def create
@user = User.new(params[:user])
if @user.save
unless @user.organization?
- flash[:success] = 'Click the link in the email we just sent to you to finish creating your account!'
+ self.current_user = @user
+ create_current_login_cookie
+ update_balance_cookie
+ flash_and_redirect(:success, 'Click the link in the email we just sent to you to finish creating your account!', root_path)
else
- flash[:success] = "Your account will be reviewed prior to approval. We'll get back to you as soon as possible."
+ flash_and_redirect(:success, "Your account will be reviewed prior to approval. We'll get back to you as soon as possible.", root_path)
end
- redirect_to root_path
else
- render :action => 'new'
+ if request.xhr?
+ render :partial => 'sessions/header_form', :status => :unprocessable_entity
+ else
+ render :action => 'new', :status => :unprocessable_entity
+ end
end
end
@@ -7,7 +7,4 @@
.row
= f.hidden_field :commentable_id
= f.hidden_field :commentable_type
- - if logged_in?
- = f.submit 'Post Comment'
- - else
- = button_to_function "Post Comment", "submitCommentToLogin();", :rel => "facebox"
+ = f.submit 'Post Comment'
@@ -1,7 +1,4 @@
-- remote_form_for Donation.new(:pitch => news_item, :amount => Donation::DEFAULT_AMOUNT), :url => myspot_donations_path, :html => {:id => "new_donation_#{news_item.id}"} do |f|
- = f.hidden_field :amount
+- form_for Donation.new(:pitch => news_item, :amount => Donation::DEFAULT_AMOUNT), :url => myspot_donations_path, :html => {:id => "new_donation_#{news_item.id}", :class => "auth"} do |f|
+ = f.hidden_field :amount, :id => "donation_default_amount"
= f.hidden_field :pitch_id
- - if logged_in?
- = image_submit_tag "donate_default.png", :alt => "Donate #{Donation::DEFAULT_AMOUNT}", :title => "Donate #{Donation::DEFAULT_AMOUNT}"
- - else
- = link_to image_tag("donate_default.png"), new_session_path(:return_to => edit_myspot_donations_amounts_path, :donation_amount => Donation::DEFAULT_AMOUNT, :news_item_id => news_item.id), :rel => "facebox", :title => "Donate #{Donation::DEFAULT_AMOUNT}"
+ = image_submit_tag "donate_default.png", :alt => "Donate #{Donation::DEFAULT_AMOUNT}", :title => "Donate #{Donation::DEFAULT_AMOUNT}"
@@ -1,13 +1,12 @@
-- remote_form_for Donation.new(:pitch => news_item), :url => myspot_donations_path, :html => {:id => "custom_donation_#{news_item.id}"} do |f|
- - if current_user && current_user.has_donated_to?(news_item)
- .float_left= f.text_field :amount, :value => number_to_currency(current_user.max_donation_for(news_item), :unit => '')
- - else
- .float_left= f.text_field :amount, :value => number_to_currency(news_item.default_donation_amount, :unit => '')
+- form_for Donation.new(:pitch => news_item), :url => myspot_donations_path, :html => {:id => "custom_donation_#{news_item.id}", :class => "auth"} do |f|
+ .float_left
+ = f.label :amount, "Donate other amount", :class => "hide"
+ - if current_user && current_user.has_donated_to?(news_item)
+ = f.text_field :amount, :value => number_to_currency(current_user.max_donation_for(news_item), :unit => '')
+ - else
+ = f.text_field :amount, :value => number_to_currency(news_item.default_donation_amount, :unit => '')
= f.hidden_field :pitch_id
.float_right
- - if logged_in?
- = image_submit_tag("donate.png", :title => "Donate another amount")
- - else
- = link_to_function image_tag("donate.png"), "submitToLogin('custom_donation_#{news_item.id}')", :rel => 'facebox', :title => "Donate another amount"
+ = image_submit_tag("donate.png", :title => "Donate another amount", :alt => "Donate another amount")
.clear
@@ -31,6 +31,7 @@
= javascript_include_tag 'jcarousellite_1.0.1'
= javascript_include_tag 'jquery.form'
= javascript_include_tag 'application'
+ = javascript_include_tag 'auth'
= stylesheet_link_tag 'facebox'
= javascript_include_tag 'facebox'
@@ -12,7 +12,7 @@
%li
= link_to "Register", new_session_path, :rel => 'facebox'
%li.no-pipe
- = link_to "Login", new_session_path, :rel => 'facebox'
+ = link_to "Login", new_session_path, :rel => 'facebox', :id => "sign_in"
.slogan
%span.pink Community
@@ -20,15 +20,14 @@
- else
= network.display_name
- if yield(:error)
- .flash.error
+ .error
%span.dismiss= link_to image_tag('close_square.png'), @close_flash_link || '' # empty string means self
%p= yield(:error)
- - if flash.any?
- .flash
- - flash.each do |key, value|
- %div{ :id => "flash_#{key}", :class => key }
- %span.dismiss= link_to image_tag('close_square.png'), @close_flash_link || '' # empty string means self
- %p.flash_note= value
+ #flash
+ - flash.each do |key, value|
+ %div{:class => key }
+ %span.dismiss= link_to image_tag('close_square.png'), @close_flash_link || '' # empty string means self
+ %p= value
= yield
.clear
#footer
@@ -53,7 +53,7 @@
= render :partial => 'shared/comment', :collection => @pitch.comments
%h3 Post A Comment
.double_content_border
- - form_for [@pitch, Comment.new(:commentable => @pitch)], :html => { :id => 'comments_form' } do |f|
+ - form_for [@pitch, Comment.new(:commentable => @pitch)], :html => { :id => 'comments_form', :class => "auth" } do |f|
= render :partial => 'comments/form', :locals => {:f => f}
.span-220.column_v.box_white
@@ -3,22 +3,18 @@
- else
%div{:id => "inline_pledge_form_#{news_item.id}"}
%div{:id => "pledge_button_#{news_item.id}"}
- - remote_form_for Pledge.new(:tip => news_item, :amount => Donation::DEFAULT_AMOUNT) do |f|
- = f.hidden_field :amount
- = f.hidden_field :tip_id
- - if logged_in?
+ - form_for Pledge.new(:tip => news_item, :amount => Donation::DEFAULT_AMOUNT), :html => {:class => 'auth'} do |f|
+ %div
+ = f.hidden_field :amount
+ = f.hidden_field :tip_id
= image_submit_tag("pledge_default.png")
- - else
- = link_to image_tag("pledge_default.png"), new_session_path(:return_to => myspot_pledges_path, :pledge_amount => Donation::DEFAULT_AMOUNT, :news_item_id => news_item.id), :rel => "facebox", :title => "Pledge #{Donation::DEFAULT_AMOUNT}"
%div.inline_pledge{:style => "display:none", :id => "pledge_custom_#{news_item.id}"}
- - remote_form_for Pledge.new(:tip => news_item), :html => { :id => "custom_pledge_#{news_item.id}" } do |f|
- .float_left= f.text_field :amount
- = f.hidden_field :tip_id
- - if logged_in?
- .float_right= image_submit_tag("pledge.png")
- - else
- .float_right= link_to_function image_tag("pledge.png"), "submitToLogin('custom_pledge_#{news_item.id}', 'pledge')", :rel => "facebox", :title => "Pledge #{Donation::DEFAULT_AMOUNT}"
+ - form_for Pledge.new(:tip => news_item), :html => { :id => "custom_pledge_#{news_item.id}", :class => 'auth' } do |f|
+ .float_left
+ = f.hidden_field :tip_id
+ = f.text_field :amount
+ .float_right= image_submit_tag("pledge.png")
.clear
.small.uppcased_button_text
= link_to_function "OR PLEDGE ANOTHER AMOUNT &raquo;", %{jQuery("#pledge_button_#{news_item.id}, #pledge_custom_#{news_item.id}").toggle();}
@@ -1,5 +1,5 @@
.login-boxer.span-13
- .span-12
+ .span-12.login
- form_tag session_url do
%h3 Thanks for joining us!
.double_content_border
@@ -8,6 +8,6 @@
.double_content_border
%input{:type => 'image', :src=>'/images/login.png', :alt => "Login", :class => "float-right"}
- .span-12
+ .span-12.register
= render :partial => "users/form"
.clear
@@ -27,7 +27,7 @@
= render :partial => 'shared/comment', :collection => @story.comments
%h3 Post A Comment
.double_content_border
- - form_for [@story, Comment.new] do |f|
+ - form_for [@story, Comment.new], :html => {:class => 'auth'} do |f|
= render :partial => 'comments/form', :locals => {:f => f}
.span-220.column_v.box_white
%h3.description
@@ -24,7 +24,7 @@
= render :partial => 'shared/comment', :collection => @tip.comments
%h3 Post A Comment
.double_content_border
- - form_for [@tip, Comment.new] do |f|
+ - form_for [@tip, Comment.new], :html => {:class => 'auth'} do |f|
= render :partial => 'comments/form', :locals => {:f => f}
.span-220.column_v.box_white
.centered= render :partial => "pledges/button", :locals => { :news_item => @tip }
@@ -1,5 +1,5 @@
- if @user && @user.errors.any?
- - content_for :error do
+ .error
= error_messages_for :user
%h3
@@ -9,7 +9,7 @@
.float-right
= link_to "Resend Activation Email", activation_email_user_path, :class => "forgot_password"
.double_content_border
-- form_for(:user, @user, :url => user_path) do |f|
+- form_for(:user, @user, :url => user_path, :html => {:class => 'register'}) do |f|
.register
%div
= f.label :first_name, "Your First Name"
@@ -20,7 +20,6 @@ Feature: Creating a pitch
Then I should be on the show pitch page
And I should see "Pitch was successfully created"
And I should see "Pitch Headline"
- And I should see a form with class "new_donation"
And I should see an "Edit This Pitch" titled link
Scenario: A reporter creates an invalid pitch
@@ -1,39 +1,18 @@
Feature: Donating
- Scenario: New User Donating 20
- Given A pitch exists
+ Scenario: Existing User Donating 20
+ Given a pitch
+ And I am logged in as a citizen
And I view the current pitch page
- And I follow "Donate 20"
- And I follow "New USERS: REGISTER HERE"
- And I fill in "Your First Name" with "My First Name"
- And I fill in "Your Last Name" with "My Last Name"
- And I fill in "Password" with "password"
- And I fill in "Confirm Password" with "password"
- And I fill in "Your E-mail Address" with "me@example.com"
- And I select "Community Member" from "user[type]"
- And I check "user_terms_of_service"
- And I press "Register"
- And I should see "Click the link in the email we just sent to you to finish creating your account!"
- When I activate my account with email "me@example.com"
+ When I press "Donate 20"
Then I should be on the edit myspot::donation_amounts page
And I should see "20.00" inside a text field
-# TODO: write this test in culerity if that ever is setup for this project
-# Scenario: New User Donating Variable Amount
-# Given A pitch exists
-# And I view the current pitch page
-# And I follow "Donate another amount"
-# And I follow "REGISTER HERE"
-# And I fill in "Your First Name" with "My First Name"
-# And I fill in "Your Last Name" with "My Last Name"
-# And I fill in "Password" with "password"
-# And I fill in "Confirm Password" with "password"
-# And I fill in "Your E-mail Address" with "me@example.com"
-# And I choose "Community Member"
-# And I check "user_terms_of_service"
-# And I press "Register"
-# And I should see "Click the link in the email we just sent to you to finish creating your account!"
-# When I activate my account with email "me@example.com"
-# Then I should be on the edit myspot::donation_amounts page
-# And I should see "200.00" inside a text field
-#
+ Scenario: Existing User Donating Variable Amount
+ Given a pitch
+ And I am logged in as a citizen
+ And I view the current pitch page
+ And I fill in "Donate other amount" with "50.00"
+ When I press "Donate another amount"
+ Then I should be on the edit myspot::donation_amounts page
+ And I should see "50.00" inside a text field
@@ -10,7 +10,7 @@
instance_variable_set("@#{model_name}", Factory(model_name.to_sym, :user => @current_user))
end
-Given /^A (\w+) exists$/ do |model_name|
+Given /^[Aa] (\w+)(?: exists)?$/ do |model_name|
instance = Factory(model_name.to_sym)
instance_variable_set("@#{model_name}", instance)
end
Oops, something went wrong.

0 comments on commit 4cc0361

Please sign in to comment.