Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unintrusive login workflow

This was done to enable submitting a comment when logged out.
  • Loading branch information...
commit 4cc03616a79777d0adebecdd30f8262d2054e01d 1 parent 578912a
GIT_AUTHOR_NAME authored veezus committed
Showing with 122 additions and 113 deletions.
  1. +28 −6 app/controllers/application_controller.rb
  2. +1 −1  app/controllers/comments_controller.rb
  3. +1 −2  app/controllers/myspot/donations_controller.rb
  4. +1 −2  app/controllers/pledges_controller.rb
  5. +14 −6 app/controllers/sessions_controller.rb
  6. +10 −4 app/controllers/users_controller.rb
  7. +1 −4 app/views/comments/_form.html.haml
  8. +3 −6 app/views/donations/_donate_default_form.html.haml
  9. +8 −9 app/views/donations/_donate_variable_amount_form.html.haml
  10. +1 −0  app/views/layouts/_head.html.haml
  11. +1 −1  app/views/layouts/_user_header.html.haml
  12. +6 −7 app/views/layouts/application.html.haml
  13. +1 −1  app/views/pitches/show.html.haml
  14. +9 −13 app/views/pledges/_button.html.haml
  15. +2 −2 app/views/sessions/_header_form.html.haml
  16. +1 −1  app/views/stories/show.html.haml
  17. +1 −1  app/views/tips/show.html.haml
  18. +2 −2 app/views/users/_form.html.haml
  19. +0 −1  features/create_a_pitch.feature
  20. +12 −33 features/donating.feature
  21. +1 −1  features/step_definitions/spotus_steps.rb
  22. +10 −1 public/javascripts/application.js
  23. +1 −1  public/stylesheets/screen_spotus.css
  24. +3 −3 spec/controllers/application_spec.rb
  25. +1 −1  spec/controllers/myspot/donations_controller_spec.rb
  26. +2 −3 spec/controllers/users_controller_spec.rb
  27. +1 −1  spec/views/users/new.html.haml_spec.rb
View
34 app/controllers/application_controller.rb
@@ -81,12 +81,12 @@ 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
@@ -94,9 +94,9 @@ 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
View
2  app/controllers/comments_controller.rb
@@ -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]
View
3  app/controllers/myspot/donations_controller.rb
@@ -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
View
3  app/controllers/pledges_controller.rb
@@ -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
View
20 app/controllers/sessions_controller.rb
@@ -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
View
14 app/controllers/users_controller.rb
@@ -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
View
5 app/views/comments/_form.html.haml
@@ -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'
View
9 app/views/donations/_donate_default_form.html.haml
@@ -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}"
View
17 app/views/donations/_donate_variable_amount_form.html.haml
@@ -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
View
1  app/views/layouts/_head.html.haml
@@ -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'
View
2  app/views/layouts/_user_header.html.haml
@@ -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
View
13 app/views/layouts/application.html.haml
@@ -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
View
2  app/views/pitches/show.html.haml
@@ -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
View
22 app/views/pledges/_button.html.haml
@@ -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();}
View
4 app/views/sessions/_header_form.html.haml
@@ -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
View
2  app/views/stories/show.html.haml
@@ -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
View
2  app/views/tips/show.html.haml
@@ -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 }
View
4 app/views/users/_form.html.haml
@@ -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"
View
1  features/create_a_pitch.feature
@@ -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
View
45 features/donating.feature
@@ -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
View
2  features/step_definitions/spotus_steps.rb
@@ -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
View
11 public/javascripts/application.js
@@ -16,6 +16,7 @@ jQuery(document).ready(function($){
btnPrev: ".prev",
visible: 1
});
+
});
jQuery("a").click(function($){
@@ -106,4 +107,12 @@ function load_categories(id) {
});
}
-
+jQuery(document).ajaxComplete(function(options, r) {
+ var notice;
+ var dismiss = "<span class=\"dismiss\"><a href=\"\"><img src=\"/images/close_square.png\" alt=\"Dismiss\" /></span>";
+ jQuery.each(["Success", "Notice", "Error"], function() {
+ if(notice = r.getResponseHeader("X-Flash-" + this)) {
+ jQuery("#flash").append(jQuery("<div/>").addClass(this.toLowerCase()).html(dismiss + "<p>" + notice + "</p>"));
+ }
+ });
+});
View
2  public/stylesheets/screen_spotus.css
@@ -417,7 +417,7 @@ body {
min-height: 80px;
}
-.error p.flash_note, .success p.flash_note, .notice p.flash_note {
+#flash p {
padding: 20px 0 0 0;
}
.success, .notice {
View
6 spec/controllers/application_spec.rb
@@ -62,7 +62,7 @@
describe "#store_comment_for_non_logged_in_user" do
before do
- controller.stub!(:params).and_return({"action"=>"create", "controller"=>"comments", "pitch_id"=>"1", "comment"=>{"body"=>"bar", "title"=>"foo"}})
+ controller.stub!(:params).and_return({"action"=>"create", "controller"=>"comments", "commentable_id"=>"1", "comment"=>{"body"=>"bar", "title"=>"foo"}})
end
it "saves the comment title and body to session" do
controller.send(:store_comment_for_non_logged_in_user)
@@ -82,8 +82,8 @@
describe "#params_for_comment" do
before do
- @html_params = {"action"=>"create", "controller"=>"comments", "pitch_id"=>"1", "comment"=>{"body"=>"bar", "title"=>"foo"}}
- @js_params = {:title => "foo", :body => "bar", :news_item_id => "1"}
+ @html_params = {"action"=>"create", "controller"=>"comments", "commentable_id"=>"1", "comment"=>{"body"=>"bar", "title"=>"foo"}}
+ @js_params = {:title => "foo", :body => "bar", :commentable_id => "1"}
end
it "returns title, body and news_item_id for html params" do
View
2  spec/controllers/myspot/donations_controller_spec.rb
@@ -55,7 +55,7 @@ def do_destroy
end
def do_create
- xhr :post, :create, :donation => {:pitch_id => @pitch.id, :amount => 25}
+ post :create, :donation => {:pitch_id => @pitch.id, :amount => 25}
end
end
View
5 spec/controllers/users_controller_spec.rb
@@ -15,7 +15,6 @@
lambda do
do_create(:email => nil)
assigns[:user].errors.on(:email).should_not be_nil
- response.should be_success
end.should_not change(User, :count)
end
@@ -25,8 +24,8 @@
post :create, :user => {}
end
- it "should be successful" do
- response.should be_success
+ it "should render the new template" do
+ response.should render_template('users/new.html.haml')
end
it "should have errors on the user" do
View
2  spec/views/users/new.html.haml_spec.rb
@@ -68,7 +68,7 @@
end
it "should display an error message" do
- template.should_receive(:content_for).once.with(:error)
+ template.should_receive(:error_messages_for).with(:user)
do_render
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.