Posts Tagged ‘ruby’

A voting extension from scratch for Rails 3 (Part 2 – Testing environment)

In the last episode we created the basic structure for the make_voteable gem. With “test first” in mind we now set up our RSpec testing environment for developing the make_voteable gem. We already installed RSpec at the beginning of this blog series. First we need to create a testing folder (named spec) directly under the [...]

A voting extension from scratch for Rails 3 (Part 1 – Overview and setup)

This blog series is about creating a voting extension for Ruby On Rails. There are already some others voting extensions available, but none is currently up-to-date 1 or ActiveRecord compatible 2. So let’s build one from scratch. It is also (or maybe even more) a blueprint of current best practices on how to build those [...]

Check your params

I just asked myself (and the community) where validating and cleaning up the GET/POST parameters would fit best in Rails. lebreeze at Stackoverflow suggested a sanitize_params method that is called by a before_filter in the ApplicationController. I agree that the ApplicationController is a good place, but I don’t like the fact that it is called [...]

Index external models with Sunspot/Solr

Lately I wanted to index all tags of ActsAsTaggableOn for full text search and autosuggestions using Solr resp. Sunspot. The ActiveRecord models of ActsAsTaggableOn are unfortunately not directly accessible as they are located in the ActsAsTaggableOn gem. Fortunately that is pretty easy to solve by using metaprogramming techniques. So I added an additional initializer named [...]

Intend to extend (metaprogramming in Ruby)

In this post I want to illustrate the different ways of how to extend Ruby classes and instances. As being a former Java developer it is always impressive, but also sometimes a bit confusing how many ways are available in Ruby to achieve that. There is already a lot written about this topic (see resources [...]

Update attributes and OpenStruct

Rails does have the conveniet method update_attributes to update one or more attributes of an ActiveRecord instance. Sometimes this is also very handy for standard Ruby objects and pretty easy to implement: class MyClass attr_accessor :foo, :bar   def update_attributes(hash) hash.each_pair do |key, value| begin self.send("#{key}=", value) rescue # do something when an attribute does [...]