module Ansr::QueryMethods

Public Instance Methods

all_filter_fields() click to toggle source
# File lib/ansr/relation/query_methods.rb, line 204
def all_filter_fields
  FACETS
end
all_sort_fields() click to toggle source
# File lib/ansr/relation/query_methods.rb, line 208
def all_sort_fields
  SORTS
end
ansr_query(*args) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 298
def ansr_query(*args)
  if args.first
    arel, bind_values = [args[0], args[1]]
    @ansr_query = model.connection.to_nosql(arel, bind_values)
  end
  @ansr_query
end
as(args) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 104
def as(args)
  spawn.as!(args)
end
as!(args) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 108
def as!(args)
  self.as_value= args
  self
end
as_unscoping(*args) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 122
def as_unscoping(*args)
  @values.delete(:as)
end
as_value() click to toggle source
# File lib/ansr/relation/query_methods.rb, line 113
def as_value
  @values[:as]
end
as_value=(args) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 117
def as_value=(args)
  raise ActiveRecord::ImmutableRelation if @loaded
  @values[:as] = args
end
build_arel() click to toggle source

Could filtering be moved out of intersection into one arel tree?

Calls superclass method
# File lib/ansr/relation/query_methods.rb, line 243
def build_arel
  arel = super
  collapse_filters(arel, (filter_values).uniq)
  arel.projections << @values[:spellcheck] if @values[:spellcheck]
  arel.projections << @values[:highlight] if @values[:highlight]
  arel.projections += facet_values
  arel.from arel.create_table_alias(arel.source.left, as_value) if as_value
  arel
end
build_facets(expr, opts={}) click to toggle source

cloning from ActiveRecord::QueryMethods.build_where as a first pass

# File lib/ansr/relation/query_methods.rb, line 254
def build_facets(expr, opts={})
  case expr
  when Hash
    build_facets(::Arel.star,expr)
  when Array
    r = expr.inject([]) {|m,e| m.concat build_facets(e,opts)}
  when String, Symbol, Arel::SqlLiteral
    [Ansr::Arel::Nodes::Facet.new(::Arel::Attributes::Attribute.new(table, expr.to_s), opts)]
  when ::Arel::Attributes::Attribute
    [Ansr::Arel::Nodes::Facet.new(expr, opts)]
  else
    [expr]
  end
end
build_where(opts, other = []) click to toggle source

cloning from ActiveRecord::QueryMethods.build_where to use our PredicateBuilder

# File lib/ansr/relation/query_methods.rb, line 270
def build_where(opts, other = [])
  case opts
  when String, Array
    #TODO: Remove duplication with: /activerecord/lib/active_record/sanitization.rb:113
    values = Hash === other.first ? other.first.values : other

    values.grep(ActiveRecord::Relation) do |rel|
      self.bind_values += rel.bind_values
    end

    [@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
  when Hash
    attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)

    attributes.values.grep(ActiveRecord::Relation) do |rel|
      self.bind_values += rel.bind_values
    end

    PredicateBuilder.build_from_hash(klass, attributes, table)
  else
    [opts]
  end
end
collapse_filters(arel, filters) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 232
def collapse_filters(arel, filters)
  predicates = filters.map do |filter|
    next filter if ::Arel::Nodes::Equality === filter
    filter = Arel.sql(filter) if String === filter # SqlLiteral-ize
    ::Arel::Nodes::Grouping.new(filter)
  end

  arel.where(::Arel::Nodes::And.new(predicates)) if predicates.present?
end
collapse_wheres(arel, wheres) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 222
def collapse_wheres(arel, wheres)
  predicates = wheres.map do |where|
    next where if ::Arel::Nodes::Equality === where
    where = Arel.sql(where) if String === where # SqlLiteral-ize
    ::Arel::Nodes::Grouping.new(where)
  end

  arel.where(::Arel::Nodes::And.new(predicates)) if predicates.present?
end
facet(expr, opts = {}) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 79
def facet(expr, opts = {})
  spawn.facet!(expr, opts)
end
facet!(expr, opts={}) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 83
def facet!(expr, opts={})
  self.facet_values+= build_facets(expr, opts)
  self
end
facet_unscoping(target_value) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 97
def facet_unscoping(target_value)
end
facet_values() click to toggle source
# File lib/ansr/relation/query_methods.rb, line 88
def facet_values
  @values[:facets] || []
end
facet_values=(values) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 92
def facet_values=(values)
  raise ActiveRecord::ImmutableRelation if @loaded
  @values[:facets]=values
