Adding Automatic Code Reloading to Rails Console

So I have no idea why the rails console in development mode doesn’t already do this, since the whole idea of the console in development mode is for us to mess around and test random stuff in development mode, right? Anyway, I got inspired and helped by Jkfill with his post on Automatic Code Reloading in Rails Console, so here goes, more for my records than yours since you should follow the instructions in his post. I am using Rails 3.2.9, so I had to copy the code from the reload! method in the rails console code, which is not exactly the best practice, but it’s just for dev mode and it works :-)

 #this should be in config/initializers/irb_reloading.rb
    if defined?(IRB::Context) && !defined?(Rails::Server) && Rails.env.development?

  class IRB::Context
    def evaluate_with_reloading(line, line_no)

        ActionDispatch::Reloader.cleanup!
      ActionDispatch::Reloader.prepare!
      evaluate_without_reloading(line, line_no)
    end
    alias_method_chain :evaluate, :reloading
  end

  puts "=> IRB code reloading enabled"
end

In the meanwhile, shortly browsed the code at the Rails gihub project and it’s really a nice way to learn more about rails… must do it more often.

Silencing Nagging Logs in Rails

Once upon a time, I was trying to do some meaningful debugging in rails with

 logger.debug 

and I was being particularly nagged by these annoying messages from Webrick:

 WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true [closed]

The way to solve this? I am using ‘thin’ instead of Webrick as a development server (yeah, I know that there are patches to solve this in Webrick but I decided to go a bit aggressive instead).

And another thing that nagged me were the asset pipeline messages with the gets for the css, js, images, etc. So one way to shut Sprockets up is given here, and just for the record:

 # place this code in config/initializers/quiet_assets.rb
    if Rails.env.development?
  Rails.application.assets.logger = Logger.new('/dev/null')
  Rails::Rack::Logger.class_eval do
    def call_with_quiet_assets(env)
      previous_level = Rails.logger.level
      Rails.logger.level = Logger::ERROR if env['PATH_INFO'] =~ %r{^/assets/}
      call_without_quiet_assets(env)
    ensure
      Rails.logger.level = previous_level
    end
    alias_method_chain :call, :quiet_assets
  end
end

Ahhhhh now I can actually see meaningful logs in my development log. whew!

New Octopress Blog…

I created this blog specifically to jot down notes of interesting / useful / meaningful stuff I find during my adventures with development, which I hope are useful to others.

And to start with, here is a link to a post that really helped me out Octopress: Setting up a Blog and Contributing to an Existing One. It wasn’t really clear to me from Octopress instructions that I had to run rake generate and rake deploy; I was just trying to make the git commits and push by hand (and becoming really frustrated by it). So, just for my own reference:

 # do stuff in your blog, like rake new_post
    # then generate the blog
    rake generate
    # then save the source branch and push it to Github
    git add .
    git commit -m "whatever message"
    git push origin source
    # and now, this is where the magic happens! this takes care
    # of pushing everything automagically onto the master branch!
    rake deploy

(depending on your system, you might need to run bundle exec followed by the rake commands).

Oh, and also… to delete a post, delete it only from

blog-root/source/_posts/post-title
and then run the commands above again. The rake tasks from Octopress take care of everything.

This cool minimalistic theme is from zespia.tw, and it’s called Slash.

Ta-da!

So far I’m really enjoying Octopress – I chose it because I want to be able to do everything from the shell, and never leave my dev environment and my beloved ST2/emacs combination.