All posts filed under: rails

Product recommendations in Spree using the Jaccard Index

rails

Being able to recommend products to shoppers is a vital part of any online store. The “Customers Who Bought This Item Also Bought” section can lead to a lot of extra sales if done well. The Jaccard Index is a way of measuring similarity between items. Using some custom SQL we can extract the values we need: [crayon-5f9370a91c854007076216/] With these values we can then calculate the affinity between sold products: [crayon-5f9370a91c85c102170651/]  

Naming service classes in Rails

programming / rails

One of the core ideas of Rails is convention over configurations. Models go in app/models, controllers go in app/controllers and views go in app/views. The danger is that we stick to those conventions no matter what and we end up either with fat controllers, fat models or even worse a mixture of both. Many times we don’t take enough advantage of Ruby’s object oriented nature and the ability to extract functionality out into separate classes. Doing […]

Adding a custom Spree payment Gateway outside a Rails Engine

computing / programming / rails / spree

Adding a new Payment Gateway to Spree through a Rails Engine is pretty straight forward as you can hook in your new gateway after the initial payment gateway array has been created. This is how the spree_gateway gem does it: [crayon-5f9370a91e3e0906836684/] If you want to do the same thing for your own project contained gateway it’s a little different. If you try to just directly edit the payment_methods array in an initializer it will get […]

Rspec + Devise user and current_user different

computing / programming / rails

If you follow the Devise Wiki on setting up Rspec and Devise you may end up wanting to set an expecatation on the currently signed in user. Don’t fall into the trap of setting it on the original user model as your specs will still fail: user.should_receive(:blah).once # Won't work Rather, you should set it on the controller’s current_user: subject.current_user.should_receive(:blah).once # Will work!

Devise Omniauth Dynamic Providers

programming / rails

My current project www.viewshound.com requires different users to have different scopes for their Facebook authentication. Warden supports dynamic providers out of the box. To get it working with Devise was pretty easy, just a couple of minor changes were needed especially to support Facebook. First up was creating the new route. As I’m using a controller called Omniauth and not Session, the :to attribute is different. The other thing to note is I’m matching /users/auth/facebook/setup, […]

Paperclip S3 expiring_url with styles and SSL

programming / rails

A current requirement is that assets uploaded to Amazon S3 must be accessed via SSL and use an expiring URL. Using an expiring url with Paperclip is extremely easy. [crayon-5f9370a91e4c2112377664/] The problem is expiring_url doesn’t let you choose a style and doesn’t use https, which breaks our SSL site. To add the behaviour was an easy “hack”. I added the file /config/initializers/paperclip.rb with the following code: [crayon-5f9370a91e4c6815170643/] Now calling my expiring image URL with a […]

Rspec/Cucumber Autotest Loop

programming / rails

Autotest would constantly run my tests even though I hadn’t modified anything. Which made it impossible to actually use. Creating the following in a file called .autotest in my application’s root solved the problem for me. This is on Rails 3 RC, RSpec 2 beta 19 and Cucumber 0.8.5. [crayon-5f9370a91e528212883373/] [crayon-5f9370a91e52c591089577/]

Thoughtbot Clearance & mongomapper

rails

Having a play with mongomapper and wanted to get Clearance working with it. Seemed to just be a simple case of doing the following in my user model. [crayon-5f9370a91e57b805775941/] My full user model ended up looking like this: [crayon-5f9370a91e57f577261746/] Perhaps if I have time, I will look into adding attr_accessible support to mongomapper myself. Update 13/03/2010: I believe the dependency on attr_accessible in clearance has since been removed. So this small hack is most probably […]

Law of Demeter and the delegate method

computing / rails

The Law of Demeter, or Principle of Least Knowledge is a fairly simple design pattern, which, simply put means that an object should only talk to it’s immediate “friends” The law states that a method M of and object O may only invoke the methods of the following kind: 1. a method on O itself2. any parameters passed to M3. any objects instantiated within M4. any direct components of O The classic example coined by […]

rake spec fixtures failing

programming / rails

For some reason my rspec fixtures were not loading into my development database when doing spec:db:fixtures:load. An extra parameter to db:fixtures:load is all that’s needed. [crayon-5f9370a91e6b4241323816/] Fixed by: [crayon-5f9370a91e6b8489419208/]

Ruby on Rails

rails

So I began looking at Ruby on Rails a few days ago and I am quite impressed. To go from a database structure to a simple web app structure takes less then 15 minutes. Rails will generate a model and a controller for each of your database tables, or if you choose to generate a scaffold, it will generate all the supporting rhtml for you instead of generating it on the fly. I’ve got to […]