Dan Croak
8/9/2007 9:26:00 PM
I forgot to mention that this is called, for example, with:
assert_ordered_by :last_name, :users, "<="
I'm also moving away from using the spaceship operator into
conditional logic land because I was having difficulty dealing
(mentally) with comparing nil to various types (dates, strings,
integers, etc.). Here's the latest non-working code... :/
def assert_ordered_by(attribute, collection, direction)
collection = assigns["#{collection.to_s}"]
return true if collection.length < 2
collection.sort do |a, b|
if a.nil? && !b.nil?
return false if direction.include?(">") # nil cannot be
greater than anything except another nil
elsif b.nil? && !a.nil?
return false if direction.include?("<") # nil cannot be
greater than anything except another nil
elsif !a.nil? && !b.nil?
assert a.send(attribute).send(direction, b.send(attribute))
elsif a.nil? && b.nil?
return true
end
end
end