Class: ApplicationRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/application_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fuzzy_search(term, **cols) ⇒ Object



4
5
6
7
# File 'app/models/application_record.rb', line 4

def self.fuzzy_search(term, **cols)
  sanitized = sanitize_for_search term, **cols
  select(Arel.sql("`#{table_name}`.*, #{sanitized} AS search_score"))
end

.match_search(term, **cols) ⇒ Object



9
10
11
12
# File 'app/models/application_record.rb', line 9

def self.match_search(term, **cols)
  sanitized = sanitize_for_search term, **cols
  select(Arel.sql("`#{table_name}`.*, #{sanitized} AS search_score")).where(sanitized)
end

.sanitize_for_search(term, **cols) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/application_record.rb', line 26

def self.sanitize_for_search(term, **cols)
  cols = cols.map do |k, v|
    if v.is_a?(Array)
      v.map { |vv| "#{sanitize_name k}.#{sanitize_name vv}" }.join(', ')
    else
      "#{sanitize_name k}.#{sanitize_name v}"
    end
  end.join(', ')

  ActiveRecord::Base.send(:sanitize_sql_array, ["MATCH (#{cols}) AGAINST (? IN BOOLEAN MODE)", term])
end

.sanitize_name(name) ⇒ Object



14
15
16
# File 'app/models/application_record.rb', line 14

def self.sanitize_name(name)
  name.to_s.delete('`').insert(0, '`').insert(-1, '`')
end

.sanitize_sql_in(ary) ⇒ Object



38
39
40
41
42
43
# File 'app/models/application_record.rb', line 38

def self.sanitize_sql_in(ary)
  return '(NULL)' unless ary.present? && ary.respond_to?(:map)

  res = ActiveRecord::Base.sanitize_sql_array([ary.map { |_e| '?' }.join(', '), *ary])
  "(#{res})"
end

.useful_err_msgObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/application_record.rb', line 58

def self.useful_err_msg
  [
    'The inverted database guide has found an insurmountable problem. Please poke it with a ' \
    'paperclip before anyone finds out.',
    'The modular cable meter has found a problem. You need to kick your IT technician in the ' \
    'shins immediately.',
    'The integral output port has found a problem. Please take it back to the shop and take ' \
    'the rest of the day off.',
    'The integral expansion converter has encountered a terminal error. You must take legal ' \
    'advice urgently.',
    'Congratulations. You have reached the end of the internet.',
    'The Spanish Inquisition raised an unexpected error. Cannot continue without comfy-chair-interrogation.',
    'The server halted in an after-you loop.',
    'A five-level precedence operation shifted too long and cannot be recovered without data loss. ' \
    'Please re-enable the encryption protocol.',
    'The server\'s headache has not improved in the last 24 hours. It needs to be rebooted.',
    'The primary LIFO data recipient is currently on a holiday and will not be back before next Thursday.',
    'The operator is currently trying to solve their Rubik\'s cube. We will come back to you when the ' \
    'second layer is completed.',
    'The encryption protocol offered by the client predates the invention of irregular logarithmic ' \
    'functions.',
    'The data in the secondary (backup) user registry is corrupted and needs to be re-filled with ' \
    'random data again.',
    'This community has reached a critical mass and collapsed into a black hole. Currently trying to ' \
    'recover using Hawking radiation.',
    'Operations are on pause while we attempt to recapture the codidactyl. Please hold.',
    'The data center is on fire. Please hold while we activate fire suppression systems.',
    'The reciprocal controller flag is set incorrectly. Please stand on your head and rickroll yourself to fix this.'
  ]
end

.with_lax_group_rulesObject

This is a BRILLIANT idea. BRILLIANT, I tell you.



46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/application_record.rb', line 46

def self.with_lax_group_rules
  return unless block_given?

  transaction do
    connection.execute 'SET @old_sql_mode = @@sql_mode'
    connection.execute "SET SESSION sql_mode = REPLACE(REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY,', ''), " \
                       "'ONLY_FULL_GROUP_BY', '')"
    yield
    connection.execute 'SET SESSION sql_mode = @old_sql_mode'
  end
end

Instance Method Details

#attributes_print(join: ', ') ⇒ Object



22
23
24
# File 'app/models/application_record.rb', line 22

def attributes_print(join: ', ')
  attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(join)
end

#match_search(term, **cols) ⇒ Object



18
19
20
# File 'app/models/application_record.rb', line 18

def match_search(term, **cols)
  ApplicationRecord.match_search(term, **cols)
end