make neater code question

question <rails-mailing-list@...> writes:

Hi,
i was wondering whether there was a better way to write something in
this type of format:

def foo
if x
somefunction
..find(:all, :conditions => a + b + c + d)
else
samefunction
  ..find(:all, :conditions => a + b + c)
end
end

into something a lot shorter like:

def foo
somefunction
find(:all, :condtions => a + b + c, :with_exclusive_scope?? => d )
end

I wrote a class a couple of days ago for just this kind of thing:

http://pastie.caboo.se/12591

use it like:

def foo
  conditions = Conditions.new
  conditions << ["a = ?", a]
  conditions << ["b = ?", b]
  conditions << ["c = ?", c]
  conditions << ["d = ?", d] if x

  find(:all, :conditions => conditions)
end

Gareth

Of course, there’s always pre-generation:

def foo
conditions = a + b + c
conditions += d if x
somefuction
find(:all, :conditions => conditions)
end

Jason