Class: Datagrid::Drivers::ActiveRecord

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

Overview

:nodoc:

Constant Summary

Constants inherited from AbstractDriver

Datagrid::Drivers::AbstractDriver::TIMESTAMP_CLASSES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractDriver

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

Class Method Details

.match?(scope) ⇒ Boolean

Returns:

  • (Boolean)

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

def self.match?(scope)
  return false unless defined?(::ActiveRecord)
  if scope.is_a?(Class)
    scope.ancestors.include?(::ActiveRecord::Base)
  else
    scope.is_a?(::ActiveRecord::Relation)
  end
end

Instance Method Details

#append_column_queries(assets, columns) ⇒ Object


29
30
31
32
33
34
35
36
37
38
# File 'lib/datagrid/drivers/active_record.rb', line 29

def append_column_queries(assets, columns)
  if columns.present?
    if assets.select_values.empty?
      assets = assets.select(Arel.respond_to?(:star) ? assets.klass.arel_table[Arel.star] : "#{assets.quoted_table_name}.*")
    end
    columns = columns.map {|c| "#{c.query} AS #{c.name}"}
    assets = assets.select(*columns)
  end
  assets
end

#asc(scope, order) ⇒ Object


44
45
46
47
48
# File 'lib/datagrid/drivers/active_record.rb', line 44

def asc(scope, order)
  # Rails 3.x.x don't able to override already applied order
  # Using #reorder instead
  scope.reorder(order)
end

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


104
105
106
107
108
109
110
# File 'lib/datagrid/drivers/active_record.rb', line 104

def batch_each(scope, batch_size, &block)
  if scope.limit_value
    raise Datagrid::ConfigurationError, "ActiveRecord can not use batches in combination with SQL limit"
  end
  options = batch_size ? { batch_size: batch_size } : {}
  scope.find_each(**options, &block)
end

#can_preload?(scope, association) ⇒ Boolean

Returns:

  • (Boolean)

120
121
122
# File 'lib/datagrid/drivers/active_record.rb', line 120

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

#column_names(scope) ⇒ Object


80
81
82
# File 'lib/datagrid/drivers/active_record.rb', line 80

def column_names(scope)
  scope.column_names
end

#contains(scope, field, value) ⇒ Object


84
85
86
87
# File 'lib/datagrid/drivers/active_record.rb', line 84

def contains(scope, field, value)
  field = prefix_table_name(scope, field)
  scope.where("#{field} #{contains_predicate} ?", "%#{value}%")
end

#default_cache_key(asset) ⇒ Object


112
113
114
# File 'lib/datagrid/drivers/active_record.rb', line 112

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

#default_order(scope, column_name) ⇒ Object


62
63
64
# File 'lib/datagrid/drivers/active_record.rb', line 62

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

#default_preload(scope, value) ⇒ Object


116
117
118
# File 'lib/datagrid/drivers/active_record.rb', line 116

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

#desc(scope, order) ⇒ Object


50
51
52
53
54
55
56
# File 'lib/datagrid/drivers/active_record.rb', line 50

def desc(scope, order)
  if order.respond_to?(:desc)
    scope.reorder(order.desc)
  else
    scope.reorder(order).reverse_order
  end
end

#greater_equal(scope, field, value) ⇒ Object


66
67
68
# File 'lib/datagrid/drivers/active_record.rb', line 66

def greater_equal(scope, field, value)
  scope.where(["#{prefix_table_name(scope, field)} >= ?", value])
end

#has_column?(scope, column_name) ⇒ Boolean

Returns:

  • (Boolean)

74
75
76
77
78
# File 'lib/datagrid/drivers/active_record.rb', line 74

def has_column?(scope, column_name)
  scope.column_names.include?(column_name.to_s)
rescue ::ActiveRecord::StatementInvalid
  false
end

#less_equal(scope, field, value) ⇒ Object


70
71
72
# File 'lib/datagrid/drivers/active_record.rb', line 70

def less_equal(scope, field, value)
  scope.where(["#{prefix_table_name(scope, field)} <= ?", value])
end

#normalized_column_type(scope, field) ⇒ Object


89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/datagrid/drivers/active_record.rb', line 89

def normalized_column_type(scope, field)
  return nil unless has_column?(scope, field)
  builtin_type = scope.columns_hash[field.to_s].type
  {
    [:string, :text, :time, :binary] => :string,
    [:integer, :primary_key] => :integer,
    [:float, :decimal] => :float,
    [:date] => :date,
    [:datetime, :timestamp] => :timestamp,
    [:boolean] => :boolean
  }.each do |keys, value|
    return value if keys.include?(builtin_type)
  end
end

#reverse_order(scope) ⇒ Object


58
59
60
# File 'lib/datagrid/drivers/active_record.rb', line 58

def reverse_order(scope)
  scope.reverse_order
end

#to_scope(scope) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/datagrid/drivers/active_record.rb', line 14

def to_scope(scope)
  return scope if scope.is_a?(::ActiveRecord::Relation)
  # Model class or Active record association
  # ActiveRecord association class hides itself under an Array
  # We can only reveal it by checking if it respond to some specific
  # to ActiveRecord method like #scoped
  if scope.is_a?(Class)
    Rails.version >= "4.0" ? scope.all : scope.scoped({})
  elsif scope.respond_to?(:scoped)
    scope.scoped
  else
    scope
  end
end

#where(scope, attribute, value) ⇒ Object


40
41
42
# File 'lib/datagrid/drivers/active_record.rb', line 40

def where(scope, attribute, value)
  scope.where(attribute => value)
end