Mocked - a minitest pattern

Minitest is good for mocking, right? Well… Minitest is gaining a lot of popularity and can actually be a 100% replacement for RSpec. It’s a pure ruby testing framework, it’s fast, light weight, and it supports both a test-unit like syntax and a spec engine with Rspec like syntax. Still, when it comes to mocking, it can be a little painful. You have to initialize mocks and verify them manually after running the code under test....

May 11, 2016 · 3 min · metalelf0

Command pattern in ruby and rails

The problem If you have a growing Rails application and you feel your models are getting too fat you might have a problem. We’ve all been educated with the “fat models, thin controllers” dogma - but sometimes putting all the domain logic inside the models has its downsides. As an example, the typical flow of an ActiveRecord object through a Rails request involves: fetching the object from the DB based on the params you receive (controller); doing something with the object inside the model (model); when something goes wrong, you set errors onto the model attributes (model); you finally return the object to the view, and present it accordingly (view)....

May 2, 2016 · 4 min · metalelf0

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

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