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



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

def column(name)
  name = name.to_s
  bt = belongs_to(name)

  columns.find { |col| col.name == name || (bt && col.name == bt.foreign_key) }
end

#column_namesObject



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

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

#columnsObject



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

def columns
  klass.respond_to?(:columns) ? klass.columns : []
end

#ilikeObject



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

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

#is_null(sql_column) ⇒ Object



123
124
125
# File 'app/models/effective/resources/sql.rb', line 123

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

#mysql?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/effective/resources/sql.rb', line 119

def mysql?
  klass.connection.adapter_name == 'MySQL'
end

#postgres?Boolean

Returns:

  • (Boolean)


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

def postgres?
  klass.connection.adapter_name == 'PostgreSQL'
end

#search_columnsObject

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



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

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



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

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…



83
84
85
86
87
88
89
90
91
# File 'app/models/effective/resources/sql.rb', line 83

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



93
94
95
96
# File 'app/models/effective/resources/sql.rb', line 93

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

#sql_column(name) ⇒ Object



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

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



32
33
34
# File 'app/models/effective/resources/sql.rb', line 32

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

#sql_operation(name, as: nil) ⇒ Object



36
37
38
39
40
41
42
43
# File 'app/models/effective/resources/sql.rb', line 36

def sql_operation(name, as: nil)
  sql_type = (as || sql_type(name))

  case sql_type
  when :boolean, :decimal, :integer, :price, :date, :datetime, :percent then :eq
  else :matches
  end
end

#sql_type(name) ⇒ Object

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/effective/resources/sql.rb', line 47

def sql_type(name)
  name = (name.kind_of?(String) ? name.split('.').first : name.to_s)

  return :belongs_to if belongs_to(name)

  # Skip using columns() cause we dont need to check for belongs_to
  column = columns.find { |col| col.name == name }

  if column.present?
    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.ends_with?('_address') || name.ends_with?('_addresses')) && defined?(EffectiveAddresses) && (klass.new rescue nil).respond_to?(name)
    :effective_addresses
  elsif name.ends_with?('_id')
    :integer
  else
    :string
  end
end

#tableObject



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

def table
  klass.unscoped.table
end