Creating an Extension
Learn how to create a Spree extension.
Overview
Spree Extensions are a way to add new functionality to your Spree store. They are a great way to extend the functionality of Spree and add new features. You can share them with Spree community on Github so anyone can use them, contribute back and share your improvements.
Getting Started
Let’s build a simple extension. Suppose we want the ability to mark certain products as being on sale. We’d like to be able to set a sale price on a product and show products that are on sale on a separate products page. This is a great example of how an extension can be used to build on the solid Spree foundation.
Before we start, let’s make sure we have Spree CLI installed by running:
So let’s start by generating the extension. Run the following command from a directory of your choice outside of our Spree application:
This creates a spree_simple_sales
directory with several additional files and directories. After generating the extension make sure you change to its directory:
Adding a Sale Price to Variants
The first thing we need to do is create a migration that adds a sale_price column to variants.
We can do this with the following command:
Because we are dealing with prices, we need to now edit the generated migration to ensure the correct precision and scale. Edit the file db/migrate/XXXXXXXXXXX_add_sale_price_to_spree_variants.rb
so that it contains the following:
Adding Our Extension to the Spree Application
Before we continue development of our extension, let’s add it to the Spree application.
Within the my_store
application directory, add the following line to the bottom of our Gemfile
:
You may have to adjust the path somewhat depending on where you created the extension. You want this to be the path relative to the location of the my_store
application.
Once you have added the gem, it’s time to bundle:
Finally, let’s run the spree_simple_sales
install generator to copy over the migration we just created answer yes if prompted to run migrations:
Adding a Controller Action to HomeController
Now we need to extend Spree::HomeController
and add an action that selects “on sale” products.
Note for the sake of this example that `Spree::HomeController` is only included in spree_frontend so you need to make it a dependency on your extensions *.gemspec file.
Make sure you are in the spree_simple_sales
root directory and run the following command to create the directory structure for our controller decorator:
Next, create a new file in the directory we just created called home_controller_decorator.rb
and add the following content to it:
This will select just the products that have a variant with a sale_price
set.
We also need to add a route to this action in our config/routes.rb
file. Let’s do this now. Update the routes file to contain the following:
Viewing On Sale Products
Setting the Sale Price for a Variant
Now that our variants have the attribute sale_price
available to them, let’s update the sample data so we have at least one product that is on sale in our application. We will need to do this in the rails console for the time being, as we have no admin interface to set sale prices for variants. So, in order to do this, first open up the rails console:
Now, follow the steps I take in selecting a product and updating its master variant to have a sale price. Note, you may not be editing the exact same product as I am, but this is not important. We just need one “on sale” product to display on the sales page.
Decorating Variants
Let’s fix our extension so that it uses the sale_price
when it is present.
First, create the required directory structure for our new decorator:
Next, create the file app/models/spree_simple_sales/spree/variant_decorator.rb
and add the following content to it:
If there is a sale_price
present on the product’s master variant, we return that price. Otherwise, we call the original implementation of price_in
using return super
.
Testing Our Decorator
It’s always a good idea to test your code. We should be extra careful to write tests for our Variant decorator since we are modifying core Spree functionality. Let’s write a couple of simple unit tests for variant_decorator.rb
Generating the Test App
An extension is not a full Rails application, so we need something to test our extension against. By running the Spree test_app
rake task, we can generate a barebones Spree application within our spec
directory to run our tests against.
We can do this with the following command from the root directory of our extension:
After this command completes, you should be able to run rspec
and see the following output:
Great! We’re ready to start adding some tests. Let’s replicate the extension’s directory structure in our spec directory by running the following command
Now, let’s create a new file in this directory called variant_decorator_spec.rb
and add the following tests to it:
These specs test that the price_in
method we overrode in our VariantDecorator
returns the correct price both when the sale price is present and when it is not.
Summary
In this tutorial, you learned how to both install extensions and create your own. A lot of core Spree development concepts were covered and you gained exposure to some of the Spree internals.
Was this page helpful?