This file contains hidden or 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
require 'benchmark' | |
PATTERN = /@@@(.*?)@@@/ | |
TIMES = 1_000 | |
string = 'one @@@two@@@ three' * 1000 | |
Benchmark.bm(10) do |benchmark| | |
benchmark.report('global') do | |
TIMES.times do |
This file contains hidden or 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
unless ENV['RAILS_ROOT'] | |
ENV['RAILS_ROOT'] = Dir.pwd | |
end | |
STDERR.puts("Loading Rails environment...") | |
require File.join(ENV['RAILS_ROOT'], 'config', 'environment') | |
groups = CharacteristicGroup.filter do | |
with(:name).in(['Gender', 'Age', 'Lifestage', 'Internal QA']) | |
end |
This file contains hidden or 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
#!/usr/bin/env ruby | |
# | |
# This is a simple script that allows you to post to Posterous from files on | |
# your computer. Currently accepted formats are HTML and markdown. | |
# | |
# USAGE | |
# | |
# posterous-post my-post.mkd | |
# | |
# CONFIGURATION |
This file contains hidden or 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
# s.query.keywords = params[:q] unless params[:q].blank? | |
# s.query.order_by(params[:order_by], params[:direction] || 'asc') if params[:order_by] && params[:order_by] != 'distance' | |
# s.query.order_by('content_score', 'desc') unless params[:order_by] && params[:order_by] == 'distance' | |
s.build do |query| | |
query.keywords(params[:q]) unless params[:q].blank? | |
query.order_by(params[:order_by], params[:direction] || 'asc') if params[:order_by] && params[:order_by] != 'distance' | |
query.order_by('content_score', 'desc') unless params[:order_by] && params[:order_by] == 'distance' | |
end |
This file contains hidden or 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
# | |
# Silently fail instead of raising an exception when an error occurs while writing to Solr. | |
# NOTE: does not fail for reads; you should catch those exceptions, for example in a rescue_from statement. | |
# | |
# To configure, add this to an initializer: | |
# Sunspot.session = SilentFailSessionProxy.new | |
# | |
# This is for Sunspot 0.18 and would need to be changed a little bit for Sunspot 1.0. | |
# | |
class SilentFailSessionProxy |
This file contains hidden or 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
require File.join(File.dirname(__FILE__), 'spec_helper') | |
describe 'function query' do | |
it "should send query to solr with function query from a block" do | |
session.search Post do | |
keywords('pizza') do | |
boost(function { :ratings_average }) | |
end | |
end | |
connection.should have_last_search_including(:bq => 'average_rating_f^2.0') |
This file contains hidden or 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 LessCompiler | |
class Middleware | |
def initialize(app) | |
@app = Rack::Static.new(app, :urls => %w(/stylesheets), :root => 'tmp') | |
@compiler = Compiler.instance | |
end | |
def call(env) | |
maybe_compile_less(env) | |
@app.call(env) |
This file contains hidden or 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 InstanceLifecycleHooks | |
def self.included(base) | |
base.module_eval do | |
alias_method_chain :run_callbacks, :instance_lifecycle_hooks | |
end | |
end | |
ActiveRecord::Callbacks::CALLBACKS.each do |callback| | |
module_eval <<-RUBY, __FILE__, __LINE__+1 | |
def #{callback}_instance(method = nil, &block) |
This file contains hidden or 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 | |
module Observing | |
CALLBACKS = [ | |
:before_create, :before_destroy, :before_save, :before_update, | |
:before_validation, :after_create, :after_destroy, :after_save, | |
:after_update, :after_validation | |
] | |
def self.included(base) | |
base.module_eval { include ActiveModel::Observing } |
This file contains hidden or 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 | |
include Criterion::MapReduce | |
end | |
end |