More at spreecommerce.com: Features | Support | Blog | Demo | Community | Download

Working with the Source Code

This guide covers obtaining and running the source code. This is primarily for developers who are interested in contributing code to the Spree project or fixing the source code themselves. It is not necessary to have a copy of the source code to run Spree. This guide covers the following topics:

1 Git

The Spree source code is currently maintained in a Git repository. Git is a distributed version control system (DVCS) and it allows us to do things never before possible with SVN. If you’re skeptical of Git just remember how skeptical you were of SVN when you were using CVS. The differences are subtle but important and they are best understood by hands on use.

The authoritative git repository is hosted by GitHub and is located in the railsdog repository. You can clone the git repository using the following command:

$ git clone git://github.com/railsdog/spree.git

If you are planning on contributing to Spree you should create a fork through Git Hub and push fixes to clearly labelled branches (see the Contributors Guide for details.)

If the official GitHub repo is down you can try our mirror at repo.or.cz

1.1 Browsing the Repository and/or Downloading the Source Code

You can easily browse the repository through Git Hub’s excellent visual interface. Git Hub also contains a link to download a tarball copy of the latest source code as well as links to previous versions.

1.2 Git on Windows

There are some well developed Git clients for Windows now. If you are on a Windows box you might want to check out the msygit project. There is also a screencast available.

1.3 SVN

There are no immediate plans to support SVN clients. Since most of the Rails community is using git SVN support is not really a priority.

1.4 Monitoring Changes in the Source

If you would like to keep up to date on changes to the source you can subscribe to the github RSS feed and you will be notified of all the commits.

2 Bundler

Spree uses the very excellent bundler gem to manage its dependencies. We are assuming you have basic familiarity with bundler. A detailed explanation of bundler is beyond the scope of this guide.

You can install the gem dependencies using bundler as follows:

$ bundle install

The standard Gemfile.lock is also included in the source so you can be sure that you’re using the exact same versions of the gem dependencies used by the core team.

3 Layout and Structure

3.1 Collection of Gems

The Spree gem itself is very minimal and consists mostly as a collection of gems. These gems are maintained together in a single Github repository and new versions of the gems are shipped with each new Spree release. The official documentation (which you are reading now) covers functionality provided by each of these gems.|

Within the Spree source, each of the gems is organized into subdirectories as follows:

Gem Directory Description
spree_api api Provides REST API access
spree_auth auth Provides authentication and authorization (via authlogic and cancan gems)
spree-core core Core functionality - all other gems depend on this gem
spree_dash dash Simple overview dashboard
spree_promo promo Coupons and promotions
spree_sample sample Sample data and images

3.2 Use of Rails Engines

Each of the gems in Spree makes use of Rails Engines. This functionality was introduced in Rails 3.0 and allows Engines to behave in a manner similar to fully-functional applications. Relying on this mechanism provides several advantages:

3.2.1 An Intuitive Mechanism for Customization

Default Spree functionality is provided via the Rails engine gems. Engines can provides several aspects traditionally associated with a Rails application including (but not limited to):

  • Models, views and controllers
  • Routes
  • Helpers
  • Rake tasks
  • Generators
  • Locales

All of these elements can be overridden in the main Rails application. Therefore, it is relatively simple to add Spree to your Rails application and then customize it further by supplying your own elements in that same application. A full discussion of Rails Engines is not appropriate here. Please consult the Rails Guides for more information.

3.2.2 Simple Distribution and Installation as Gems

Using a Spree gem is as simple as adding it to your Gemfile (ie. very simple)

gem 'spree-core', '0.30.0'

Distribution of Spree (and its extensions) is also consistent with Rails standards and modularity (see next.)

3.2.3 Consistency With a Rails Standard

Prior to version 0.30.0, Spree used a complex custom mechanism for implementing “engine-like” functionality. While it was difficult to strip this functionality out of Spree the benefits are well worth it. Spree will now receive all of the massive testing and attention to detail that comes for free when using the Rails core.

3.2.4 Modularity

There are differing opinions on what belongs in the “core.” People are often expressing their opinion that Spree is either “getting too fat” or “lacks basic features.” By relying on these engines (and distributing them as gems), developers are free to use only the parts of Spree that they find useful. For instance, this would allow you to omit promotions functionality or to replace the authentication mechanism.

The engine architecture is a work in progress. Some of the modularity described above is theoretical and yet to be tested in a real world application. Expect minor difficulties if you decide to use a subset of the Spree gems (although we would gladly work with you to resolve them.)

4 Building a Sandbox Application

When working with the Spree source you may find yourself wanting to see how the code performs in the context of an actual application. This is due to the fact that Spree is intended to be distributed as a gem and is not designed to be run as a standalone application. Spree includes a helpful rake task for setting up such a test application.

$ rake sandbox

This will create a barebones rails application configured with the Spree gem. It runs the migrations for you and sets up the sample data. The resulting sandbox folder is already ignored by .gitignore and it is deleted and rebuilt from scratch each time the rake task runs.

5 Building the Gem from the Source

The Spree gem can easily be built from the source.

$ rake gem

Currently there is no shortcut for building all of the Spree gems from source with a single command (contributions welcome). Therefore, you will need to build them individually. For example:

$ cd core $ rake gem

6 Tips for Working with the Source

6.1 Using the “Edge” Code

If you are interested in simply using the latest edge code (as opposed to contributing to it) then the simplest thing to do is add a :git directive to your Gemfile and point it at the master branch.

gem 'spree', :git => 'git://github.com/railsdog/spree.git'

This will effectively use the latest code from the Git repository at the time you run bundle install. This version of the code will be “frozen” in your Gemfile.lock and will ensure that anyone else using your project code is using the exact same version of the Spree code as you are. You will need to update the bundle if you want to update to code that is newer since the last time you updated.

$ bundle update

Heroku now supports “git-based” gems. This means you can work with the latest version of Spree without having to build a gem or use git submodules (which were not supported.)

6.2 Developing on the “Edge”

If you plan on using the edge code but also contributing back to Spree, then you may be interested in the following approach. Create your Rails app that will be using the Spree gem in a directory that has the same parent as a locally cloned version of the Spree source. Then simply use the following in your Gemfile.

gem 'spree', :path => '../spree'

See the excellent bundler documentation for more details.