class Rails::Surrender::QueryParamParser
parse the requests query_params
for surrender's controls and validate the formatting
Constants
- COUNT_PARAM
- EXCLUDE_PARAM
- FILTER_PARAM
- IDS_PARAM
- INCLUDE_PARAM
- PAGE_DEFAULT
- PAGE_PARAM
- PER_PAGE_DEFAULT
- PER_PARAM
- Pagination
- SORT_PARAM
- Sort
Attributes
query_params[R]
Public Class Methods
new(query_params)
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 24 def initialize(query_params) @query_params = query_params end
Public Instance Methods
count?()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 67 def count? query_params.key?(COUNT_PARAM) end
exclude()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 32 def exclude @exclude ||= parse_yml(query_params[EXCLUDE_PARAM], :exclude) end
filter()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 59 def filter @filter ||= parse_yml(query_params[FILTER_PARAM], :filter) end
filter?()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 55 def filter? filter.present? end
ids?()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 63 def ids? query_params.key?(IDS_PARAM) end
include()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 28 def include @include ||= parse_yml(query_params[INCLUDE_PARAM], :include) end
paginate?()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 71 def paginate? query_params.key? PAGE_PARAM end
pagination()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 75 def pagination @pagination ||= Pagination.new( page: query_params[PAGE_PARAM]&.to_i || PAGE_DEFAULT, per: query_params[PER_PARAM]&.to_i || PER_PAGE_DEFAULT ) end
sort()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 40 def sort @sort ||= begin sort = String.new(query_params[SORT_PARAM] || '') direction_flag = ['+', '-'].include?(sort[0, 1]) ? sort.slice!(0) : '+' direction = direction_flag == '-' ? 'DESC' : 'ASC' scope_method = "sort_by_#{sort}".gsub('.', '_').to_sym association, attribute = sort_attributes(sort) Sort.new(request: query_params[SORT_PARAM], direction: direction, attribute: attribute, association: association, scope_method: scope_method) end end
sort?()
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 36 def sort? query_params.key? SORT_PARAM end
Private Instance Methods
parse_yml(query_string, action)
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 90 def parse_yml(query_string, action) query_string ||= '' # empty string in case nil is passed Psych.safe_load("[#{query_string.gsub(/(,|:)/, '\1 ')}]") rescue StandardError raise Error, I18n.t('surrender.error.query_string.incorrect_format', param: action) end
sort_attributes(sort)
click to toggle source
# File lib/rails/surrender/helpers/query_param_parser.rb, line 84 def sort_attributes(sort) return sort.split('.') if sort.include? '.' ['', sort] end