Rails New Features and TrAvid

So, I was asked recently what kind of new features I used for my website TrAvid. After some reflection, I come up with the following list. I’ve decided to share the list with other Rails developers.

named_scope

I use vote_fu, but it doesn’t provide which user liked(voted up) or disliked which story, so I implemented the following named_scope for vote model:

#vote.rb
named_scope :for_voter_liked, lambda { |*args| {:conditions => ["voter_id = ? AND voter_type = ? AND vote = TRUE", args.first.id, args.first.class.name]} }
named_scope :for_voter_disliked, lambda { |*args| {:conditions => ["voter_id = ? AND voter_type = ? AND vote = FALSE", args.first.id, args.first.class.name]} }

And for user model, I have
# user.rb
def liked
liked = Vote.for_voter_liked(self)
liked.each do |voteable|
v_type = voteable.voteable_type
v_id = voteable.voteable_id
model = Vote.find_voteable(v_type, v_id)
instance_var = v_type.downcase.pluralize
instance_eval <<-EOT
if instance_variable_get("@#{instance_var}").nil?
instance_variable_set("@#{instance_var}", [])
end
instance_variable_get("@#{instance_var}").push(model)
EOT
end
end

Cache in 2.1

I used the new caching capability provided by Rails 2.1 for retrieving information for the featured destinations:

# city.rb
def City.popular_cities
Rails.cache.fetch('popular_cities') { City.fetch_popular_cities }
end

def City.fetch_popular_cities
City.all.find_all{|city| city.vote_score > CUT_OFF_LINE}
end

rescue_from

I use rescue_from to handle exceptions.

# application.rb
rescue_from ActiveRecord::RecordNotFound, :with => :redirect_if_not_found

def redirect_if_not_found
render :file => RAILS_ROOT+'/public/404.html', :status => 404
end

counter_cache

I use counter_cache for active record.

# city.rb
belongs_to :country, :counter_cache => true

on enhancing polymorphic_url

So, Rails provide edit_polymorphic_url and edit_polymorphic_path helper methods. But for my website, I have some models which can benefit from the *_polymorphic_url helpers. So, I implemented the following helper:

%w(bookmark add_tags).each do |action|
module_eval < "#{action}")
end

def #{action}_polymorphic_path(record_or_hash)
polymorphic_url(record_or_hash, :action => "#{action}", :routing_type => :path)
end
EOT
end

This allows me to call helper methods such as bookmark_polymorphic_path(@country, @city, @attraction). And from maintenance point of view, adding any more path like these would be very easy.

I hope this run down helps.

Intoducing TrAvid

For some, vacations provide a chance to escape the reality and to venture into the unknown.  But the unknown can sometimes be overwhelming and, at times, intimidating: planning, scheduling, budgeting, exploring and safety.

TrAvid begins with a simple idea—travel planning should not be the same as researching for a school paper. TrAvid provides quick-to-find information for a destination. Information such as restaurants, hotels, local cuisines, attractions, tours, stories and even movies filmed in a city! The information is contributed, voted and discussed among TrAvid users.

For more information, please visit http://travid.info/about.