Skip to content

Instantly share code, notes, and snippets.

View dblock's full-sized avatar
🐝
Alexa, ask the future of America to be great again! (try it)

Daniel (dB.) Doubrovkine dblock

🐝
Alexa, ask the future of America to be great again! (try it)
View GitHub Profile
@dblock
dblock / api_page_helper.rb
Created November 2, 2011 23:08
pagination helper with Grape
module ApiPageHelper
PAGINATE_OPTIONS = {
:default_page_size => 10
}
PAGINATE_PARAMS = [ "page", "offset", "size" ]
def paginate(coll, options = {})
options = PAGINATE_OPTIONS.merge(options)
if params[:page]
page = params[:page].to_i
size = (params[:size] || options[:default_page_size]).to_i
@dblock
dblock / assert_url_helper.rb
Last active September 18, 2018 03:47
A monkey patch for serving compressed assets for Rails 3.0 (asset_tag_helper.rb) and another for Rails 3.1 (asset_paths.rb).
# Rails 4
require 'action_view/helpers/asset_url_helper'
module ActionView
module Helpers
module AssetUrlHelper
def accept_encoding?(encoding)
request = self.request if respond_to?(:request)
return false unless request
@dblock
dblock / assets.rake
Created December 10, 2011 20:12
Upload assets to S3, preserve two most recent versions.
namespace :assets do
# uploads assets to s3 under assets/githash, deletes stale assets
task :uploadToS3, [ :to ] => :environment do |t, args|
from = File.join(Rails.root, 'public/assets')
to = args[:to]
hash = (`git rev-parse --short HEAD` || "").chomp
logger.info("[#{Time.now}] fetching keys from #{to}")
existing_objects_hash = {}
@dblock
dblock / oauth_controller.rb
Created December 11, 2011 15:16
An updated OAuth2 controller for a Rails app (implies you have ClientApplication and AccessGrant)
class OauthController < ApplicationController
class ApiOAuthError < StandardError
attr_accessor :code, :description, :uri, :state
def initialize(code, description, uri = nil, state = nil)
@code = code
@description = description
@uri = uri
@dblock
dblock / carrierwave.rb
Created January 31, 2012 13:55
Delayed image processing with CarrierWave
CarrierWave.configure do |config|
...
end
Mongoid::Document::ClassMethods.send(:include, DelayedImageProcessing)
module CarrierWave
# http://sleeplesscoding.blogspot.com/2011/09/recreate-single-version-of.html
# Note: is_processing_delayed should be set before calling recreate_version! if the version depends on it.
@dblock
dblock / grape-rackup-with-static.rb
Created February 20, 2012 17:53
Rackup static pages along with a Grape API, inspired by Rack::TryStatic
class App
def initialize(options)
@try = ['', *options.delete(:try)]
@static = ::Rack::Static.new(
lambda { [404, {}, []] },
options)
end
def call(env)
@dblock
dblock / garb_model.rb
Created March 31, 2012 14:59
An iterator over all Google Analytics results with Garb
module Garb
module Model
def all(profile, options = {}, &block)
limit = options.delete(:limit)
total = 0
while ((rs = results(profile, options)) && rs.any?)
rs.each do |r|
yield r
total += 1
break if limit and total >= limit
@dblock
dblock / failures_formatter.rb
Created May 4, 2012 19:52
RSpec tests broken up into suites with retry.
# inspired by https://github.com/rspec/rspec-core/pull/596
require 'rspec/core/formatters/base_formatter'
module RSpec
module Core
module Formatters
class FailuresFormatter < BaseFormatter
def dump_failures
return if failed_examples.empty?
@dblock
dblock / action_mailer_with_text_part.rb
Created May 17, 2012 14:57
Insert an automatic text MIME part into HTML e-mail.
#
# Insert an automatic text MIME part into HTML e-mail.
# (c) Daniel Doubrovkine, Art.sy 2012
# MIT License
#
class ActionMailerWithTextPart < ActionMailer::Base
def collect_responses_and_parts_order(headers)
responses, parts_order = super(headers)
html_part = responses.detect { |response| response[:content_type] == "text/html" }
@dblock
dblock / mongoid_criteria.rb
Created May 24, 2012 19:38
Mongoid::Criteria each_by iterator
module Mongoid
class Criteria
def each_by(by, &block)
idx = 0
total = 0
set_limit = options[:limit]
while ((results = ordered_clone.limit(by).skip(idx)) && results.any?)
results.each do |result|
return self if set_limit and set_limit >= total