How to create new Report extension in Spree 0.70.x

In this tutorial, I will show how to create new Report as extension for Spree 0.70.x backend from scratch. The idea is to generate a new extension with our new Report logic and register it by having the function to be merged with the constant AVAILABLE_REPORTS of the Admin::ReportsController. No rocket-science here, it’s pure basic RoR coding, and I hope I could show you a thing or two onto how to write your own extension for Spree.


It is required that Spree 0.70.x is installed on your computer.

$ gem install spree --version=0.70.2

Generate new extension

Thanks to Spree generator, we could generate a new extension for Spree easily by using spree command. Let’s create our cool FooReport:

$ spree extension FooReport

Spree generates a skeleton mountable engine with name ‘spree_foo_report’:

      create  spree_foo_report
      create  spree_foo_report/app
      create  spree_foo_report/app/assets/javascripts/admin/spree_foo_report.js
      create  spree_foo_report/app/assets/javascripts/store/spree_foo_report.js
      create  spree_foo_report/app/assets/stylesheets/admin/spree_foo_report.css
      create  spree_foo_report/app/assets/stylesheets/store/spree_foo_report.css
      create  spree_foo_report/app/controllers
      create  spree_foo_report/app/helpers
      create  spree_foo_report/app/models
      create  spree_foo_report/app/views
      create  spree_foo_report/app/overrides
      create  spree_foo_report/config
      create  spree_foo_report/db
      create  spree_foo_report/lib
      create  spree_foo_report/lib/spree_foo_report.rb
      create  spree_foo_report/lib/spree_foo_report/engine.rb
      create  spree_foo_report/lib/generators/spree_foo_report/install/install_generator.rb
      create  spree_foo_report/script
      create  spree_foo_report/script/rails
      create  spree_foo_report/spec
      create  spree_foo_report/LICENSE
      create  spree_foo_report/Rakefile
      create  spree_foo_report/
      create  spree_foo_report/.gitignore
      create  spree_foo_report/spree_foo_report.gemspec
      create  spree_foo_report/Versionfile
      create  spree_foo_report/config/routes.rb
      create  spree_foo_report/spec/spec_helper.rb
      create  spree_foo_report/.rspec
      append  Gemfile

Customize extension


Thanks to greatness of mountable engine brought by Rails 3.1, now we could have our Spree extension deployed as gem and easily distributed with everyone. We need to provide details for our gem. Change the file spree_foo_report.gemspec:

# encoding: UTF-8 do |s|
  s.platform    = Gem::Platform::RUBY        = 'spree_foo_report'
  s.version     = '0.1'
  s.summary     = 'The Foo Man Chew Report'
  s.description = 'All the secret of Universe'
  s.required_ruby_version = '>= 1.8.7'            = 'Foo Man Chew'             = ''
  s.homepage          = ''

  s.files         = `git ls-files`.split("\n")
  s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
  s.require_path = 'lib'
  s.requirements << 'none'

  s.add_dependency 'spree_core', '>= 0.70.2'
  s.add_development_dependency 'rspec-rails'

If you are writing a public extension, the best practice is to have your GitHub URL as the homepage.


In the scope of this tutorial, our extension is written for Spree 0.70.x. However if future version is released which might break our current codebase, we could easily separate our code into Git branches or taggings. So Versionfile comes in handy, it designate the compatibility with different version of Spree . Read more at Change our Versionfile to:

# This file is used to designate compatibility with different versions of Spree
# Please see for details

"0.70.x" => { :branch => "0-70-stable"}


README.mdfile is used to describe your extension. Now we add some installation and testing details:

Spree Foo Report

Fooo foo fooo fooo .. M..AN..CHEW!!!


Append to your Gemfile:

    gem 'spree_foo_report', :git => 'git://'


Be sure to add the rspec-rails gem to your Gemfile and then create a dummy test app for the specs to run against.

    $ bundle exec rake test app
    $ bundle exec rspec spec

Copyright (c) 2011 Foo Man Chew, released under the New BSD License

ReportsController Decorator

We extends the Spree Core ReportsController by adding a new file in app/controllers/admin/reports_controller_decorator.rb:

Admin::ReportsController.class_eval do
  AVAILABLE_REPORTS.merge({:foo_report => {:name => I18n.t(:foo_name), :description => I18n.t(:foo_description)}})

  def foo_report
    # Add your logic here

By having our new foo_report added in AVAILABLE_REPORTS, this ensure our new report to be listed on admin/reports index page. Yet, we are not done, we need to add in translation string for foo_name and foo_description by adding those 2 into config/locales/en.yml:

  foo_name: The Foo Report
  foo_description: The You Know All Report


But what’s good for a report with no visual presentation? Let’s add in our View at app/views/admin/reports/foo_report.html.erb. Please note that the view name should be the same as the function name.

<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</h1>


We’re nearly there, the last thing is to configure the routing. Edit config/routes.rb:

Rails.application.routes.draw do
  match '/admin/reports/foo_report' => 'admin/reports#foo_report', :via => [:get, :post]


There are 2 ways to try out our work, first is to build our gem and have it included in sandbox’s Gemfile OR we could commit the whole extension to GH and configure our Gemfile to use git path.

About Jones Lee

Nothing much about me..

5 responses to “How to create new Report extension in Spree 0.70.x

  1. Simon

    Hi –

    I’m very new to Rails and Spree, so forgive my dumb question. I’ve been trying out some customizations, and was trying to follow your article.

    My basic procedure has been:
    – create a brand new, empty rails app called “myApp”
    – install spree via myApp’s Gemfile

    I can startup my application and I have access to the backend; I’ve loaded the Spree sample data, so I can see the sample “Sales total” report.

    Using the content of your article, I think I should be able to add the reports_controller_decorator.rb to myApp/app/controllers/admin. However, no matter what I’ve tried I keep getting the same error:

    in ‘block in ‘; uninitialized constant AVAILABLE_REPORTS (NameError)

    I guess this implies my decorator is being ‘seen’ and loaded ok; do you know why the original definition of AVAILABLE_REPORTS is not being found?

    I feel I’m at the first hurdle with Spree and would really appreciate any help. Thank you.

    • Can you please paste the code to GitHub gist?

      • Simon

        Hi –

        Thanks for getting back to me. I had a look on the Spree mailing list and saw some references to ‘Admin::ReportsController::AVAILABLE_REPORTS’. So I changed my code to use this full namespace and the server now starts up. I am making some progress now 😉

        Next stop: Deface

        thanks again

  2. I haven’t read the article completely, but I did notice a typo in the title. You have “Extesion” and I think you mean “Extension” 🙂

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: