Skip to content

Instantly share code, notes, and snippets.

@silviorelli
silviorelli / paginate.rb
Created September 10, 2020 14:09 — forked from be9/paginate.rb
kaminari + JSON API pagination helper
def paginate(scope, default_per_page = 20)
collection = scope.page(params[:page]).per((params[:per_page] || default_per_page).to_i)
current, total, per_page = collection.current_page, collection.num_pages, collection.limit_value
return [{
pagination: {
current: current,
previous: (current > 1 ? (current - 1) : nil),
next: (current == total ? nil : (current + 1)),
@silviorelli
silviorelli / application_controller.rb
Created July 1, 2020 15:21 — forked from mwlang/application_controller.rb
Logging headers, params, and body to console -- useful for debugging what a mobile app is sending Rails API backend server.
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
around_action :global_request_logging
def global_request_logging
http_request_header_keys = request.headers.env.keys.select{|header_name| header_name.match("^HTTP.*|^X-User.*")}
http_request_headers = request.headers.env.select{|header_name, header_value| http_request_header_keys.index(header_name)}
puts '*' * 40
pp request.method
@silviorelli
silviorelli / ml-ruby.md
Created May 6, 2016 07:36 — forked from gbuesing/ml-ruby.md
Resources for Machine Learning in Ruby

Resources for Machine Learning in Ruby

Gems