RefineryCMS & Spree Quick Start
RefineryCMS and Spree both use Devise for authentication, but they are both completely separate (so you must have separate user accounts for each). In Spree 1.2, authentication has been moved to a separate gem, allowing you to use your own authentication. As of Refinery 2.0 it's still quite a lot of work to roll your own authentication.
For this reason, it seemed easiest to configure Spreee to use RefineryCMS for authentication.
UPDATE: I moved this code into a a gem, to save you some time. See spree-refinery-authentication.
Create a new Refinery app:
gem install refinerycms refinerycms refinery_spree
Add Spree 1.2 to the Gemfile:
gem 'spree', '~> 1.2.0'
rails g spree:install
Update config/routes.rb to use Refinery for the home page
root :to => "refinery/pages#home"
RefineryCMS uses WillPaginage for pagination, while Spree uses Kaminari. Unfortunately, the two don't cooperate, and you will see the following error when you try to view your Spree products list:
NoMethodError in Spree::ProductsController#index undefined method `per' for #<ActiveRecord::Relation:0x007f800252e6b0>
To fix it, add the following monkey patch to config/initializers/will_paginate.rb:
if defined?(WillPaginate) module WillPaginate module ActiveRecord module RelationMethods alias_method :per, :per_page alias_method :num_pages, :total_pages end end end end
When you visit a RefineryCMS page, you will see the following unhelpful error message:
NoMethodError in Refinery/pages#home undefined method `refinery_user?' for #<#<Class:0x007f8a2c762bf0>:0x007f8a2f2a42f0>
I'm not exactly sure what causes this, but it's easy to fix. Simply add the following line to app/controllers/application_controller.rb:
class ApplicationController < ActionController::Base protect_from_forgery helper_method :refinery_user? end
Tell Spree to use RefineryCMS for authentication
Update config/initializers/spree.rb to use Refinery authentication
Spree.user_class = "Refinery::User"
Run generator to add Spree support to Refinery's user model (will add 3 new columns to refinery_users table)
rails g spree:custom_user Refinery::User rake db:migrate
Update lib/spree/authentication_helpers.rb to use
current_refinery_user and Refinery's login/signup routes
module Spree module AuthenticationHelpers def self.included(receiver) receiver.send :helper_method, :spree_login_path receiver.send :helper_method, :spree_signup_path receiver.send :helper_method, :spree_logout_path receiver.send :helper_method, :spree_current_user end def spree_current_user current_refinery_user end def spree_login_path refinery.new_refinery_user_session_path end def spree_signup_path refinery.new_refinery_user_registration_path end def spree_logout_path refinery.destroy_refinery_user_session_path end end end ApplicationController.send :include, Spree::AuthenticationHelpers
Open http://localhost:3000/refinery and create an admin user for yourself. Then add yourself as a Spree admin:
rails console Refinery::User.first.spree_roles << Spree::Role.find_or_create_by_name("admin")
All done! Now you can access Refinery at http://localhost:3000/refinery and Spree at http://localhost:3000/admin