Null objects in Rails

The problem Recently I’ve seen in a project I work on a lot of occurrences of this code: if user.privacy && user.privacy.enables_page?(...) The first part of the condition above is a bad practice in object oriented design. It forces collaborators of user to know a part of its implementation it could have a privacy or it couldn’t. What we want Wouldn’t it be much better to just write this:...

December 23, 2013 · 2 min · metalelf0

Fluentd usage example with bash and ruby

Fluentd is an open source tool to collect events and logs. Its architecture allows to easily collect logs from different input sources and redirect them to different output sinks. Some input examples are HTTP, syslog, or apache logs, and some output sinks are files, mail, and databases (both RDBMS and NoSQL ones). Also, it allows to parse logs and to extract only the significative parts from each of them; saving this structured information on a DB allows much easier log searching and analysis....

August 9, 2013 · 4 min · metalelf0

Easily change the path for your Paperclip attachments

Today after releasing an app to production environment I saw a couple of paperclip warnings like this in my production.log file: [paperclip] Duplicate URL for round_image with /system/:attachment/:id/:style/:filename. This will clash with attachment defined in PageElements::FranchisingCarouselEntry class This happens because I defined an attachment with the same name in two different models, and the default strategy Paperclip uses to choose attachment locations could lead to filename clashing. Here is a more detailed example:...

December 10, 2012 · 2 min · metalelf0

Vim regexp example: make a variable out of params

Today I wrote a regexp to change params[:page] into page. Here you are: :'<,'>s/params\[:\(\p\{-}\)\]/\1/g Let’s explain it briefly: the first part, :'<,'>s/, is the vim command to substitute a pattern (or a regexp) with another one. The <,'> part tells vim to operate on the visually selected text. the second part is the trickiest one. Let’s see it part to part: params\[: is the first part of the string we want to match....

July 9, 2012 · 2 min · metalelf0

Add bundle dir to your ctags

Ctags are a great way to improve navigation between large codebases. Used together with vim they allow to quickly jump to any method definition with just a keystroke - C-]. Adding your bundle dir when generating the tags file will allow jumping to the internals of the ruby gems you are using. Let’s see how to do this. The setup needed is the following: install Exuberant Ctags. I suggest using brew install ctags, and remember to fix your $PATH so that running ctags --version shows Exuberant Ctags....

March 26, 2012 · 2 min · metalelf0