When working on larger projects the RAILS_ROOT/app folder structure can be a little restrictive. Lately I’ve taken to adding more sub-directories into RAILS_ROOT/app like, observers, mailers and presenters (check out ActivePresenter and Presenter Pattern if you’ve never heard of it). It’s a simple trivial addition to the folder structure that allows me to keep my sanity. Instead of sifting through 40+ files in the models folder I can quickly drill down to what I’m looking for.
Luckily, Rails is nice enough to have a simple mechanism to do this. Open your environment.rb file and look for the lines that read:
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
Simply un-comment the second line and replace it with something like this:
# Add additional load paths for your own custom dirs
config.load_paths += %W( #{RAILS_ROOT}/app/clients #{RAILS_ROOT}/app/observers #{RAILS_ROOT}/app/presenters #{RAILS_ROOT}/app/mailers )
Very similar to moving the Ruby files around, I have also taken to moving some of the views around, specifically, mailer views. I like to keep them near the mailer model, and not in the RAILS_ROOT/app/views path.
This change is also quite easy thanks to the extensibility of the Rails framework. Near the end of your environment.rb file add the following line:
config.action_mailer.template_root = "#{RAILS_ROOT}/app/mailers"
I finally got around to using a mocking library today at work. I chose Mocha to start, no real reason, just the first name to pop into my head. After about 15 minutes (10 reading docs, and 5 coding) I am in love. Seriously, testing has never been this easy. What would have taken me at least 30 minutes to add new fixtures wihtout breaking old tests or screwing anything else up was easily solved in one to 2 lines per test case.
class SupportControllerTest < ActionController::TestCase
def test_applicant_activated_view
Applicant.any_instance.expects(:activated?).returns(:true)
post :create, :applicant => {:last_name => "Leapfrog", :email => "sos_platform@leapfrogqa.com"}
assert_select "ul.activated"
end
end
The first line in the above method mocks out the activated? method on any instance of the Applicant class. Isn’t that simple and clean? Without seeing the rest of the business logic I know it might be hard to accept why I need an entire library to help me with this simple boolean method, but trust me, activated? method is actually delegated to another model which is using an ActiveResource object to truly determine the result.
Recently an old friend, David Moffitt, asked me to tweak some Javascript I wrote for him and his company GTTS over a year ago. Looking at it again after such a long time was scary. The code was terrible. All functions, no classes, just a mess. I rewrote it for them as a Prototype based Class. Very clean and easy.
The Scroller object creates a simple news ticker that nicely rotates messages, stops when you mouse over, and continues when you mouse out. Yep, thats it. Check out my github project. View the comments in scroller.js for directions on how to use it.
Why the almost empty github project? The theory is I might one day write more Javascript and need a place to put it. So for now, only one file in that “project” but just you wait.
The Mack Framework is a pretty nifty combination of Rails, Merb and many other Ruby frameworks. It’s built off Rack and therefore should be simple to deploy. I really want to work with it, but what to do? Does anyone have any ideas? I need a project.
From a recent IM conversation with a good friend of mine:
Andrew Bloom
12:51 i hate working on shitty codebases
12:51 i wish i had time to do a major refactoring
Stephan Mokey
12:52 yeah, code is never done, it’s just due
12:52 we are artists
Recursion, every programmers favorite topic. Luckily that’s not what this is about, really. Today at work I was writing a Rails Generator that needed to created <a href=”http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/”>ERB</a> templates for views. It sounded pretty simple, but I hit one stumbling point. How do I make my template an ERB template that generates an ERB template of its own without interpreting all of the tags? After a bit of Googling I started to get discouraged. Then I remembered Rails already does some of this in places like the scaffold generator. I opened up the source and the templates and found an ERB tag I had never seen before <code><%%= do_something %></code>. There it was, the ‘%%’ will tell ERB to treat this as a tag for a future run. It will remove the second % and leave you with the output you expect.
Plugistrano is a little project I’ve been pondering for a while. My boss Jeff Cohen came up with the idea a few weeks ago. While in Portland at RailsConf 2008 I made some progress with it, thanks to the help of my coworker Mike.
Check out the github repo. Let me know what you think, fork it, fix it and make it better.
note: I have only tested this (very briefly) with Capistrano 1.4.1. It might not work with 2.0. I will try to test it with 2.0 later
Have you ever heard of reflows in terms of browser rendering? I hadn’t but this short blog post has some neat videos of them in action. Check it out, let me know what you think.
Tomorrow my coworker and I are getting on a plane to Portland for RailsConf. I’m pretty excited, we’ve both registered to take some extra classes Thursday. If any of you, my loyal readers are going to be at RailsConf send me an email or find me on twitter, as I’m going to need some people to hang out with while I’m away from home.
Lately I’ve been lacking any personal projects. To compensate I finding myself working on code for my employer (Leapfrog Online) during my free time. That’s not very fun, but my lack of creativity wasn’t able to provide a solution.
All of that changed tonight. For some unknown reason I suddenly came up with a project. I’m not going to say what the actual project is yet, but I will say I’ve been doing a lot of reading on the Arduino, and was thrilled to find this: RAD. I will probably end up ordering one of these starter kits when I get back from RailsConf in a week.
I went to RIT for Computer Engineering, and I’ve always loved working with hardware as a hobby. Since I’ve been out of school I really haven’t done much with my soldering iron or bit mask knowledge, mostly Rails work, so this will be a fun change.