Class: Datagrid::Drivers::Mongoid

Inherits:
AbstractDriver show all
Defined in:
lib/datagrid/drivers/mongoid.rb

Overview

:nodoc:

Constant Summary

Constants inherited from AbstractDriver

AbstractDriver::TIMESTAMP_CLASSES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractDriver

#append_column_queries, guess_driver, inherited, #is_timestamp?, #match?, #reverse_order, #where_by_timestamp_gotcha

Class Method Details

.match?(scope) ⇒ Boolean

Returns:

  • (Boolean)

5
6
7
8
9
10
11
12
# File 'lib/datagrid/drivers/mongoid.rb', line 5

def self.match?(scope)
  return false unless defined?(::Mongoid)
  if scope.is_a?(Class)
    scope.ancestors.include?(::Mongoid::Document)
  else
    scope.is_a?(::Mongoid::Criteria)
  end
end

Instance Method Details

#asc(scope, order) ⇒ Object


29
30
31
# File 'lib/datagrid/drivers/mongoid.rb', line 29

def asc(scope, order)
  scope.asc(order)
end

#batch_each(scope, batch_size, &block) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/datagrid/drivers/mongoid.rb', line 81

def batch_each(scope, batch_size, &block)
  current_page = 0
  loop do
    batch = scope.skip(current_page * batch_size).limit(batch_size).to_a
    return if batch.empty?
    scope.skip(current_page * batch_size).limit(batch_size).each do |item|
      yield(item)
    end
    current_page+=1
  end
end

#can_preload?(scope, association) ⇒ Boolean

Returns:

  • (Boolean)

101
102
103
# File 'lib/datagrid/drivers/mongoid.rb', line 101

def can_preload?(scope, association)
  !! scope.klass.reflect_on_association(association)
end

#column_names(scope) ⇒ Object


57
58
59
# File 'lib/datagrid/drivers/mongoid.rb', line 57

def column_names(scope)
  to_scope(scope).klass.fields.keys
end

#contains(scope, field, value) ⇒ Object


53
54
55
# File 'lib/datagrid/drivers/mongoid.rb', line 53

def contains(scope, field, value)
  scope.where(field => Regexp.compile(Regexp.escape(value)))
end

#default_cache_key(asset) ⇒ Object


93
94
95
# File 'lib/datagrid/drivers/mongoid.rb', line 93

def default_cache_key(asset)
  asset.id || raise(NotImplementedError)
end

#default_order(scope, column_name) ⇒ Object


37
38
39
# File 'lib/datagrid/drivers/mongoid.rb', line 37

def default_order(scope, column_name)
  has_column?(scope, column_name) ? column_name : nil
end

#default_preload(scope, value) ⇒ Object


97
98
99
# File 'lib/datagrid/drivers/mongoid.rb', line 97

def default_preload(scope, value)
  scope.includes(value)
end

#desc(scope, order) ⇒ Object


33
34
35
# File 'lib/datagrid/drivers/mongoid.rb', line 33

def desc(scope, order)
  scope.desc(order)
end

#greater_equal(scope, field, value) ⇒ Object


41
42
43
# File 'lib/datagrid/drivers/mongoid.rb', line 41

def greater_equal(scope, field, value)
  scope.where(field => {"$gte" => value})
end

#has_column?(scope, column_name) ⇒ Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/datagrid/drivers/mongoid.rb', line 49

def has_column?(scope, column_name)
  column_names(scope).include?(column_name.to_s)
end

#less_equal(scope, field, value) ⇒ Object


45
46
47
# File 'lib/datagrid/drivers/mongoid.rb', line 45

def less_equal(scope, field, value)
  scope.where(field => {"$lte" => value})
end

#normalized_column_type(scope, field) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/datagrid/drivers/mongoid.rb', line 61

def normalized_column_type(scope, field)
  type = to_scope(scope).klass.fields[field.to_s].try(:type)
  return nil unless type
  {
    [BigDecimal , String, Symbol, Range, Array, Hash, ] => :string,
    [Boolean] => :boolean,

    [Date] => :date,

    TIMESTAMP_CLASSES => :timestamp,

    [Float] => :float,

    [Integer] => :integer,
  }.each do |keys, value|
    return value if keys.include?(type)
  end
  return :string
end

#to_scope(scope) ⇒ Object


14
15
16
17
18
19
20
# File 'lib/datagrid/drivers/mongoid.rb', line 14

def to_scope(scope)
  if scope.respond_to?(:all)
    scope.all
  else
    scope.where(nil)
  end
end

#where(scope, attribute, value) ⇒ Object


22
23
24
25
26
27
# File 'lib/datagrid/drivers/mongoid.rb', line 22

def where(scope, attribute, value)
  if value.is_a?(Range)
    value = {"$gte" => value.first, "$lte" => value.last}
  end
  scope.where(attribute => value)
end