Class: HQ::GraphQL::Filters::Filter
- Inherits:
-
Object
- Object
- HQ::GraphQL::Filters::Filter
- Includes:
- ActiveModel::Validations, HQ::GraphQL::FilterOperations
- Defined in:
- lib/hq/graphql/filters.rb
Direct Known Subclasses
BooleanFilter, DateFilter, NumericFilter, StringFilter, UuidFilter
Constant Summary
Constants included from HQ::GraphQL::FilterOperations
HQ::GraphQL::FilterOperations::OPERATIONS
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .class_from_column(column) ⇒ Object
- .for(filter, **options) ⇒ Object
- .validate_operations(*operations) ⇒ Object
- .validate_value(**options) ⇒ Object
Instance Method Summary collapse
- #display_error_message ⇒ Object
-
#initialize(filter, table:) ⇒ Filter
constructor
A new instance of Filter.
- #to_arel ⇒ Object
- #validate_boolean_values ⇒ Object
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
#column ⇒ Object (readonly)
Returns the value of attribute column.
76 77 78 |
# File 'lib/hq/graphql/filters.rb', line 76 def column @column end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
76 77 78 |
# File 'lib/hq/graphql/filters.rb', line 76 def operation @operation end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
76 77 78 |
# File 'lib/hq/graphql/filters.rb', line 76 def table @table end |
#value ⇒ Object (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, **) class_from_column(filter.field).new(filter, **) 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(**) validates :value, **, unless: ->(filter) { filter.operation == WITH } end |
Instance Method Details
#display_error_message ⇒ Object
85 86 87 88 89 |
# File 'lib/hq/graphql/filters.rb', line 85 def return unless errors.any? = errors..values.join(", ") "#{column.name.camelize(:lower)} (type: #{column.type}, operation: #{operation.name}, value: \"#{value}\"): #{}" end |
#to_arel ⇒ Object
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_values ⇒ Object
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 |