Skip to content

Instantly share code, notes, and snippets.

@abrambailey
Created August 10, 2014 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abrambailey/dceb8f450b6f139fab2a to your computer and use it in GitHub Desktop.
Save abrambailey/dceb8f450b6f139fab2a to your computer and use it in GitHub Desktop.
How to add Spree to an existing Ruby on Rails application with devise authentication
gem 'spree', github: 'spree/spree', :branch => '2-3-stable'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '2-3-stable'
$ bundle install
$ rails g spree:install --migrate=false --sample=false --seed=false
config/initializers/spree.rb by changing this line: Spree.user_class = "Spree::User" to this: Spree.user_class = "User"
$ rake spree_auth:install:migrations
$ rails g spree:auth:install
$ rails g spree:custom_user User
$ rake db:migrate
Add this code to user.rb (unless it is already there)
include Spree::UserReporting
include Spree::UserApiAuthentication
has_and_belongs_to_many :spree_roles,
:join_table => 'spree_roles_users',
:foreign_key => "user_id",
:class_name => "Spree::Role"
has_many :spree_orders, :foreign_key => "user_id", :class_name => "Spree::Order"
belongs_to :ship_address, :class_name => 'Spree::Address'
belongs_to :bill_address, :class_name => 'Spree::Address'
# has_spree_role? simply needs to return true or false whether a user has a role or not.
def has_spree_role?(role_in_question)
spree_roles.where(:name => role_in_question.to_s).any?
end
def last_incomplete_spree_order
spree_orders.incomplete.where(:created_by_id => self.id).order('created_at DESC').first
end
user = [find desired user for admin role]
user.spree_roles << Spree::Role.find_or_create_by(name: "admin")
#routes.rb
mount Spree::Core::Engine, :at => '/spree'
#will_paginate.rb (if using will_paginate gem)
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
def per(value = nil) per_page(value) end
def total_count() count end
def first_page?() self == first end
def last_page?() self == last end
end
end
module CollectionMethods
alias_method :num_pages, :total_pages
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment