Skip to content

Instantly share code, notes, and snippets.

@kunalchaudhari
Created March 15, 2012 04:05
Show Gist options
  • Select an option

  • Save kunalchaudhari/2041801 to your computer and use it in GitHub Desktop.

Select an option

Save kunalchaudhari/2041801 to your computer and use it in GitHub Desktop.
Simplify Spree's checkout login
// Simplify Spree's checkout login: http://i.imgur.com/aXE4V.png
//
// App available at https://github.com/mikkelb/tootie
//
// If the user has a password, log in and proceed with regular checkout. If she doesn't,
// proceed with guest checkout.
//
// CURRENT STATUS: Only regular checkouts work - it doesn't know how to do guest checkouts.
//
// <iszak> I don't understand why you're even using JS to dictate the path, why not have two
// radio options with customer/guest and then an action that redirects them appropriately.
//
// <Radar> afaik, the form with the login goes to the actual login action then redirects back to
// the order's address. The form on the right goes to an update action for the order and then
// goes back to checkout/registration
// <Radar> You should make it one form that goes to a custom action inside your application and
// then redirect them back to order/address when it's complete.
//
// <versicolor> i think you should call this method :
// https://github.com/spree/spree/blob/master/auth/app/controllers/spree/checkout_controller_decorator.rb#L11
//
(function($){
$(document).ready(function(){
$("form").find("input:radio.action-switcher").change(function() {
var user_session = $(this),
form = user_session.closest("form"),
proceed;
if (login.is(":checked") && login.val() === "guest_user_session") {
proceed = "guest-user-action";
} else {
proceed = "user-action";
}
form.attr("action", form.data(proceed));
});
});
})(jQuery);
<%= form_for :user, :url => spree.user_session_path, :html => {:data => {"user-action" => spree.user_session_path, "guest-user-action" => spree.guest_session_path}} do |f| %>
<fieldset id="sign-in">
<h2>Your email address</h2>
<%= f.label :email, t(:enter_your_email) %>
<%= f.email_field :email, :class => "big" %>
<p><%= t :to_confirm_your_order %></p>
<h2 id="password"><%= t :do_you_have_a_password %></h2>
<ul class="radios">
<li>
<input type="radio" class="action-switcher" value="user_session" />
<%= f.label :password, t(:yes_my_password_is) %>
<%= f.password_field :password, :class => "big" %>
<p><%= link_to t(:forgot_password), spree.new_user_password_path %></p>
</li>
<li>
<input type="radio" class="action-switcher" value="guest_user_session" />
<label>No, continue to checkout</label>
</li>
</ul>
<%= f.submit t(:sign_in), :class => "button dark-big" %>
</fieldset>
<% end %>
Spree::CheckoutController.class_eval do
#
# Custom
#
def guest_user_session
# <Radar> You should make it one form that goes to a custom action inside your application
# and then redirect them back to order/address when it's complete.
# redirect_to ?
end
#
# Default
#
before_filter :check_authorization
before_filter :check_registration, :except => [:registration, :update_registration]
helper "spree/users"
def registration
@user = Spree::User.new
end
def update_registration
fire_event("spree.user.signup", :order => current_order)
# Temporarily change the state to something other than cart so we can validate the order email address.
#
current_order.state = "address"
if current_order.update_attributes(params[:order])
redirect_to checkout_path
else
@user = Spree::User.new
render "registration"
end
end
private
def check_authorization
authorize!(:edit, current_order, session[:access_token])
end
# Introduces a registration step whenever the +registration_step+ preference is true.
#
def check_registration
return unless Spree::Auth::Config[:registration_step]
return if current_user or current_order.email
store_location
redirect_to spree.checkout_registration_path
end
# Overrides the equivalent method defined in Spree::Core.
# This variation of the method will ensure that users are redirected to the tokenized order url unless authenticated as a registered user.
#
def completion_route
return order_path(@order) if current_user
spree.token_order_path(@order, @order.token)
end
end
<!-- Regular checkouts:
https://github.com/spree/spree/blob/master/auth/app/views/spree/shared/_login.html.erb -->
<%= form_for :user, :url => spree.user_session_path do |f| %>
<p>
<%= f.label :email, t(:email) %>
<%= f.email_field :email, :class => 'title' %>
</p>
<p>
<%= f.label :password, t(:password) %>
<%= f.password_field :password, :class => 'title' %>
</p>
<label>
<%= f.check_box :remember_me %>
<%= f.label :remember_me, t(:remember_me) %>
</label>
<%= f.submit t(:login) %>
<% end %>
<!-- Guest checkouts:
https://github.com/spree/spree/blob/master/core/app/views/spree/checkout/registration.html.erb
<%= form_for @order, :url => update_checkout_registration_path, :method => :put, :html => { :id => 'checkout_form_registration' } do |f| %>
<p>
<%= f.label :email, t(:email) %>
<%= f.email_field :email, :class => 'title' %>
</p>
<%= f.submit t(:continue) %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment