Module: Ansr::QueryMethods

Includes:
ActiveRecord::QueryMethods, Configurable
Included in:
Base, Relation
Defined in:
lib/ansr/relation/query_methods.rb

Instance Method Summary collapse

Methods included from Configurable

#config

Instance Method Details

#all_filter_fieldsObject



93
94
95
# File 'lib/ansr/relation/query_methods.rb', line 93

def all_filter_fields
  FACETS
end

#all_sort_fieldsObject



97
98
99
# File 'lib/ansr/relation/query_methods.rb', line 97

def all_sort_fields
  SORTS
end

#build_filter(opts, other = []) ⇒ Object

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ansr/relation/query_methods.rb', line 122

def build_filter(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
      opts = (other.empty? ? opts : ([opts] + other))
      [model().send(:sanitize_sql, opts, model().table_name)]
    when Hash
      attributes = model().send(:expand_hash_conditions_for_sql_aggregates, opts)

      attributes.keys.each do |k|
        sk = filter_name(k)
        attributes[sk] = attributes.delete(k) unless sk.eql? k.to_s
      end
      attributes.values.grep(ActiveRecord::Relation) do |rel|
        self.bind_values += rel.bind_values
      end

      ActiveRecord::PredicateBuilder.build_from_hash(model(), attributes, model().table)
    else
      [opts]
  end
end

#collapse_wheres(arel, wheres) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/ansr/relation/query_methods.rb', line 111

def collapse_wheres(arel, wheres)
  predicates = wheres.map do |where|
    next where if ::Arel::Nodes::Equality === where
    where if String === where
    ::Arel::Nodes::Grouping.new(where)
  end

  arel.where(::Arel::Nodes::And.new(predicates)) if predicates.present?
end

#field_name(expr) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ansr/relation/query_methods.rb', line 60

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().name
        # oof, this is really hacky
        field_name = "#{expr.left.relation.name}.#{expr.left.name}".to_sym
      else
        field_name = expr.left.name.to_sym
      end
    when ::Arel::Attributes::Attribute
      if expr.relation.name != model().name
        # oof, this is really hacky
        field_name = "#{expr.relation.name}.#{expr.name}".to_sym
      else
        field_name = expr.name.to_sym
      end
    when ::Arel::Nodes::Unary
      if expr.expr.relation.name != model().name
        # oof, this is really hacky
        field_name = "#{expr.expr.relation.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(args) ⇒ Object



37
38
39
40
# File 'lib/ansr/relation/query_methods.rb', line 37

def filter(args)
  check_if_method_has_arguments!("filter", args)
  spawn.filter!(args)
end

#filter!(args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ansr/relation/query_methods.rb', line 42

def filter!(args)
  return self if args.empty?

  filter_where = build_filter(args)
  return self unless filter_where
  filter_name = filter_name(filter_where)
  if (filter_name)
    @klass = @klass.view(*filter_where)
    model().projections += Array(filter_name)
  end

  self
end

#filter_name(expr) ⇒ Object



56
57
58
# File 'lib/ansr/relation/query_methods.rb', line 56

def filter_name(expr)
  connection.sanitize_filter_name(field_name(expr))
end

#filter_valuesObject



28
29
30
# File 'lib/ansr/relation/query_methods.rb', line 28

def filter_values
  @values[:filter] ||= []
end

#filter_values=(values) ⇒ Object

Raises:

  • (ImmutableRelation)


32
33
34
35
# File 'lib/ansr/relation/query_methods.rb', line 32

def filter_values=(values)
  raise ImmutableRelation if @loaded
  @values[:filter] = values
end

#find(id) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/ansr/relation/query_methods.rb', line 101

def find(id)
  klass = model()
  rel = klass.build_default_scope.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(arel, bind_values) ⇒ Object



150
151
152
153
154
# File 'lib/ansr/relation/query_methods.rb', line 150

def find_by_nosql(arel, bind_values)
  query = model.connection.to_nosql(arel, bind_values)
  aliases = model.connection.to_aliases(arel, bind_values)
  model.connection.execute(query, aliases)
end