Module: QueryStringInterface

Defined in:
lib/query_string_interface.rb,
lib/query_string_interface/filter.rb,
lib/query_string_interface/helpers.rb,
lib/query_string_interface/version.rb,
lib/query_string_interface/sorting_filters.rb,
lib/query_string_interface/filter_collection.rb,
lib/query_string_interface/parsers/array_parser.rb,
lib/query_string_interface/parsers/regex_parser.rb,
lib/query_string_interface/parsers/number_parser.rb,
lib/query_string_interface/parsers/date_time_parser.rb,
lib/query_string_interface/parsers/boolean_and_nil_parser.rb

Defined Under Namespace

Modules: Helpers, Parsers Classes: Filter, FilterCollection, SortingFilters

Constant Summary collapse

NORMAL_CONDITIONAL_OPERATORS =
[:exists, :gte, :gt, :lte, :lt, :ne, :size, :near, :within]
ARRAY_CONDITIONAL_OPERATORS =
[:all, :in, :nin]
CONDITIONAL_OPERATORS =
ARRAY_CONDITIONAL_OPERATORS + NORMAL_CONDITIONAL_OPERATORS
SORTING_OPERATORS =
[:asc, :desc]
FIELD_FILTERING_OPERATORS =
[:only, :except]
OR_OPERATOR =
:or
OPERATORS =
CONDITIONAL_OPERATORS + SORTING_OPERATORS + [OR_OPERATOR]
ATTRIBUTE_REGEX =
/^(.*)\.(#{(OPERATORS).join('|')})$/
OPERATOR_REGEX =
/^.*\.(#{CONDITIONAL_OPERATORS.join('|')})$/
ORDER_BY_PARAMETER =
:order_by
PAGINATION_PARAMTERS =
[:per_page, :page]
FRAMEWORK_PARAMETERS =
[:controller, :action, :format]
CONTROL_PARAMETERS =
[:disable_default_filters]
RESERVED_PARAMETERS =
FRAMEWORK_PARAMETERS + PAGINATION_PARAMTERS + [ORDER_BY_PARAMETER] + CONTROL_PARAMETERS + FIELD_FILTERING_OPERATORS
VERSION =
"0.6.0"

Instance Method Summary collapse

Instance Method Details

#default_filtering_optionsObject



23
24
25
# File 'lib/query_string_interface.rb', line 23

def default_filtering_options
  {}
end

#default_pagination_optionsObject



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

def default_pagination_options
  { :per_page => 12, :page => 1 }
end

#default_sorting_optionsObject



27
28
29
# File 'lib/query_string_interface.rb', line 27

def default_sorting_options
  []
end

#field_filtering_options(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/query_string_interface.rb', line 59

def field_filtering_options(options={})
  options = options.with_indifferent_access

  if options[:only]
    { :only => parse_array(options[:only]) }.with_indifferent_access
  elsif options[:except]
    { :except => parse_array(options[:except]) }.with_indifferent_access
  end
end

#filtering_attributes_to_replaceObject



39
40
41
# File 'lib/query_string_interface.rb', line 39

def filtering_attributes_to_replace
  {}
end

#filtering_options(options = {}) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/query_string_interface.rb', line 47

def filtering_options(options={})
  QueryStringInterface::FilterCollection.new(
    only_filtering(options),
    options.has_key?(:disable_default_filters) ? {} : default_filtering_options,
    filtering_attributes_to_replace
  ).parse
end

#only_filtering(options = {}) ⇒ Object



69
70
71
# File 'lib/query_string_interface.rb', line 69

def only_filtering(options={})
  options.with_indifferent_access.except(*RESERVED_PARAMETERS)
end

#pagination_options(options = {}) ⇒ Object



43
44
45
# File 'lib/query_string_interface.rb', line 43

def pagination_options(options={})
  default_pagination_options.with_indifferent_access.merge(options)
end

#sorting_attributes_to_replaceObject



35
36
37
# File 'lib/query_string_interface.rb', line 35

def sorting_attributes_to_replace
  {}
end

#sorting_options(options = {}) ⇒ Object



55
56
57
# File 'lib/query_string_interface.rb', line 55

def sorting_options(options={})
  QueryStringInterface::SortingFilters.new(options, default_sorting_options, sorting_attributes_to_replace).parse
end