Module: Effective::Resources::Sql

Included in:
Effective::Resource
Defined in:
app/models/effective/resources/sql.rb

Instance Method Summary collapse

Instance Method Details

#column(name) ⇒ Object



5
6
7
8
# File 'app/models/effective/resources/sql.rb', line 5

def column(name)
  name = name.to_s
  columns.find { |col| col.name == name || (belongs_to(name) && col.name == belongs_to(name).foreign_key) }
end

#column_namesObject



14
15
16
# File 'app/models/effective/resources/sql.rb', line 14

def column_names
  @column_names ||= columns.map { |col| col.name }
end

#columnsObject



10
11
12
# File 'app/models/effective/resources/sql.rb', line 10

def columns
  klass.columns
end

#ilikeObject



101
102
103
# File 'app/models/effective/resources/sql.rb', line 101

def ilike
  @ilike ||= (postgres? ? 'ILIKE' : 'LIKE')  # Only Postgres supports ILIKE, Mysql and Sqlite3 use LIKE
end

#is_null(sql_column) ⇒ Object



115
116
117
# File 'app/models/effective/resources/sql.rb', line 115

def is_null(sql_column)
  mysql? == true ? "ISNULL(#{sql_column})" : "#{sql_column} IS NULL"
end

#max_idObject



22
23
24
25
# File 'app/models/effective/resources/sql.rb', line 22

def max_id
  return 999999 unless klass.respond_to?(:unscoped)
  @max_id ||= klass.unscoped.maximum(klass.primary_key).to_i
end

#mysql?Boolean

Returns:

  • (Boolean)


110
111
112
113
# File 'app/models/effective/resources/sql.rb', line 110

def mysql?
  return @mysql unless @mysql.nil?
  @mysql ||= (klass.connection.kind_of?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) rescue false)
end

#postgres?Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'app/models/effective/resources/sql.rb', line 105

def postgres?
  return @postgres unless @postgres.nil?
  @postgres ||= (klass.connection.kind_of?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) rescue false)
end

#search_columnsObject

Any string or text columns TODO: filter out _type columns for polymorphic



90
91
92
93
# File 'app/models/effective/resources/sql.rb', line 90

def search_columns
  return @_search_columns if @_search_columns
  columns.map { |column| column.name if [:string, :text].include?(column.type) }.compact
end

#search_columns=(name) ⇒ Object



95
96
97
98
99
# File 'app/models/effective/resources/sql.rb', line 95

def search_columns=(name)
  names = Array(name)
  names.each { |name| raise "unknown search column: #{name}" unless column_names.include?(name) }
  @_search_columns = names
end

#sort_columnObject

This tries to figure out the column we should order this collection by. Whatever would match up with the .to_s Unless it’s set from outside by datatables…



73
74
75
76
77
78
79
80
81
# File 'app/models/effective/resources/sql.rb', line 73

def sort_column
  return @_sort_column if @_sort_column

  ['name', 'title', 'label', 'subject', 'full_name', 'first_name', 'email', 'number', 'description'].each do |name|
    return name if column_names.include?(name)
  end

  klass.primary_key
end

#sort_column=(name) ⇒ Object



83
84
85
86
# File 'app/models/effective/resources/sql.rb', line 83

def sort_column=(name)
  raise "unknown sort column: #{name}" unless column_names.include?(name)
  @_sort_column = name
end

#sql_column(name) ⇒ Object



27
28
29
30
31
32
# File 'app/models/effective/resources/sql.rb', line 27

def sql_column(name)
  column = column(name)
  return nil unless table && column

  [klass.connection.quote_table_name(table.name), klass.connection.quote_column_name(column.name)].join('.')
end

#sql_direction(name) ⇒ Object



34
35
36
# File 'app/models/effective/resources/sql.rb', line 34

def sql_direction(name)
  name.to_s.downcase == 'desc' ? 'DESC'.freeze : 'ASC'.freeze
end

#sql_type(name) ⇒ Object

This is for EffectiveDatatables (col as:) Might be :name, or ‘users.name’



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/effective/resources/sql.rb', line 40

def sql_type(name)
  name = name.to_s.split('.').first

  if belongs_to(name)
    :belongs_to
  elsif (column = column(name))
    column.type
  elsif has_many(name)
    :has_many
  elsif has_one(name)
    :has_one
  elsif belongs_to_polymorphic(name)
    :belongs_to_polymorphic
  elsif has_and_belongs_to_many(name)
    :has_and_belongs_to_many
  elsif active_storage(name)
    :active_storage
  elsif name == 'id' && defined?(EffectiveObfuscation) && klass.respond_to?(:deobfuscate)
    :effective_obfuscation
  elsif name == 'roles' && defined?(EffectiveRoles) && klass.respond_to?(:with_role)
    :effective_roles
  elsif (name.include?('_address') || name.include?('_addresses')) && defined?(EffectiveAddresses) && (klass.new rescue nil).respond_to?(:effective_addresses)
    :effective_addresses
  elsif name.ends_with?('_id')
    :integer
  else
    :string
  end
end

#tableObject



18
19
20
# File 'app/models/effective/resources/sql.rb', line 18

def table
  klass.unscoped.table
end