Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ control over how the filters are applied to the `Arel` relation.
This example shows how you can implement different approaches for different filters.

```ruby
def self.apply_filter(records, filter, value)
def self.apply_filter(records, filter, value, options)
case filter
when :visibility
records.where('users.publicly_visible = ?', value == :public)
Expand Down
14 changes: 7 additions & 7 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,24 +326,24 @@ def apply_sort(records, order_options)
end
end

def apply_filter(records, filter, value)
def apply_filter(records, filter, value, options = {})
records.where(filter => value)
end

def apply_filters(records, filters)
def apply_filters(records, filters, options = {})
required_includes = []

if filters
filters.each do |filter, value|
if _associations.include?(filter)
if _associations[filter].is_a?(JSONAPI::Association::HasMany)
required_includes.push(filter)
records = apply_filter(records, "#{filter}.#{_associations[filter].primary_key}", value)
records = apply_filter(records, "#{filter}.#{_associations[filter].primary_key}", value, options)
else
records = apply_filter(records, "#{_associations[filter].foreign_key}", value)
records = apply_filter(records, "#{_associations[filter].foreign_key}", value, options)
end
else
records = apply_filter(records, filter, value)
records = apply_filter(records, filter, value, options)
end
end
end
Expand All @@ -362,7 +362,7 @@ def filter_records(filters, options)

records = records(options)
records = apply_includes(records, include_directives)
apply_filters(records, filters)
apply_filters(records, filters, options)
end

def sort_records(records, order_options)
Expand Down Expand Up @@ -603,7 +603,7 @@ def _associate(klass, *attrs)

if resource_class
records = public_send(associated_records_method_name)
records = resource_class.apply_filters(records, filters)
records = resource_class.apply_filters(records, filters, options)
order_options = self.class.construct_order_options(sort_criteria)
records = resource_class.apply_sort(records, order_options)
records = resource_class.apply_pagination(records, paginator, order_options)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/resource/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_has_many_association_filters

# define apply_filters method on post resource to not respect filters
PostResource.instance_eval do
def apply_filters(records, filters)
def apply_filters(records, filters, options)
# :nocov:
records
# :nocov:
Expand All @@ -155,7 +155,7 @@ def apply_filters(records, filters)

# reset method to original implementation
PostResource.instance_eval do
def apply_filters(records, filters)
def apply_filters(records, filters, options)
# :nocov:
super
# :nocov:
Expand Down