Class: RestPack::Serializer::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/restpack_serializer/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, params = {}, scope = nil, context = {}) ⇒ Options

Returns a new instance of Options.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/restpack_serializer/options.rb', line 7

def initialize(serializer, params = {}, scope = nil, context = {})
  params.symbolize_keys! if params.respond_to?(:symbolize_keys!)

  @page = params[:page] ? params[:page].to_i : 1
  @page_size = params[:page_size] ? params[:page_size].to_i : RestPack::Serializer.config.page_size
  @include = params[:include] ? params[:include].split(',') : []
  @filters = filters_from_params(params, serializer)
  @sorting = sorting_from_params(params, serializer)
  @serializer = serializer
  @model_class = serializer.model_class
  @scope = scope || model_class.send(:all)
  @context = context
  @include_links = true
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def context
  @context
end

#filtersObject

Returns the value of attribute filters.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def filters
  @filters
end

#includeObject

Returns the value of attribute include.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def include
  @include
end

Returns the value of attribute include_links.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def include_links
  @include_links
end

#model_classObject

Returns the value of attribute model_class.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def model_class
  @model_class
end

#pageObject

Returns the value of attribute page.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def page
  @page
end

#page_sizeObject

Returns the value of attribute page_size.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def page_size
  @page_size
end

#scopeObject

Returns the value of attribute scope.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def scope
  @scope
end

#serializerObject

Returns the value of attribute serializer.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def serializer
  @serializer
end

#sortingObject

Returns the value of attribute sorting.



3
4
5
# File 'lib/restpack_serializer/options.rb', line 3

def sorting
  @sorting
end

Instance Method Details

#default_page_size?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/restpack_serializer/options.rb', line 33

def default_page_size?
  @page_size == RestPack::Serializer.config.page_size
end

#filters_as_url_paramsObject



37
38
39
# File 'lib/restpack_serializer/options.rb', line 37

def filters_as_url_params
  @filters.sort.map { |k,v| map_filter_ids(k,v) }.join('&')
end

#scope_with_filtersObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/restpack_serializer/options.rb', line 22

def scope_with_filters
  scope_filter = {}

  @filters.keys.each do |filter|
    value = query_to_array(@filters[filter])
    scope_filter[filter] = value
  end

  @scope.where(scope_filter)
end

#sorting_as_url_paramsObject



41
42
43
44
# File 'lib/restpack_serializer/options.rb', line 41

def sorting_as_url_params
  sorting_values = sorting.map { |k, v| v == :asc ? k : "-#{k}" }.join(',')
  "sort=#{sorting_values}"
end