Skip to content

Instantly share code, notes, and snippets.

View Breefield's full-sized avatar

Bree Hoffman Breefield

View GitHub Profile
class Location < ActiveRecord::Base
belongs_to :located, :polymorphic => true
geocoded_by :address
before_validation :geocode, :if => :changed?
end
Event.create(name: 'Coffee Party', location_attributes: {address: '3126 16th St. San Francisco, CA 94103'})
# (0.2ms) begin transaction
# SQL (2.8ms) INSERT INTO "events" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Fri, 22 Nov 2013 19:09:51 UTC +00:00], ["name", "Coffee Party"], ["updated_at", Fri, 22 Nov 2013 19:09:51 UTC +00:00]]
# SQL (0.3ms) INSERT INTO "locations" ("address", "created_at", "latitude", "located_id", "located_type", "longitude", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["address", "3126 16th St. San Francisco, CA 94103"], ["created_at", Fri, 22 Nov 2013 19:09:51 UTC +00:00], ["latitude", 37.7648968], ["located_id", 2], ["located_type", "Event"], ["longitude", -122.4225344], ["updated_at", Fri, 22 Nov 2013 19:09:51 UTC +00:00]]
# (2.2ms) commit transaction
# <Event id: 2, name: "Coffee Party", created_at: "2013-11-22 19:09:51", updated_at: "2013-11-22 19:09:51">
class Event < ActiveRecord::Base
has_one :location, as: :located, dependent: :destroy
accepts_nested_attributes_for :location
end
class CreateLocations < ActiveRecord::Migration
def change
create_table :locations do |t|
t.string :address
t.float :latitude
t.float :longitude
t.integer :located_id
t.string :located_type
t.timestamps
Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n")
end
twitter_regex = /(@[A-Za-z0-9_]{1,15})/i
formatted_phone_regex = /((?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *x(\d+))?\b)/i
email_regex = /(\b[^\s]+?\s*(@|at)\s*[^\s]+?\.[^\s]+?\b)/i # Very general so as to catch BS
class Message < ActiveRecord::Base
validates :body, cold_shoulder: true
end
@Breefield
Breefield / init.rb
Created November 14, 2013 01:34
s.files = Dir['lib/*.rb'] + Dir['locales/*']
Gem::Specification.new do |s|
s.files = 'lib/cold_shoulder.rb'
end
class UserSession < Authlogic::Session::Base
validate :check_presence
# Without the following method, calling persisted? on UserSession fails,
# which is needed for polymorphic_url when called with a UserSession.
def destroyed?
false
end
# Logs are saying:
#
# Cache read: geocoded/New York, NY
# Cache fetch_hit: geocoded/New York, NY
# Google Geocoding API error: over query limit.
#
# Seems to hit a match, then query google anway
def self.geocode(address)
location = Rails.cache.fetch("geocoded/#{address}") do