module BeyondTheApi::IndexRenderHelpers

Constants

API_DEFAULT_PAGE_SIZE

Protected Instance Methods

apply_filters(list, *scopes) click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 47
def apply_filters(list, *scopes)
  scopes.each do |scope|
    param_name = scope.to_s.sub('by_', '')
    param = params[param_name]
    list = list.send(scope, param) if param.present? && list.respond_to?(scope)
  end
  list
end
enum_list_to_array_hashes(list) click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 56
def enum_list_to_array_hashes(list)
  array = []
  list.each do |name, id|
    array << {
      id: id,
      name: name
    }
  end
  array
end
index_params_to_render(options) click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 19
def index_params_to_render(options)
  params_to_render = { json: @list, meta: @meta, current_user: current_user }
  serializer = options[:serializer]
  params_to_render[:each_serializer] = serializer if serializer
  add_fields_and_include_to_render_options(params_to_render)
end
paginate(default) click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 37
def paginate(default)
  return @list unless paginate?(default)
  @list.page(page).per_page(params[:per_page] || default)
end
paginate_array(default) click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 42
def paginate_array(default)
  return @list unless paginate?(default)
  @list.paginate(page: page, per_page: params[:per_page] || default)
end
render_json_list(list, options = {}) click to toggle source

Index ##

# File lib/beyond_the_api/index_render_helpers.rb, line 10
def render_json_list(list, options = {})
  @list = list
  @list = paginate(options[:default_page_size] || API_DEFAULT_PAGE_SIZE)
  @meta[:total_pages] = (options[:total_pages] || @list.total_pages) if total_pages?
  @meta[:total_count] ||= (options[:total_count] || @list.count) if total_count?

  render index_params_to_render(options)
end
render_json_list_enum(list, name, options = {}) click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 26
def render_json_list_enum(list, name, options = {})
  @list = list
  @list = paginate_array(options[:default_page_size] || API_DEFAULT_PAGE_SIZE)
  @meta[:total_count] = @list.count if total_count?
  @meta[:total_pages] = @list.total_pages if total_pages?
  render json: {
    name => @list,
    meta: @meta
  }
end

Private Instance Methods

page() click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 77
def page
  params[:page] || 1
end
paginate?(default) click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 81
def paginate?(default)
  params[:per_page] || !default == :unlimited
end
total_count?() click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 73
def total_count?
  params[:total_count].to_s == 'true'
end
total_pages?() click to toggle source
# File lib/beyond_the_api/index_render_helpers.rb, line 69
def total_pages?
  @list.respond_to?(:total_pages) && params[:total_pages].to_s == 'true'
end