Class: Datagrid::Drivers::Sequel

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

Overview

:nodoc:

Constant Summary

Constants inherited from AbstractDriver

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/sequel.rb', line 5

def self.match?(scope)
  return false unless defined?(::Sequel)
  if scope.is_a?(Class)
    scope.ancestors.include?(::Sequel::Model)
  else
    scope.is_a?(::Sequel::Dataset)
  end
end

Instance Method Details

#append_column_queries(assets, columns) ⇒ Object


19
20
21
# File 'lib/datagrid/drivers/sequel.rb', line 19

def append_column_queries(assets, columns)
  super
end

#asc(scope, order) ⇒ Object


27
28
29
# File 'lib/datagrid/drivers/sequel.rb', line 27

def asc(scope, order)
  scope.order(::Sequel.lit(order))
end

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


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

def batch_each(scope, batch_size, &block)
  scope.extension(:pagination).each_page(batch_size) do |page|
    page.each(&block)
  end
end

#can_preload?(scope, association) ⇒ Boolean

Returns:

  • (Boolean)

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

def can_preload?(scope, association)
  !! scope.model.association_reflection(association)
end

#column_names(scope) ⇒ Object


55
56
57
# File 'lib/datagrid/drivers/sequel.rb', line 55

def column_names(scope)
  scope.columns
end

#contains(scope, field, value) ⇒ Object


59
60
61
62
# File 'lib/datagrid/drivers/sequel.rb', line 59

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

#default_cache_key(asset) ⇒ Object


79
80
81
# File 'lib/datagrid/drivers/sequel.rb', line 79

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

#default_order(scope, column_name) ⇒ Object


39
40
41
# File 'lib/datagrid/drivers/sequel.rb', line 39

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

#default_preload(scope, value) ⇒ Object


89
90
91
# File 'lib/datagrid/drivers/sequel.rb', line 89

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

#desc(scope, order) ⇒ Object


31
32
33
# File 'lib/datagrid/drivers/sequel.rb', line 31

def desc(scope, order)
  scope.order(::Sequel.desc(::Sequel.lit(order)))
end

#greater_equal(scope, field, value) ⇒ Object


43
44
45
# File 'lib/datagrid/drivers/sequel.rb', line 43

def greater_equal(scope, field, value)
  scope.where(::Sequel.lit("#{prefix_table_name(scope, field)} >= ?", value))
end

#has_column?(scope, column_name) ⇒ Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/datagrid/drivers/sequel.rb', line 51

def has_column?(scope, column_name)
  scope.columns.include?(column_name.to_sym)
end

#less_equal(scope, field, value) ⇒ Object


47
48
49
# File 'lib/datagrid/drivers/sequel.rb', line 47

def less_equal(scope, field, value)
  scope.where(::Sequel.lit("#{prefix_table_name(scope, field)} <= ?", value))
end

#normalized_column_type(scope, field) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/datagrid/drivers/sequel.rb', line 64

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

#reverse_order(scope) ⇒ Object


35
36
37
# File 'lib/datagrid/drivers/sequel.rb', line 35

def reverse_order(scope)
  super
end

#to_scope(scope) ⇒ Object


14
15
16
17
# File 'lib/datagrid/drivers/sequel.rb', line 14

def to_scope(scope)
  return scope if scope.is_a?(::Sequel::Dataset)
  scope.where({})
end

#where(scope, attribute, value) ⇒ Object


23
24
25
# File 'lib/datagrid/drivers/sequel.rb', line 23

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