Instantly share code, notes, and snippets.

View Gemfile
source 'https://rubygems.org'
gem 'activerecord'
gem 'globalize'
gem 'sqlite3'
View modified_query.json
{
"from": 0,
"size": 10,
"sort": [
{
"_score": {
"order": "desc"
},
"boost": {
"order": "desc",
View base_query.json
{
"from": 0,
"size": 10,
"sort": [
{
"_score": {
"order": "desc"
},
"boost": {
"order": "desc",
View settings.rb
module MyApp
def self.settings
Settings.instance
end
class MyApp::Settings < BasicObject
# so we can reference classes w/o appending '::'
def self.const_missing(name)
::Object.const_get(name)
end
View better_polymorphic.md

Usage:

  polymorphic :commentable do
    belongs_to :post
    belongs_to :comment
    belongs_to :product
  end

Adds validations that only one is set, and an accessor ('commentable' from above) that gets and sets the polymorphic relation.

View indexes.rb
# leaves around a pointer to the root mapping after use
def indexes(name, options = {}, &block)
@_mapping_pointer ||= @mapping
@_mapping_pointer[name] = options
if block_given?
previous = @_mapping_pointer
@_mapping_pointer[name][:type] ||= 'object'
@_mapping_pointer = @_mapping_pointer[name][:properties] = {}
yield
View tire_ext.rb
module Tire
def self.inverted_aliases
aliases.inject({}) do |acc, (index, aliases)|
aliases.each do |als|
acc[als] ||= SortedSet.new
acc[als] << index
end
acc
end
end
View gist:1365900
Process: ruby [80361]
Path: /usr/local/rvm/rubies/ruby-1.9.2-p290/bin/ruby
Identifier: ruby
Version: ??? (???)
Code Type: X86-64 (Native)
Parent Process: ruby [80349]
Date/Time: 2011-11-14 16:42:57.491 -0800
OS Version: Mac OS X Server 10.7 (11A2061)
Report Version: 9
View gist:873252
# Relevant for http://local.seomoz.org:3000/campaigns/1/rankings
Keyword.all.each do |keyword|
[ Week.current.previous,
Week.current.previous.previous,
Week.current.previous.previous.previous ].each do |week|
GA::KeywordMetric.create({
"google_analytics_connection_id"=>2,
"google_analytics_engine_id"=> 96,