Search code examples
ruby-on-railsdevisespree

Spree_auth_devise gem with Devise


Hello fellow Spree users,

I'd like to understand a little more about the spree_auth_devise gem that works with Spree. If I have Devise set up already for user authentication, do I even need this? It's been giving me problems in terms of integrating my existing user model from Devise with its user class. I've read the documentation several times (link here)

Following the spree guides I modified the spree.rb file so that it references my User class. app/config/initializers/spree.rb:

Spree.user_class = "User"

      Rails.application.config.to_prepare do
        require_dependency 'spree/authentication_helpers'
      end

I then ran the migration to add the necessary spree columns to my User table.

class AddSpreeFieldsToCustomUserTable < ActiveRecord::Migration
  def up
    add_column "users", :spree_api_key, :string, :limit => 48
    add_column "users", :ship_address_id, :integer
    add_column "users", :bill_address_id, :integer
  end
end

and it migrated fine. The server starts, but when I navigate to where I mounted Spree, I get this error:

undefined method `last_incomplete_spree_order' for #User:0x007fb1e51824c0

Turns out that the Spree.user_class that I had changed to "User" gets changed back to Spree::User for some reason when I added spree_auth_devise. I verified this in my rails console. So that would make sense why I'm getting that error since all those Spree methods are defined by the Spree user class.

When I delete the spree_auth_devise gem, then suddenly everything works and my Spree.user_class is correctly defined as I set it ("User").

So I figured I would trash that gem and go on with my life. But apparently getting others to pull down my repo causes problems in the Spree migrations if they don't have that spree_auth_devise gem installed. So I've had to tell them add it for the migrations then delete it again when the migrations finish.

Also found the code that sets the user class in spree_auth_devise gem. I'm afraid to change it here.

spree_auth_devise/lib/spree/auth/engine.rb:

initializer "spree_auth_devise.set_user_class", :after => :load_config_initializers do
  Spree.user_class = "Spree::User
end

To sum up:

Why exactly do I need this gem? How can I keep it but change it so that it knows my User class? Thanks.


Solution

  • Spree Auth Devise is just for Spree Authentication. Since you already have Devise setup, I would simply use Spree Auth Devise as an example codebase for any Spree specific user issues and continue with User as your spree user class.

    # config/initializers/spree.rb
    Spree.user_class = "User"
    

    For the migrations, I would remove any spree_auth_devise specific migrations. You'll know they're SAD migrations since Rails namespaces migrations at the end by the Rails engine. For example:

    20141124203907_add_confirmable_to_users.spree_auth.rb
    

    Hope this helps.

    EDIT:

    I was looking at the undefined method last_incomplete_spree_order and it appears it gets defined inside of Spree Core when the Spree user class is set. Source code here.