Module: YiffSpace::Concerns::AttributeMatchers

Defined in:
lib/yiffspace/concerns/attribute_matchers.rb

Instance Method Summary collapse

Instance Method Details

#add_range_relation(arr, field) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yiffspace/concerns/attribute_matchers.rb', line 37

def add_range_relation(arr, field, &)
  return all if arr.nil?

  q = block_given? ? yield : all
  case arr[0]
  when :eq
    if arr[1].is_a?(Time)
      q.where.between(field => arr[1].all_day)
    else
      q.where(field => arr[1])
    end
  when :gt
    q.where.gt(field => arr[1])
  when :gte
    q.where.gte(field => arr[1])
  when :lt
    q.where.lt(field => arr[1])
  when :lte
    q.where.lte(field => arr[1])
  when :in
    q.where.in(field => arr[1])
  when :between
    q.where.between(field => arr[1]..arr[2])
  else
    q.none
  end
end

#attribute_present(attribute, value) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/yiffspace/concerns/attribute_matchers.rb', line 86

def attribute_present(attribute, value, &)
  return all if value.nil?

  q = block_given? ? yield : all
  if value.to_s.truthy?
    q.where.not(attribute => nil)
  elsif value.to_s.falsy?
    q.where(attribute => nil)
  else
    q.none
  end
end

#boolean_attribute_matches(attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/yiffspace/concerns/attribute_matchers.rb', line 6

def boolean_attribute_matches(attribute, value, &)
  return all if value.nil?

  if value.to_s.truthy?
    value = true
  elsif value.to_s.falsy?
    value = false
  else
    raise(ArgumentError, "value must be truthy or falsy")
  end

  q = block_given? ? yield : all
  q.where(attribute => value)
end

#datetime_attribute_matches(attribute, range) ⇒ Object

datetime columns return ActiveSupport::TimeWithZone



33
34
35
# File 'lib/yiffspace/concerns/attribute_matchers.rb', line 33

def datetime_attribute_matches(attribute, range, &)
  numeric_attribute_matches(attribute, range, &)
end

#ip_attribute_matches(attribute, value) ⇒ Object



79
80
81
82
83
84
# File 'lib/yiffspace/concerns/attribute_matchers.rb', line 79

def ip_attribute_matches(attribute, value, &)
  return all if value.nil?

  q = block_given? ? yield : all
  q.where.inet_contained_within_or_equals(attribute => value)
end

#numeric_attribute_matches(attribute, value) ⇒ Object

range: “5”, “>5”, “<5”, “>=5”, “<=5”, “5..10”, “5,6,7”



22
23
24
25
26
27
28
29
30
# File 'lib/yiffspace/concerns/attribute_matchers.rb', line 22

def numeric_attribute_matches(attribute, value, &)
  return all if value.nil?

  value = value.to_s.strip
  column = QueryHelper.get_column(attribute, table_name)
  parsed_range = ParseValue.range(value, column.type)

  add_range_relation(parsed_range, attribute, &)
end

#text_attribute_matches(attribute, value, convert_to_wildcard: false) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yiffspace/concerns/attribute_matchers.rb', line 65

def text_attribute_matches(attribute, value, convert_to_wildcard: false, &)
  return all if value.nil?

  value = "*#{value}*" if convert_to_wildcard && value.exclude?("*")
  q = block_given? ? yield : all
  if value.is_a?(Array)
    q.where.ilike_any(attribute => value)
  elsif value =~ /\*/
    q.where.ilike(attribute => value)
  else
    q.where.tsquery(attribute => value)
  end
end