Developer Tips and Tricks
This guide presents accumulated wisdom from person-years of Spree use.
The important commands
was removed in favor of Bundler.
Before updating, you will want to ensure the installed spree gem is
up-to-date by modifying to match the new spree version and
Thanks to Rails 3.1 Mountable Engine, the update process is “non-destructive”
than in previous versions of Spree. The core files are encapsulated separately from
sandbox, thus upgrading to newer files will not override nor replace
sandbox’s customized files.
This makes it easier to see when and how some file has changed – which is often
useful if you need to update a customized version.
Dos and Don’ts
NOTE: Try to avoid modifying and : use initializers instead.
Tracking changes for overridden code
Be aware that core changes might have an impact on the components you have overridden in your project.
You might need to patch your local copies, or ensure that such copies interact correctly with changed
If you can help us generalise the core code so that your preferred effect is achieved by altering
a few parameters, this will be more useful than duplicating several files. Ideas and suggestions are always
Initializers are run during startup, and are the recommended way to execute certain settings. You can
put initializers in extensions, thus have a way to execute extension-specific configurations.
See the extensions guide for more information.
Use and to test basic functioning after you’ve made changes.
Analysing crashes on a non-local machine
If you’re testing on a server, whether in production or development mode, the following code in one
of your files might save some time. It triggers local behaviour for users who have
an admin role. One useful consequence is that uncaught exceptions will show the detailed error page
instead of , so you don’t have to hunt through the server logs.
ENV["RAILS_ENV] != “production” || current_user.present? && current_user.has_role?(:admin)
Managing large projects
To fork or not to fork…
Suppose there’s a few details of Spree that you want to override due to personal or client preference,
but which aren’t the usual things that you’d override (like views) – so something like tweaks to the
models or controllers.
You could hide these away in your site extension, but they could get mixed up with your real site
customizations. You could also fork Spree and run your site on this forked version, but this can
also be a headache to get right. There’s also the hassle of tracking changes to and
pulling them into your project at the right time.
So here’s a compromise: have an extra extension, say , to contain your small collection
of modified files, which is loaded first in the extension order. The benefits are:
- it’s clear what you are overriding, and easier to check against core changes
- you can base your project on an official gem release or a commit stage
- such tweaks can become part of your client site project and be managed with SCM etc.
If you find yourself wanting extensive changes to core, this technique probably won’t work so well.
But then again, if this is the case, then you probably want to look seriously at splitting some
code off into stand-alone extensions and then see whether any of the other code should be contributed
to the core.
Setting up submodules
Some of us use Git submodules
to tie large projects together: the basic Spree shell plus the
extension is the main repository, and everything else is loaded in as a submodule,
including Spree itself. See this Spree-demo fork
for an example.
The basic command to set up a submodule is this. Call it for each of the submodules you need.
For Spree, you want to use the path .
git submodule add some_repo [-b branch] vendor/extensions/some_ext
If your project is public, then you probably want to give the public version of your repo so
other people can use it without change. When I need to change a submodule in place, I add a
new remote with the proper URL for changes to be pushed to.
The branch option is recommended (and you probably want to specify “master” most of the time).
Checking out submodules
On the first run, the following command fetches the submodule repositories into the nominated
git submodule update —init
is very useful for managing your submodules. For example, you can update all of them
with , or check for modifications with this
(the gets round a return-code issue):
git submodule foreach “git status || echo ok”
When pushing modifications, remember to check each of your repositories (with above),
and then check in the main repository. Git tracks the last commit in each submodule and includes
this information in the main repository, hence allowing the right versions to be checked out
NOTE: seems to be a recent addition, so you might need to update your installation of .