This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if Rails.env.production? | |
require 'new_relic/agent/instrumentation/controller_instrumentation' | |
module NewRelic | |
module Agent | |
module Instrumentation | |
module API | |
def newrelic_request_headers | |
@newrelic_request.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Written by https://github.com/macreery (c) Art.sy, 2012, MIT License | |
module ApiConditionalGetHelper | |
# Borrows generously from http://themomorohoax.com/2009/01/07/using-stale-with-rails-to-return-304-not-modified | |
# See also RFC 2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.3.4 | |
# for explanation of how If-Modified-Since and If-None-Match request headers are handled. | |
def fresh?(metadata = {}) | |
self.last_modified = metadata[:last_modified] | |
self.etag = metadata[:etag] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Written (mostly) by https://github.com/macreery (c) Art.sy, 2012, MIT License | |
module ApiCache | |
# cache wrapper, yields an executable block | |
def self.cache(options = {}) | |
# options set default expiration time and force a miss if specified | |
options = standardize_options(options) | |
cache_options = options[:cache_options] || {} | |
cache_options[:expires_in] = 24.hours if not cache_options[:expires_in] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActionView | |
module Helpers | |
module DateHelper | |
# returns a date range in English | |
# July 3-5, both days are the same month | |
# July 3 - August 5, days are different months | |
# December 20, 2010 - January 10, 2012, days are over a year apart | |
def date_range_in_words(start_date, end_date) | |
# convert to date | |
start_date = start_date.to_date if start_date && start_date.respond_to?(:to_date) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Mongoid | |
class Criteria | |
def sample(n = 1) | |
indexes = (0..self.count-1).sort_by{ rand }.slice(0,n).collect! | |
if n == 1 | |
return self.skip(indexes.first).first | |
else | |
return indexes.map{ |index| self.skip(index).first } | |
end |