Skip to content
This repository

conditions_fu

It’s a little DSL for easily creating your active record conditions. It was born out of my need to map arbitry parameter names to different active record attributes.

ConditionsFu::Builder.blueprint(:user) do
   is_true(“admin” => params[:admin])
   includes({:parents_last_name => params[:surname]}, :or) do
      includes({:last_name => params[:surname]}, :or)
      is_like(“parent_email” => params[:email]) do
         is_true(:child => params[:child])
      end
   end
end

params = {:surname => “Barrie”, :email => “cam@snepo.com”, :child => false, :admin => true }
ConditionsFu::Builder.make(params) will return:
[“admin = ? AND (parents_last_name ~* ? OR last_name ~* ? OR (parent_email ILIKE ? AND child = ?))”, true, “Barrie”, “Barrie”, “cam@snepo.com”, true]

Note how blocks will couple elements together, and the last arg is a binding for the next element.

Currently it’s Postgresql only(mainly on account of the includes method), as that’s the only DB I’ve had to use it on, but patches are happily accepted provided that there’s specs accompanying them.

Something went wrong with that request. Please try again.