Spree Commerce

Try It Now

Polymorphic Caclulators

Posted on August 13, 2009 by Sean Schofield

There has been significant refactoring to the implementation of calculators. Calculators are now polymorphic and belong to calculable. This will have a non trivial impact on your existing store configuration. After upgrading to Spree 0.9.0 you are likely going to have to make several manual adjustments to the existing tax and shipping configurations. Ultimately we feel this is outweighed by the superior design of the new calculator system which will allow for a more modular design.

WARNING Many of the existing calculator extensions are not yet updated to support Spree 0.9.0. Please check the extension registry to see which versions are supported. Our goal is to backport most of the useful calculators shortly after the release.

All calculators need to implement the following method

def compute(something=nil)

The calculator is passed an optional "target" on which to base their calculation. This method is expected to return a single numeric value when the calculation is complete. A value of nil should be returned in the event that a charge is not applicable.

Calculators are also configurable thorugh Spree’s flexible preference system. Default values for the preferences are configured through the class definition. For example, the flat rate calculator class definition specifies an amount with a default value of 0.

class Calculator::FlatRate < Calculator
preference :amount, :decimal, :default => 0

Spree now contains a standard mechanism by which calculator preferences can be editied. The screenshot below shows how the amounts for the flat rate calculator are now editable directly in the admin interface.


"New Localizations: Dutch and Finnish"

Posted on July 31, 2009 by Sean Schofield

We’re please to announce two more localizations: Dutch and Finnish. The Dutch localization was contributed by Onno Schuit. The Finnish translation was generously contributed by Valafar. We are always on the lookout for new translations. If you are interested in contributing a patch, please read the guide on how to contribute.

Updating Searchlogic

Posted on July 30, 2009 by paulcc

Spree core now uses Searchlogic version 2.1.13 (unless a new version appears whilst I am writing). This has meant a few changes to how search and pagination are set up, but also allowed a few details of Spree to be improved at the same time. Extension developers will benefit too – the new gem is much more flexible, and easy to use. Marcin (Swistak) suggested that it might even go a fair way to providing the functionality we wanted in product groups, and I think he has a good point.

So, what’s new? Searchlogic has been completely rewritten in version 2, and uses a different approach to version 1. It now focuses more on key functionality and less on reinventing the wheel, e.g. control of pagination has passed back to the pagination gems. As a result, the code has gone fromĀ  ~2300 loc to ~420 loc. However, several useful bits of functionality have been added. For more information, see the github project and the rdoc documentation.

I think there’s two sides to this new version. Firstly, it provides a wide set of named scopes which you can use alongside existing ones. These are all dynamically generated, and able to follow model structure – including all associations, and to apply a range of tests to the fields identified. Here’s some examples from the documentation:

  • Within a model, filtering by various tests on fields:
    User.username_equals(&#8220;bjohnson&#8221;)<br />
    User.username_begins_with(&#8220;bjohnson&#8221;)<br />
  • Using associations (note the change of naming convention):
  • Scopes for ordering:
  • List modifiers, eg test on a list of values for any or all hits – this is very useful (and more efficient than doing it yourself)
    User.email_like_any([&#8220;joe&#8221;, &#8220;bill&#8221;])

Secondly, there’s the search aspect. This allows searches to be defined on top of some base scope, and customized in various ways. The search option names follow the same pattern as the named scopes above, and can be chained together or set separately. For example:

search = User.search(:username_equals =&gt; &#8220;bjohnson&#8221;, :age_less_than =&gt; 30)<br />
search.email_contains(&#8220;bjohnson&#8221;)<br />
search.age_gt = 20</p>

Gotcha #1: it isn’t possible in this version to over-ride the a search in the base scope by supplying options to the search, eg. check out the following example. It could be a bug.

scope = Product.ascend_by_master_price<br />
search = scope.search().order(&#8220;descend_by_master_price&#8221;)<br />

As for a chain of named scopes, you can get results out of a search with all, first, etc – or use some pagination library like will_paginate to handle limited fetches. See app/controllers/admin/product_controller.rb for an example of this in use.

Form handling is also much easier. There’s no need to include 'admin' in the submit path (it is detected automatically), and no need for fields_for to access associated records – you just need to adjust the field names to follow the new naming convention, eg for product search, you can search for variants_sku_contains to look for a substring in the sku field of any of the product’s variants. Particularly note how the check boxes have been brought within the searchlogic framework: the concepts represented by the check boxes map directly to one of the new-format searchlogic queries, so naming the box after the relevant condition is all you need to do.

Finally, if you are migrating from an earlier version, you need to run rake spree:upgrade and check that the will_paginate gem is listed in config/environment.rb, and also ensure that config/initializers/searchlogic.rb has been removed.

New Spree Stores (July 2009)

Posted on July 29, 2009 by Sean Schofield

Four brand new Spree sites have launched this month. Slouchback.com was built in just a few days and makes use of Amazon Flexible Payments Service as one of its interesting innovations. The site is also a little bit a head of the Spree core development since it has discounts and variant images (both of which are planned features for the next release.)

MovingBoxesForLess.com was also launched this month. The creator of this project was new to Spree but was able to build this site in under a week. Interesting side note, the site is hosted on Webbynode.

The Location VIP Montreal store launched this week. It lists cars for sale as well as rent. Spree can also be easily used for a small online craft business as seen in the newly launched Beca Designs shop.