Archive for June, 2009

  • 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 [...]