Module: JSONAPI::Serialization::IncludeFiltering

Included in:
IncludesSerialization
Defined in:
lib/json_api/serialization/concerns/include_filtering.rb

Instance Method Summary collapse

Instance Method Details

#filter_by_query(loaded_array, resource_scope) ⇒ Object



36
37
38
39
# File 'lib/json_api/serialization/concerns/include_filtering.rb', line 36

def filter_by_query(loaded_array, resource_scope)
  valid_ids = resource_scope.where(id: loaded_array.filter_map(&:id)).pluck(:id).to_set
  loaded_array.select { |r| valid_ids.include?(r.id) }
end

#filter_by_where_hash(loaded_array, resource_scope, related_klass) ⇒ Object



16
17
18
19
20
21
# File 'lib/json_api/serialization/concerns/include_filtering.rb', line 16

def filter_by_where_hash(loaded_array, resource_scope, related_klass)
  hash = where_values_hash_for_scope(resource_scope, related_klass)
  return filter_by_query(loaded_array, resource_scope) if hash.blank?

  loaded_array.select { |r| record_matches_where_hash?(r, hash) }
end

#filter_loaded_records(association, related_klass) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/json_api/serialization/concerns/include_filtering.rb', line 6

def filter_loaded_records(association, related_klass)
  loaded_array = association.target.respond_to?(:to_a) ? association.target.to_a : Array(association.target)
  return [] if loaded_array.empty?

  resource_scope = ResourceLoader.find_for_model(related_klass).records
  return loaded_array if resource_scope.where_clause.empty?

  filter_by_where_hash(loaded_array, resource_scope, related_klass)
end

#record_matches_where_hash?(record, hash) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/json_api/serialization/concerns/include_filtering.rb', line 29

def record_matches_where_hash?(record, hash)
  hash.all? do |attr, val|
    r_val = record.read_attribute(attr)
    val.is_a?(Array) ? val.include?(r_val) : r_val == val
  end
end

#where_values_hash_for_scope(resource_scope, related_klass) ⇒ Object



23
24
25
26
27
# File 'lib/json_api/serialization/concerns/include_filtering.rb', line 23

def where_values_hash_for_scope(resource_scope, related_klass)
  resource_scope.where_values_hash(related_klass.table_name)
rescue StandardError
  {}
end