Module: ActiveRecord::QueryMethods

Defined in:
lib/rearmed/monkey_patches/rails_4.rb

Defined Under Namespace

Classes: OrChain

Instance Method Summary collapse

Instance Method Details

#or(opts = :chain, *rest) ⇒ Object

Post.where(“id = 1”).or.where(“id = 2”) Post.where(“id = 1”).or.containing_the_letter_a Post.where(“id = 1”).or(Post.where(“id = 2”)) Post.where(“id = 1”).or(“id = ?”, 2)



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rearmed/monkey_patches/rails_4.rb', line 107

def or(opts = :chain, *rest)
  if opts == :chain
    OrChain.new(self)
  else
    left = self
    right = (ActiveRecord::Relation === opts) ? opts : klass.unscoped.where(opts, rest)

    unless left.where_values.empty? || right.where_values.empty?
      left.where_values = [left.where_ast.or(right.where_ast)]
      right.where_values = []
    end

    left = left.merge(right)
  end
end

#where_astObject

Returns an Arel AST containing only where_values



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rearmed/monkey_patches/rails_4.rb', line 124

def where_ast
  arel_wheres = []

  where_values.each do |where|
    arel_wheres << (String === where ? Arel.sql(where) : where)
  end

  return Arel::Nodes::Grouping.new(Arel::Nodes::And.new(arel_wheres)) if arel_wheres.length >= 2

  if Arel::Nodes::SqlLiteral === arel_wheres.first
    Arel::Nodes::Grouping.new(arel_wheres.first)
  else
    arel_wheres.first
  end
end