Skip to content

Instantly share code, notes, and snippets.

@jagdeepsingh
Last active July 12, 2017 05:15
Show Gist options
  • Save jagdeepsingh/3d63d65c6a80bd8ebdc3a229766d1af9 to your computer and use it in GitHub Desktop.
Save jagdeepsingh/3d63d65c6a80bd8ebdc3a229766d1af9 to your computer and use it in GitHub Desktop.
Spree

Spree

Spree is a complete open source ecommerce solution for Ruby on Rails.

Links:

Contents:

Install dependencies

$ gem install rails -v 5.0.2

$ gem install bundler
$ bundler -v
Bundler version 1.15.1

$ brew -v
Homebrew 1.2.3
Homebrew/homebrew-core (git revision 0818; last commit 2017-06-19)

$ brew update

$ brew install imagemagick
Installed version 7.0.6-0 of imagemagick

$ mysql --version
mysql  Ver 14.14 Distrib 5.7.16, for osx10.11 (x86_64) using  EditLine wrapper

Back to top

Setup application

$ rails _5.0.2_ new mystore -d mysql
$ cd mystore

Add following to Gemfile:

gem 'spree', '~> 3.2.1'
gem 'spree_auth_devise', '~> 3.2.0'
gem 'spree_gateway', '~> 3.2.0'

Run bundle install.

$ rake db:create
$ rails g spree:install
Admin User Email: spree@example.com
Admin User Password: spree123

$ rails g spree:auth:install

$ rails g spree_gateway:install
$ rails server

Now visit http://localhost:3000 to view the product store and http://localhost:3000/admin to access the admin end.

Back to top

Install extension

We will install spree extension spree_i18n for an example here.

# Gemfile
gem 'spree_i18n', github: 'spree-contrib/spree_i18n', branch: ‘master'

Run bundle install.

$ bundle exec rails g spree_i18n:install

Back to top

Create extension

Go to directory outside your Spree application.

We are creating an extension named spree_simple_sales here.

$ spree extension simple_sales
$ cd spree_simple_sales
$ bundle install

Modify database

$ bundle exec rails g migration add_sale_price_to_spree_variants sale_price:decimal    # OR whatever

Modify model

# app/models/spree/variant_decorator.rb
module Spree
  Variant.class_eval do
  end
end

Modify controller

# app/controllers/spree/home_controller_decorator.rb
module Spree
  HomeController.class_eval do
    def sale
    end
  end
end

Add routes

# config/routes.rb
Spree::Core::Engine.routes.draw do
  get '/sale' => 'home#sale'
end

Add views

Create file app/views/spree/home/sale.html.erb and add the view content.

Modify existing views

# app/overrides/add_sale_price_to_product_edit.rb
Deface::Override.new(virtual_path: 'spree/admin/products/_form',
  name: 'add_sale_price_to_product_edit',
  insert_after: "erb[loud]:contains('text_field :price')",
  text: "
    <%= f.field_container :sale_price do %>
      <%= f.label :sale_price, raw(Spree.t(:sale_price) + content_tag(:span, ' *')) %>
      <%= f.text_field :sale_price, value:
        number_to_currency(@product.sale_price, unit: '') %>
      <%= f.error_message_on :sale_price %>
    <% end %>
  ")

Use the extension in application

Add the following to Gemfile and run bundle install:

gem 'spree_simple_sales', path: 'path/to/extension’
$ rails g spree_simple_sales:install

Visit http://localhost:3000/sale and you should see the new page created in the extension.

Back to top

Build guides

$ cd spree/guides/
$ bundle install
$ nanoc autocompile

Now, spree guides will be available at http://localhost:3000/.

Back to top

References

Back to top

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment