Class: JsonApiFilter::FilterAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_filter/filter_attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allowed_filters, params) ⇒ FilterAttributes

Returns a new instance of FilterAttributes.

Parameters:

  • allowed_filters (ActiveSupport::HashWithIndifferentAccess, Hash)
  • params (ActiveSupport::HashWithIndifferentAccess, Hash)


8
9
10
11
# File 'lib/json_api_filter/filter_attributes.rb', line 8

def initialize(allowed_filters, params)
  @allowed_filters = allowed_filters
  @params = params
end

Instance Attribute Details

#allowed_filtersObject (readonly)

Returns the value of attribute allowed_filters.



3
4
5
# File 'lib/json_api_filter/filter_attributes.rb', line 3

def allowed_filters
  @allowed_filters
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/json_api_filter/filter_attributes.rb', line 3

def params
  @params
end

Instance Method Details

#nested_allowed_filterArray<Symbol>

Keys of nested allowed filters only one level of nesting supported

Examples:

for: permitted_filters [:id, :author, :name, users: [:id]]
it returns: :users

Returns:

  • (Array<Symbol>)


31
32
33
# File 'lib/json_api_filter/filter_attributes.rb', line 31

def nested_allowed_filter
  allowed_filters.select{|f| f.class == Hash}.map(&:keys).flatten
end

#processActiveSupport::HashWithIndifferentAccess, Hash

Returns:

  • (ActiveSupport::HashWithIndifferentAccess, Hash)


14
15
16
17
18
19
20
21
# File 'lib/json_api_filter/filter_attributes.rb', line 14

def process
  params.select do |k,v|
    allowed_filters.include?(k.to_sym) ||
      allowed_filters.include?(k.to_s) ||
      nested_allowed_filter.include?(k.to_sym) ||
      nested_allowed_filter.include?(k.to_s)
  end
end