Module: CanCanCan::BabySqueel::ExpressionCombinator

Included in:
BabySqueelAdapter
Defined in:
lib/cancancan/baby_squeel/expression_combinator.rb

Constant Summary collapse

ALWAYS_TRUE =

This true expression is used to indicate a condition that is always satisfied.

Arel::Nodes::SqlLiteral.new('1=1').freeze
ALWAYS_FALSE =

This true expression is used to indicate a condition that is never satisfied.

Arel::Nodes::SqlLiteral.new('1=0').freeze

Instance Method Summary collapse

Instance Method Details

#combine_squeel_expressions(left_expression, left_expression_joins, operator, right_expression, right_expression_joins) ⇒ Array<(Squeel::Nodes::Node, Array)>

Combines two Squeel expressions. This is aware of the ALWAYS_TRUE and ALWAYS_FALSE constants and performs simplification.

Parameters:

  • left_expression (Squeel::Nodes::Node)

    The left expression.

  • left_expression_joins (Array)

    An array of joins which the Squeel expression must be joined to.

  • operator (Symbol)

    The operator to combine with. This must be either :and or :or.

  • right_expression (Squeel::Nodes::Node)

    The right expression.

  • right_expression_joins (Array)

    An array of joins which the Squeel expression must be joined to.

Returns:

  • (Array<(Squeel::Nodes::Node, Array)>)

    A tuple containing the combination of the given expressions, as well as an array of joins which the Squeel expression must be joined to.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cancancan/baby_squeel/expression_combinator.rb', line 21

def combine_squeel_expressions(left_expression, left_expression_joins, operator,
                               right_expression, right_expression_joins)
  case operator
  when :and then conjunction_expressions(left_expression, left_expression_joins,
                                       right_expression, right_expression_joins)
  when :or then disjunction_expressions(left_expression, left_expression_joins,
                                       right_expression, right_expression_joins)
  else
    raise ArgumentError, "#{operator} must either be :and or :or"
  end
end

#conjunction_expressions(left_expression, left_expression_joins, right_expression, right_expression_joins) ⇒ Array<(Squeel::Nodes::Node, Array)>

Computes the conjunction of the two Squeel expressions.

Boolean simplification is done for the ALWAYS_TRUE and ALWAYS_FALSE values.

Parameters:

  • left_expression (Squeel::Nodes::Node)

    The left expression.

  • left_expression_joins (Array)

    An array of joins which the Squeel expression must be joined to.

  • right_expression (Squeel::Nodes::Node)

    The right expression.

  • right_expression_joins (Array)

    An array of joins which the Squeel expression must be joined to.

Returns:

  • (Array<(Squeel::Nodes::Node, Array)>)

    A tuple containing the conjunction of the left and right expressions, as well as an array of joins which the Squeel expression must be joined to.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cancancan/baby_squeel/expression_combinator.rb', line 44

def conjunction_expressions(left_expression, left_expression_joins, right_expression,
                            right_expression_joins)
  if left_expression == ALWAYS_FALSE || right_expression == ALWAYS_FALSE
    [ALWAYS_FALSE, []]
  elsif left_expression == ALWAYS_TRUE
    [right_expression, right_expression_joins]
  elsif right_expression == ALWAYS_TRUE
    [left_expression, left_expression_joins]
  else
    [left_expression.and(right_expression), left_expression_joins + right_expression_joins]
  end
end

#disjunction_expressions(left_expression, left_expression_joins, right_expression, right_expression_joins) ⇒ Array<(Squeel::Nodes::Node, Array)>

Computes the disjunction of the two Squeel expressions.

Boolean simplification is done for the ALWAYS_TRUE and ALWAYS_FALSE values.

Parameters:

  • left_expression (Squeel::Nodes::Node)

    The left expression.

  • left_expression_joins (Array)

    An array of joins which the Squeel expression must be joined to.

  • right_expression (Squeel::Nodes::Node)

    The right expression.

  • right_expression_joins (Array)

    An array of joins which the Squeel expression must be joined to.

Returns:

  • (Array<(Squeel::Nodes::Node, Array)>)

    A tuple containing the disjunction of the left and right expressions, as well as an array of joins which the Squeel expression must be joined to.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cancancan/baby_squeel/expression_combinator.rb', line 68

def disjunction_expressions(left_expression, left_expression_joins, right_expression,
                            right_expression_joins)
  if left_expression == ALWAYS_TRUE || right_expression == ALWAYS_TRUE
    [ALWAYS_TRUE, []]
  elsif left_expression == ALWAYS_FALSE
    [right_expression, right_expression_joins]
  elsif right_expression == ALWAYS_FALSE
    [left_expression, left_expression_joins]
  else
    [left_expression.or(right_expression), left_expression_joins + right_expression_joins]
  end
end