Archive for the tips Category

  • Extending the timeout for Rails processes through Passenger on Nginx - August 5th, 2009

    I recently had to allow for a long running reporting task in a Rails app which is running under Passenger (mod_rails) on Nginx
    The default setting is 60 seconds and setting send_timeout to 300 in the Nginx config wasn’t working.
    After much searching, I found the answer. Edit
    /ext/nginx/Conguration.c and change the lines:
    ngx_conf_merge_msec_value(conf->upstream.send_timeout, prev->upstream.send_timeout, 60000);
    ngx_conf_merge_msec_value(conf->upstream.read_timeout, prev->upstream.read_timeout, 60000);
    to (for [...]

  • Rails plugin that provides rake tasks for identifying missing specs - June 18th, 2009

    I have a bad habbit of generating my views while specing the controller and then never generating a view spec.
    I wrote a rake task to tell me which views have no specs, kind of like a rcov for views.
    I then realised that it could be useful to find all files with missing specs so now [...]

  • How to select an array of values in Rails - June 15th, 2009

    I recently wanted to get a list of IDs from rails, but wanted them in an array.
    First try is to use :select => ‘id’ in:
    Model.find(:all, :select => ‘id’)
    but that creates a model for each id
    In order to get an array of values, you can use select_values:
    Model.connection.select_values(’select id from models’)
    This is great, but what if you [...]

  • Accessing the Rails logger inside of library files - June 10th, 2009

    A quick tip, if you’re developing a library which lives inside of RAILS_ROOT/lib, you can use the Rails logger via DEFAULT_RAILS_LOGGER
    class MyLib
      def my_method
        RAILS_DEFAULT_LOGGER.info "my log message" if RAILS_DEFAULT_LOGGER
      end
    end
    I added if RAILS_DEFAULT_LOGGER so that it doesn’t trip up if used outside of RAILS

  • Download a large amount of data in CSV from Rails - June 3rd, 2009

    I’ve just had to re-write an export script because the application now needed to be able to download 14000 records in CSV format.
    Originally I was reading all the records (with multiple joins) and then creating a csv string and then writing it to the response using send_data.
    The original code
    This is a simplified version of my [...]

  • Map Fields - A Rails plugin to ease the importing of CSV files - June 3rd, 2009

    I’ve just released a plugin, map-fieilds, that eases the importing of CSV files.
    When importing CSV files for a project, I wanted to add flexibility for the users so that they could import their CSV files in a looser format and then map their format to the format I needed.
    map-fieilds will intercept calls to a method [...]

  • Conditionally include development gems - May 28th, 2009

    I was playing with metric_fu and after having to install multiple dependency gems I realised that it was going to be a pain to put the project into production.
    The metric_fu instructions suggest using config.gem in your environment file but that will mean that when you push the app to production, you need to install the [...]

  • Easily compare a variable with multiple values - May 27th, 2009

    I often forget this simple little trick for comparing multiple values against a single variable.
    Instead of
    var = 2
    if var == 1 || var == 2 || var == 3
      puts "yes"
    end
    #=> "yes"
    You can do the following
    var = 2
    if [1,2,3].include?(var)
      puts "yes"
    end
    #=> "yes"

  • Using Webrat’s save_and_open_page on Linux - March 17th, 2009

    Mmmm, this year has flown and I have done hardly any blogging, despite my intention to blog at least once a week. Oh, well. My excuse is working extra hard to cover a family holiday and the upcoming trip to Scottland on Rails.
    The best way to get going on something is to just do it [...]

  • How to protect downloads but still have nginx serve the files - January 13th, 2009

    I’ve just been working on a project where a number of downloads needed to be restricted to specific users. I needed to authenticate the user and then allow them access to the file. This is not too difficult in rails:
    def download
      if authenticated?
        send_file #{RAILS_ROOT}/downloads/images/myfile.zip’
      end
    end
    The problem with this is that if the file [...]

←Older