lib/sakuramochi/relation.rb in sakuramochi-0.5.1 vs lib/sakuramochi/relation.rb in sakuramochi-0.5.2
- old
+ new
@@ -16,38 +16,39 @@
when Sakuramochi::Condition::Nodes::Expression
case node.operator.to_s
when 'and'
left = collapse_conditions(node.left, other)
right = collapse_conditions(node.right, other)
- Arel::Nodes::And.new([left, right])
+ left && right ? Arel::Nodes::And.new([left, right]) : left || right
+
when 'or'
left = collapse_conditions(node.left, other)
right = collapse_conditions(node.right, other)
- Arel::Nodes::Or.new(left, right)
+ left && right ? Arel::Nodes::Or.new(left, right) : left || right
end
when Sakuramochi::Condition::Nodes::Term
case node.operator.to_s
when 'not'
right = collapse_conditions(node.value, other)
- Arel::Nodes::Not.new(right)
+ Arel::Nodes::Not.new(right) if right
end
when Sakuramochi::Condition::Nodes::Factor
wheres = build_where_without_condition(node.value, other)
arel = table.from(table)
collapse_wheres(arel, (wheres - ['']).uniq)
arel.constraints.inject { |left, right| left.and(right) }
when Sakuramochi::Condition::Nodes::Group
expression = collapse_conditions(node.expression, other)
- Arel::Nodes::Grouping.new(expression)
+ Arel::Nodes::Grouping.new(expression) if expression
end
end
def build_where_with_condition(opts, other = [])
ast = Sakuramochi::Condition::Parser.new(opts.dup).parse
- [collapse_conditions(ast, other)]
+ [collapse_conditions(ast, other)].compact
end
end
end
end