Class: TypeScopes::String

Inherits:
TypeScopes show all
Defined in:
lib/type_scopes/string.rb

Constant Summary

Constants inherited from TypeScopes

VERSION

Class Method Summary collapse

Methods inherited from TypeScopes

append_scope, inject, support?

Class Method Details

.inject_for_column(model, name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/type_scopes/string.rb', line 6

def self.inject_for_column(model, name)
  column = model.arel_table[name]
  append_scope(model, :"#{name}_like", lambda { |str, sensitive: true| where(column.matches(str, nil, sensitive)) })
  append_scope(model, :"#{name}_not_like", lambda { |str, sensitive: true| where(column.does_not_match(str, nil, sensitive)) })
  append_scope(model, :"#{name}_ilike", lambda { |str| where(column.matches(str)) })
  append_scope(model, :"#{name}_not_ilike", lambda { |str| where(column.does_not_match(str)) })

  append_scope(model, :"#{name}_contains", lambda { |str, sensitive: true|
    send("#{name}_like", "%#{sanitize_sql_like(str)}%", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_does_not_contain", lambda { |str, sensitive: true|
    send("#{name}_not_like", "%#{sanitize_sql_like(str)}%", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_starts_with", lambda { |str, sensitive: true|
    send("#{name}_like", "#{sanitize_sql_like(str)}%", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_does_not_start_with", lambda { |str, sensitive: true|
    send("#{name}_not_like", "#{sanitize_sql_like(str)}%", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_ends_with", lambda { |str, sensitive: true|
    send("#{name}_like", "%#{sanitize_sql_like(str)}", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_does_not_end_with", lambda { |str, sensitive: true|
    send("#{name}_not_like", "%#{sanitize_sql_like(str)}", sensitive: sensitive)
  })

  append_scope(model, :"#{name}_matches", lambda { |str, sensitive: true| where(column.matches_regexp(str, sensitive)) })
  append_scope(model, :"#{name}_does_not_match", lambda { |str, sensitive: true| where(column.does_not_match_regexp(str, sensitive)) })
end

.typesObject



2
3
4
# File 'lib/type_scopes/string.rb', line 2

def self.types
  ["character", "text", "varchar"].freeze
end