Archive for May, 2009

rake task to generate CHANGELOG file (for git repositories)

generate (or update) a CHANGELOG file for git repositories (shamelessly copied from manveru/ramaze).

desc 'update changelog'
task :changelog do
  File.open('CHANGELOG', 'w+') do |changelog|
    `git log -z --abbrev-commit`.split("\0").each do |commit|
      next if commit =~ /^Merge: \d*/
      ref, author, time, _, title, _, message = commit.split("\n", 7)
      ref = ref[/commit ([0-9a-f]+)/, 1]
      author = author[/Author: (.*)/, 1].strip
      time = Time.parse(time[/Date: (.*)/, 1]).utc
      title.strip!

      changelog.puts "[#{ref} | #{time}] #{author}"
      changelog.puts '', " * #{title}"
      changelog.puts '', message.rstrip if message
      changelog.puts
    end
  end
end

strip whitespace using rake

This snippet is taken from the wonderful webrat library

desc 'Removes trailing whitespace'
task :whitespace do
  sh %{find . -name '*.rb' -exec sed -i '' 's/ *$//g' {} \\;}
end

authenticated flash upload using merb-auth and dm-paperclip

In order to get multiple file upload working, we decided to use flash. The problem with the flash upload library we used (uploadify — another alternative is swfupload), is that they both don’t send the session cookie to the server.

Since we have to authenticate our image uploads, we needed to get around this by following 2 steps:

  1. install the rack middleware from below (original code by Angel Pizarro, found via thewebfellas)
  2. send the raw cookie data as query parameter keyed by your apps session_id_key
# Merb.root/lib/rack/flash_upload.rb
module Merb
  module Rack
    class SetSessionCookieFromFlash < Merb::Rack::Middleware
      # :api: private
      def initialize(app, session_key = '_session_id')
        super(app)
        @session_key = session_key
      end
      # :api: plugin
      def call(env)
        if env["HTTP_USER_AGENT"] =~ /^(Adobe|Shockwave) Flash/
          params = Merb::Parse.query(env['QUERY_STRING'])
          if params[@session_key]
            env['HTTP_COOKIE'] = [@session_key, params[@session_key]].join('=').freeze
          end
        end
        @app.call(env)
      end
    end
  end
end

# Merb.root/config/rack.rb
require 'rack/flash_upload'
use Merb::Rack::SetSessionCookieFromFlash, Merb::Config[:session_id_key]

datamapper storage engine reflection

# Find out what storage engines are installed and are supported by datamapper
# This could be especially useful in testing scenarios, where one wants to run
# specs against all adapters that are available on the machine in use.

require "rubygems"
require "dm-core"
require "spec"

module DataMapper

  def self.available_storage_engines
    [ :sqlite3, :mysql, :postgres ].select do |name|
      storage_engine_available?(name)
    end
  end

  def self.storage_engine_available?(name)
    ::DataObjects.const_defined?(Extlib::Inflection.classify(name.to_s))
  end

end

# spec helper
# set it so that it fits your actually installed do_xxx drivers
AVAILABLE_STORAGE_ENGINES = [
  :sqlite3,
  :mysql
  # :postgres
]

describe 'DataMapper storage engine reflection: ' do

  describe 'DataMapper.storage_engine_available?(name)' do
    it 'should return true for adapters known to be installed on this machine' do
      DataMapper.storage_engine_available?(:sqlite3).should  == AVAILABLE_STORAGE_ENGINES.include?(:sqlite3)
      DataMapper.storage_engine_available?(:mysql).should    == AVAILABLE_STORAGE_ENGINES.include?(:mysql)
      DataMapper.storage_engine_available?(:postgres).should == AVAILABLE_STORAGE_ENGINES.include?(:postgres)
    end
  end

  describe 'DataMapper.available_storage_engines' do
    it 'should try all adapters shipped with dm-core and return all adapters known to be installed on this machine' do
      storage_engines = DataMapper.available_storage_engines
      storage_engines.size.should == AVAILABLE_STORAGE_ENGINES.size
      storage_engines.each do |storage_engine|
        AVAILABLE_STORAGE_ENGINES.should include(storage_engine)
      end
    end
  end

end

# mungo:Desktop snusnu$ spec -cfs storage_engine_reflection.rb
#
# DataMapper storage engine reflection:  DataMapper.storage_engine_available?(name)
# - should return true for adapters known to be installed on this machine
#
# DataMapper storage engine reflection:  DataMapper.available_storage_engines
# - should try all adapters shipped with dm-core and return all adapters known to be installed on this machine
#
# Finished in 0.002688 seconds
#
# 2 examples, 0 failures