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

Developer Tips and Tricks

This guide presents accumulated wisdom from person-years of Spree use.

1 Upgrade Considerations

1.1 The important commands

spree --update will copy in any new “infrastructure” files, e.g. new configuration or javascript or style files.

Before updating, you will want to ensure the installed spree gem is up-to-date. For example, if you are using Spree edge, then get the latest edge source, and run rake spree:gem:install.

Once you have the latest spree gem installed, run spree --update from inside RAILS_ROOT (your application directory) and your application will be upgraded.

The update process is less “destructive” than in previous versions of Spree. Instead of silently replacing crucial files in your application, Spree now checks the content of files it needs to replace, and if the old version differs, it will be saved with a ~ suffix.

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. The update command will also no longer copy the routes.rb file – the original version just loads the core Spree routes file, so has no need to change. (Recall that you can define new routes in your extensions.)

1.2 Dos and Don’ts

Try to avoid modifying config/boot.rb and config/environment.rb: use initializers instead.

1.3 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 code (e.g. using appropriate ids in Html to allow the javascript to work).

If you can help us to generalise the core code so that your preferred effect is achieved by altering a few parameters, then this is more useful than duplicating several files. Ideas and suggestions always welcome.

The Spree Wiki contains a few scripts to automate some of the checking.

1.4 Initializers

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.

2 Debugging techniques

2.1 Use tests!

Use rake spec and rake test to test basic functioning after you’ve made changes.

2.2 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 FOO_extension.rb 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 404.html, so you don’t have to hunt through the server logs.

Spree::BaseController.class_eval do def local_request? ENV["RAILS_ENV] != "production" || current_user.present? && current_user.has_role?(:admin) end end

3 Managing large projects

3.1 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. Your 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 railsdog/master and pulling them into your project at the right time.

So here’s a compromise: have an extra extension, say spree-tweaks, 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 railsdog/master 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.

3.2 Setting up submodules

Some of us use Git submodules to tie large projects together: the basic Spree shell plus the site/ 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 vendor/spree.

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).

3.3 Checking out submodules

On the first run, the following command fetches the submodule repositories into the nominated directories.

git submodule update --init

3.4 Managing submodules

git submodule foreach is very useful for managing your submodules, e.g. you can update all of them with git submodule foreach "git pull", or check for modifications with this (the echo ok gets round a return-code issue):

git submodule foreach "git status || echo ok"

When pushing modifications, remember to check each of your repositories (with foreach 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 later.

git foreach seems to be a recent addition, so you might need to update your installation of git.