Class: Ransack::Adapters::Mongoid::Context

Inherits:
Context
  • Object
show all
Defined in:
lib/ransack/adapters/mongoid/context.rb

Instance Attribute Summary

Attributes inherited from Context

#arel_visitor, #auth_object, #base, #engine, #klass, #object, #search, #search_key

Instance Method Summary collapse

Methods inherited from Context

#association_path, #bind, #chain_scope, #contextualize, for, for_class, for_object, #ransackable_association?, #ransackable_attribute?, #ransackable_scope?, #scope_arity, #searchable_associations, #searchable_attributes, #sortable_attributes, #traverse, #unpolymorphize_association

Constructor Details

#initialize(object, options = {}) ⇒ Context

Because the AR::Associations namespace is insane JoinDependency = ::Mongoid::Associations::JoinDependency JoinPart = JoinDependency::JoinPart


13
14
15
16
# File 'lib/ransack/adapters/mongoid/context.rb', line 13

def initialize(object, options = {})
  super
  # @arel_visitor = @engine.connection.visitor
end

Instance Method Details

#attribute_method?(str, klass = @klass) ⇒ Boolean


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ransack/adapters/mongoid/context.rb', line 64

def attribute_method?(str, klass = @klass)
  exists = false
  if ransackable_attribute?(str, klass)
    exists = true
  elsif (segments = str.split(/_/)).size > 1
    remainder = []
    found_assoc = nil
    while !found_assoc && remainder.unshift(
      segments.pop) && segments.size > 0 do
      assoc, poly_class = unpolymorphize_association(
        segments.join('_')
        )
      if found_assoc = get_association(assoc, klass)
        exists = attribute_method?(remainder.join('_'),
          poly_class || found_assoc.klass
        )
      end
    end
  end
  exists
end

#evaluate(search, opts = {}) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ransack/adapters/mongoid/context.rb', line 46

def evaluate(search, opts = {})
  viz = Visitor.new
  relation = @object.where(viz.accept(search.base))
  if search.sorts.any?
    ary_sorting = viz.accept(search.sorts)
    sorting = {}
    ary_sorting.each do |s|
      sorting.merge! Hash[s.map { |k, d| [k.to_s == 'id' ? '_id' : k, d] }]
    end
    relation = relation.order_by(sorting)
    # relation = relation.except(:order)
    # .reorder(viz.accept(search.sorts))
  end
  # -- mongoid has different distinct method
  # opts[:distinct] ? relation.distinct : relation
  relation
end

#klassify(obj) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ransack/adapters/mongoid/context.rb', line 91

def klassify(obj)
  if Class === obj && obj.ancestors.include?(::Mongoid::Document)
    obj
  elsif obj.respond_to? :klass
    obj.klass
  elsif obj.respond_to? :base_klass
    obj.base_klass
  else
    raise ArgumentError, "Don't know how to klassify #{obj}"
  end
end

#relation_for(object) ⇒ Object


18
19
20
# File 'lib/ransack/adapters/mongoid/context.rb', line 18

def relation_for(object)
  object.all
end

#table_for(parent) ⇒ Object


86
87
88
89
# File 'lib/ransack/adapters/mongoid/context.rb', line 86

def table_for(parent)
  # parent.table
  Ransack::Adapters::Mongoid::Table.new(parent)
end

#type_for(attr) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ransack/adapters/mongoid/context.rb', line 22

def type_for(attr)
  return nil unless attr && attr.valid?
  name    = attr.arel_attribute.name.to_s
  # table   = attr.arel_attribute.relation.table_name

  # schema_cache = @engine.connection.schema_cache
  # raise "No table named #{table} exists" unless schema_cache.table_exists?(table)
  # schema_cache.columns_hash(table)[name].type

  # when :date
  # when :datetime, :timestamp, :time
  # when :boolean
  # when :integer
  # when :float
  # when :decimal
  # else # :string

  name = '_id' if name == 'id'

  t = object.klass.fields[name].type

  t.to_s.demodulize.underscore.to_sym
end