Class: ActiveHashRelation::FilterApplier
- Inherits:
-
Object
- Object
- ActiveHashRelation::FilterApplier
- Includes:
- AssociationFilters, ColumnFilters, Helpers, LimitFilters, ScopeFilters, SortFilters
- Defined in:
- lib/active_hash_relation/filter_applier.rb
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Instance Method Summary collapse
- #apply_filters ⇒ Object
- #filter_class(resource_name) ⇒ Object
-
#initialize(resource, params, include_associations: false, model: nil, is_not: false) ⇒ FilterApplier
constructor
A new instance of FilterApplier.
- #run_not_filters ⇒ Object
- #run_or_filters ⇒ Object
Methods included from LimitFilters
Methods included from SortFilters
Methods included from ScopeFilters
Methods included from AssociationFilters
Methods included from ColumnFilters
#filter_boolean, #filter_date, #filter_datetime, #filter_decimal, #filter_float, #filter_integer, #filter_string, #filter_text
Methods included from Helpers
#engine_name, #find_model, #model_class_name
Constructor Details
#initialize(resource, params, include_associations: false, model: nil, is_not: false) ⇒ FilterApplier
Returns a new instance of FilterApplier.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/active_hash_relation/filter_applier.rb', line 12 def initialize(resource, params, include_associations: false, model: nil, is_not: false) @configuration = Module.nesting.last.configuration @resource = resource if params.respond_to?(:to_unsafe_h) @params = HashWithIndifferentAccess.new(params.to_unsafe_h) else @params = HashWithIndifferentAccess.new(params) end @include_associations = include_associations @model = find_model(model) is_not ? @is_not = true : @is_not = false end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
10 11 12 |
# File 'lib/active_hash_relation/filter_applier.rb', line 10 def configuration @configuration end |
Instance Method Details
#apply_filters ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 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 64 65 66 67 |
# File 'lib/active_hash_relation/filter_applier.rb', line 25 def apply_filters run_or_filters run_not_filters table_name = @model.table_name @model.columns.each do |c| next if @params[c.name.to_s].nil? next if @params[c.name.to_s].is_a?(String) && @params[c.name.to_s].blank? case c.type when :integer if @model.defined_enums[c.name] && @model.defined_enums[c.name][@params[c.name]] @params[c.name] = @model.defined_enums[c.name][@params[c.name]] end @resource = filter_integer(@resource, c.name, table_name, @params[c.name]) when :float @resource = filter_float(@resource, c.name, table_name, @params[c.name]) when :decimal @resource = filter_decimal(@resource, c.name, table_name, @params[c.name]) when :string, :uuid, :text @resource = filter_string(@resource, c.name, table_name, @params[c.name]) when :date @resource = filter_date(@resource, c.name, table_name, @params[c.name]) when :datetime, :timestamp @resource = filter_datetime(@resource, c.name, table_name, @params[c.name]) when :boolean @resource = filter_boolean(@resource, c.name, table_name, @params[c.name]) end end if @params.include?(:scopes) if ActiveHashRelation.configuration.filter_active_record_scopes @resource = filter_scopes(@resource, @params[:scopes], @model) else Rails.logger.warn('Ignoring ActiveRecord scope filters because they are not enabled') end end @resource = filter_associations(@resource, @params, @model) if @include_associations @resource = apply_limit(@resource, @params[:limit]) if @params.include?(:limit) @resource = apply_sort(@resource, @params[:sort], @model) if @params.include?(:sort) return @resource end |
#filter_class(resource_name) ⇒ Object
69 70 71 |
# File 'lib/active_hash_relation/filter_applier.rb', line 69 def filter_class(resource_name) "#{configuration.filter_class_prefix}#{resource_name.pluralize}#{configuration.filter_class_suffix}".constantize end |
#run_not_filters ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_hash_relation/filter_applier.rb', line 92 def run_not_filters if @params[:not].is_a?(Hash) && !@params[:not].blank? @resource = self.class.new( @resource, @params[:not], include_associations: @include_associations, is_not: true ).apply_filters end end |
#run_or_filters ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/active_hash_relation/filter_applier.rb', line 73 def run_or_filters if @params[:or].is_a?(Array) if ActiveRecord::VERSION::MAJOR < 5 return Rails.logger.warn("OR query is supported on ActiveRecord 5+") end if @params[:or].length >= 2 array = @params[:or].map do |or_param| self.class.new(@resource, or_param, include_associations: @include_associations).apply_filters end @resource = @resource.merge(array[0]) array[1..-1].each{|query| @resource = @resource.or(query)} else Rails.logger.warn("Can't run an OR with 1 element!") end end end |