posted on Tuesday, February 08, 2011
In one of my projects I had to use I18n and I had a problem always seeing something like:
translation missing: ru.activerecord.errors.messages.record_invalid when there is no translation found.
I prefer to see default English translation if there is a translation missing error, why not ?
Especially in development mode.
So here is a small piece of code that you can put into config/initializers folder:
module I18n
def self.custom_handler(exception, locale, key, options)
case exception
when I18n::MissingTranslationData
I18n::Backend::Simple.new.translate(:en, key, options || {})
else
raise exception
end
end
end
I18n.exception_handler = :custom_handler
Continue reading
posted on Tuesday, December 14, 2010
There is an easy way to add view path dynamicly inside ActionController using prepend_view_path or append_view_path. But there is a problem because each request will start a filesystem traversal to do a pathset, we don't want to do that especially if there is a big project.
So here is an idea of how to handle that view_path insertion...
Continue readingposted on Tuesday, September 14, 2010
Once i started searching and subscribing to feeds, my Google Reader was rapidly filled up with all that shit (kidding), i meant interesting news posts. In order to make Google Reader easier to use and to make sure that i'm reading feeds that are important to me i have to get it organized. It seems very easy with all these tools in Google Reader, but in reality i tried many times and still missing lots of interesting posts from the feeds, maybe i just don't have time to read all that super organized mess ))
Reading blogs helps us remain valuable in our space. Sometimes it's hard to find time but there are ways to make that time efficient and once you got an organized list of favorite news sites and blogs into your reader, you are done.
I organize my feeds by how important is not to miss the content from each particular feed. So first typically are blogs of friends and people that I like to follow, people which don't generate posts with the same speed as new york times, so reading this first part takes maybe 20 minutes a day. Second part of feeds contains feeds of blogs I like, but don’t need to read every day. When I find a new interesting blog to follow, I will typically put it here until i'll decide where it will go. Third part of feeds are items that i rarely read. Typically, i'll read these items only if there is nothing to read and i don't wanna go to search something in the web, so i read it even rarer than fourth part of my feeds which makes more sense because here goes the fun stuff. So in fourth part i have those feeds that i read after work at the end of the day when my brain is already broken and can not even think, and all that remains is laugh out loud.
Continue readingposted on Tuesday, June 29, 2010
This will increment "views" attribute of the post and save it to database, increment!(attribute, by=1):
@post.increment!(:views)
This will decrement "views" attribute of the post and save it to database, decrement!(attribute, by=1):
@post.decrement!(:views)
This will assign to attribute the boolean opposite of it:
@post.published # -> true
@post.toggle!(:published)
@post.published # -> false
class PostsController < ApplicationController
before_filter :find_post, :only => [:publish, :show]
def show
@post.increment!(:views)
end
private
def find_post
@post = Post.find(params[:id])
end
end
Reloading attributes from database, to get the latest values, reload(options = nil):
@post = user.posts.find(5)
user.posts.find(5).increment!(:views)
@post.views # -> 0
@post.reload
@post.views # -> 1
Reordering of already defined scope:
Post.scoped.collect {|p| p.id }
- 1
- 2
- 3
Post.scoped.reverse_order.collect {|p| p.id }
- 3
- 2
- 1
Continue reading
posted on Thursday, June 24, 2010
Simple fragment caching example:
<% cache do %>
Something here ...
<% end %>
Or if you want to specify action name and suffix of this cache block:
<% cache(:action => 'index', :action_suffix => 'all') do %>
Something here ...
<% end %>
Maybe it's not the best way to handle cached fragment names but i prefer using this kind of key:
<% cache('top#posts#') do %>
top posts block content ...
<% end %>
where 'top' is just a key of this cached fragment, but #posts# is used as identifier for the type of content, so for example we have 3 cached blocks, and they are not related to one controller or action:
When new post was created or deleted, instead of specifying expire_fragment for each cache key, you just add one line to your sweeper or observer or wherever you specify your expiring:
expire_fragment %r{#posts#}
And after that all cached blocks that related to posts are expired, its usefull when you have lots of fragments which are loaded from different controllers and actions and placed all over the website. The result of this is you'll never mess up with key names of your fragments, and will be sure that your cache is always up to date.
Continue reading