Learn how to create a Spree extension.
spree_simple_sales
directory with several additional files and directories. After generating the extension make sure you change to its directory:
db/migrate/XXXXXXXXXXX_add_sale_price_to_spree_variants.rb
so that it contains the following:
my_store
application directory, add the following line to the bottom of our Gemfile
:
my_store
application.
Once you have added the gem, it’s time to bundle:
spree_simple_sales
install generator to copy over the migration we just created (answer yes if prompted to run migrations):
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:
home_controller_decorator.rb
and add the following content to it:
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:
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:
sale_price
when it is present.
First, create the required directory structure for our new decorator:
app/models/spree_simple_sales/spree/variant_decorator.rb
and add the following content to it:
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
).
variant_decorator.rb
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:
rspec
and see the following output:
variant_decorator_spec.rb
and add the following tests to it:
price_in
method we overrode in our VariantDecorator
returns the correct price both when the sale price is present and when it is not.