end
field_name(expr) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 168
def field_name(expr)
  if expr.is_a? Array
    return expr.collect{|x| field_name(x)}.compact
  else
    case expr
    when ::Arel::Nodes::Binary
      if expr.left.relation.name != model().table.name
        # oof, this is really hacky
        field_name = "#{expr.left.relation.name}.#{expr.left.name}".to_sym
        field_name = expr.left.name.to_sym
      else
        field_name = expr.left.name.to_sym
      end
    when ::Arel::Attributes::Attribute, Ansr::Arel::ConfiguredField
      if expr.relation.name != model().table.name
        # oof, this is really hacky
        field_name = "#{expr.relation.name}.#{expr.name}".to_sym
        field_name = expr.name.to_sym
      else
        field_name = expr.name.to_sym
      end
    when ::Arel::Nodes::Unary, Ansr::Arel::Nodes::Filter
      if expr.expr.relation.name != model().table.name
        # oof, this is really hacky
        field_name = "#{expr.expr.relation.name}.#{expr.expr.name}".to_sym
        field_name = expr.expr.name.to_sym
      else
        field_name = expr.expr.name.to_sym
      end
    else
      field_name = expr.to_sym
    end
    return field_name
  end
end
filter(expr) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 36
def filter(expr)
  check_if_method_has_arguments!("filter", expr)
  spawn.filter!(expr)
end
filter!(expr) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 41
def filter!(expr)
  return self if expr.empty?

  filter_nodes = build_where(expr)
  return self unless filter_nodes
  filters = []
  filter_nodes.each do |filter_node|
    case filter_node
    when ::Arel::Nodes::In, ::Arel::Nodes::Equality
      filter_node.left = Ansr::Arel::Nodes::Filter.new(filter_node.left)
    when ::Arel::SqlLiteral
      filter_node = Ansr::Arel::Nodes::Filter.new(filter_node)
    when String, Symbol
      filter_node = Ansr::Arel::Nodes::Filter.new(::Arel::SqlLiteral.new(filter_node.to_s))
    else
      raise "unexpected filter node type #{filter_node.class}"
    end
    filters << filter_node
  end
  self.filter_values+= filters 

  self
end
filter_name(expr) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 100
def filter_name(expr)
  connection.sanitize_filter_name(field_name(expr))
end
filter_unscoping(target_value) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 65
def filter_unscoping(target_value)
  target_value_sym = target_value.to_sym

  filter_values.reject! do |rel|
    case rel
    when ::Arel::Nodes::In, ::Arel::Nodes::Equality
      subrelation = (rel.left.kind_of?(::Arel::Attributes::Attribute) ? rel.left : rel.right)
      subrelation.name.to_sym == target_value_sym
    else
      raise "unscope(filter: #{target_value.inspect}) failed: unscoping #{rel.class} is unimplemented."
    end
  end
end
filter_values() click to toggle source
# File lib/ansr/relation/query_methods.rb, line 27
def filter_values
  @values[:filter] ||= []
end
filter_values=(values) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 31
def filter_values=(values)
  raise ImmutableRelation if @loaded
  @values[:filter] = values
end
find(id) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 212
def find(id)
  klass = model()
  rel = where(klass.table.primary_key.name => id).limit(1)
  rel.to_a
  unless rel.to_a[0]
    raise 'Bad ID'
  end
  rel.to_a.first
end
find_by_nosql(*args) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 294
def find_by_nosql(*args)
  model.connection.execute(ansr_query(*args))
end
highlight(expr, opts={}) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 126
def highlight(expr, opts={})
  spawn.highlight!(expr, opts)
end
highlight!(expr, opts = {}) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 130
def highlight!(expr, opts = {})
  self.highlight_value= Ansr::Arel::Nodes::Highlight.new(expr, opts)
end
highlight_unscoping(*args) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 143
def highlight_unscoping(*args)
  @values.delete(:highlight)
end
highlight_value() click to toggle source
# File lib/ansr/relation/query_methods.rb, line 134
def highlight_value
  @values[:highlight]
end
highlight_value=(val) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 138
def highlight_value=(val)
  raise ActiveRecord::ImmutableRelation if @loaded
  @values[:highlight] = val
end
spellcheck(expr, opts={}) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 147
def spellcheck(expr, opts={})
  spawn.spellcheck!(expr, opts)
end
spellcheck!(expr, opts = {}) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 151
def spellcheck!(expr, opts = {})
  self.spellcheck_value= Ansr::Arel::Nodes::Spellcheck.new(expr, opts)
end
spellcheck_unscoping(*args) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 164
def spellcheck_unscoping(*args)
  @values.delete(:spellcheck)
end
spellcheck_value() click to toggle source
# File lib/ansr/relation/query_methods.rb, line 155
def spellcheck_value
  @values[:spellcheck]
end
spellcheck_value=(val) click to toggle source
# File lib/ansr/relation/query_methods.rb, line 159
def spellcheck_value=(val)
  raise ActiveRecord::ImmutableRelation if @loaded
  @values[:spellcheck] = val
end