Class: HQ::GraphQL::Filters::Filter

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, HQ::GraphQL::FilterOperations
Defined in:
lib/hq/graphql/filters.rb

Constant Summary

Constants included from HQ::GraphQL::FilterOperations

HQ::GraphQL::FilterOperations::OPERATIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter, table:) ⇒ Filter

Returns a new instance of Filter.



78
79
80
81
82
83
# File 'lib/hq/graphql/filters.rb', line 78

def initialize(filter, table:)
  @table = table
  @column = filter.field
  @operation = filter.operation
  @value = filter.value
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



76
77
78
# File 'lib/hq/graphql/filters.rb', line 76

def column
  @column
end

#operationObject (readonly)

Returns the value of attribute operation.



76
77
78
# File 'lib/hq/graphql/filters.rb', line 76

def operation
  @operation
end

#tableObject (readonly)

Returns the value of attribute table.



76
77
78
# File 'lib/hq/graphql/filters.rb', line 76

def table
  @table
end

#valueObject (readonly)

Returns the value of attribute value.



76
77
78
# File 'lib/hq/graphql/filters.rb', line 76

def value
  @value
end

Class Method Details

.class_from_column(column) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hq/graphql/filters.rb', line 47

def self.class_from_column(column)
  case column.type
  when :boolean
    BooleanFilter
  when :date, :datetime
    DateFilter
  when :decimal, :integer
    NumericFilter
  when :string, :text
    StringFilter
  when :uuid
    UuidFilter
  end
end

.for(filter, **options) ⇒ Object



43
44
45
# File 'lib/hq/graphql/filters.rb', line 43

def self.for(filter, **options)
  class_from_column(filter.field).new(filter, **options)
end

.validate_operations(*operations) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/hq/graphql/filters.rb', line 62

def self.validate_operations(*operations)
  valid_operations = operations + [WITH]
  validates :operation, inclusion: {
    in: valid_operations,
    message: "only supports the following operations: #{valid_operations.map(&:name).join(", ")}"
  }
end

.validate_value(**options) ⇒ Object



70
71
72
# File 'lib/hq/graphql/filters.rb', line 70

def self.validate_value(**options)
  validates :value, **options, unless: ->(filter) { filter.operation == WITH }
end

Instance Method Details

#display_error_messageObject



85
86
87
88
89
# File 'lib/hq/graphql/filters.rb', line 85

def display_error_message
  return unless errors.any?
  messages = errors.messages.values.join(", ")
  "#{column.name.camelize(:lower)} (type: #{column.type}, operation: #{operation.name}, value: \"#{value}\"): #{messages}"
end

#to_arelObject



91
92
93
# File 'lib/hq/graphql/filters.rb', line 91

def to_arel
  operation.to_arel(table: table, column_name: column.name, value: value)
end

#validate_boolean_valuesObject



95
96
97
98
99
# File 'lib/hq/graphql/filters.rb', line 95

def validate_boolean_values
  is_valid = BOOLEAN_VALUES.any? { |v| value.casecmp(v) == 0 }
  return if is_valid
  errors.add(:value, "WITH operation only supports boolean values (#{BOOLEAN_VALUES.join(", ")})")
end