Skip to main content

Overview

With Dependencies, you can easily replace parts of Spree core with your custom code. You can replace Services, CanCanCan Abilities (used for Permissions), and API Serializers (used for generating JSON API responses).

Application (global) customization

This will change every aspect of the application (both APIs, Admin Panel, and Storefront). In your config/initializers/spree.rb file, you can set the following:
or using the block syntax:
Now let’s create your custom service.
And add the following code to it:
This code will:
  1. Inherit from Spree::Cart::AddItem
  2. Override the call method to add your custom logic
  3. Call run :add_to_line_item to add the item to the cart
  4. Call run Spree.cart_recalculate_service to recalculate the cart (returns the resolved class)
  5. Call run :update_in_external_system to execute your custom logic, eg. updating Order in an external system such as ERP

Using dependencies in your code

When you need to use a dependency in your code, you can access it directly via the Spree module:

Controller level customization

If you need to replace serializers or Services in a specific API endpoint you can create a code decorator:
and add the following code to it:
This will change the serializer in this API endpoint to MyNewAwesomeCartSerializer and also it will swap the default add_item_service to MyNewAwesomeAddItemToCart. Different API endpoints can have different dependency injection points. You can review their source code to see what you can replace.

API level customization

Storefront API and Platform API have separate Dependencies injection points so you can easily customize one without touching the other. In your Spree initializer (config/initializers/spree.rb) please add:
This will swap the default Cart serializer and Add Item to Cart service for your custom ones within all Storefront API endpoints that use those classes. You can mix and match both global and API-level customizations:
The second line will have precedence over the first one, and the Storefront API will use AnotherAddItemToCart and the rest of the application will use MyNewAwesomeAddItemToCart.

Debugging dependencies

Spree provides rake tasks to help you debug and inspect dependencies:

List all dependencies

This will output all dependencies with their current values:
You can use grep to filter results:

Show only overridden dependencies

This shows only the dependencies that have been customized, along with their original and current values:

Validate all dependencies

This validates that all dependencies can be resolved to valid classes. If any dependency points to a non-existent class, it will report an error:

Programmatic introspection

You can also inspect dependencies programmatically:

Backwards compatibility

The legacy string-based syntax is still supported for backwards compatibility:
Both syntaxes can coexist, but the new syntax is recommended as it’s more concise and provides better error messages at assignment time.

Default values

Default values can be easily checked by:
  1. Using the rake task: bin/rake spree:dependencies:list
  2. Looking at the source code: