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 yourconfig/initializers/spree.rb file, you can set the following:
- Inherit from
Spree::Cart::AddItem - Override the
callmethod to add your custom logic - Call
run :add_to_line_itemto add the item to the cart - Call
run Spree.cart_recalculate_serviceto recalculate the cart (returns the resolved class) - Call
run :update_in_external_systemto 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 theSpree module:
Controller level customization
If you need to replace serializers or Services in a specific API endpoint you can create a code decorator: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:
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
grep to filter results:
Show only overridden dependencies
Validate all dependencies
Programmatic introspection
You can also inspect dependencies programmatically:Backwards compatibility
The legacy string-based syntax is still supported for backwards compatibility:Default values
Default values can be easily checked by:- Using the rake task:
bin/rake spree:dependencies:list - Looking at the source code:

