Skip to content

Instantly share code, notes, and snippets.

@hardbap
hardbap / regex_a.rb
Last active June 26, 2018 17:26 — forked from JoshCheek/regex.rb
Ruby 1.9 regex, named capture groups to local variables
/(?<area_code>\d{3}) (?<local_number>\d{3}-\d{4})/ =~ "123 456-7890"
local_variables # => [:area_code, :local_number]
area_code # => "123"
local_number # => "456-7890"
@hardbap
hardbap / chat.rb
Created February 7, 2013 19:07 — forked from rkh/chat.rb
# coding: utf-8
require 'sinatra'
set server: 'thin', connections: []
get '/' do
halt erb(:login) unless params[:user]
erb :chat, locals: { user: params[:user].gsub(/\W/, '') }
end
get '/stream', provides: 'text/event-stream' do
@hardbap
hardbap / _media-queries.scss
Created March 13, 2012 17:32 — forked from anthonyshort/_media-queries.scss
Media Queries in Sass
// Dimensions of devices
// iPad Retina - 1536x2048
// iPad - 768x1024
// iPhone - 320x480
// iPhone Retina - 640x960
@mixin mobile-only {
@media only screen and (max-width : 480px) {
@content;
}
def reorder
redirect_to confirm_order_path and return if params[:reorder] == current_order.id.to_s
reorder = current_user.orders.find(params[:reorder])
redirect_to favorites_order_path, :notice => 'The order is no longer available.' and return if reorder.nil?
redirect_to favorites_order_path, :notice => 'There are items in your cart. Please cancel your current order before reordering.' and return if (current_order.items.any?)
search_result = reorder.next_available_delivery
@hardbap
hardbap / TechDecisions.md
Created November 27, 2011 14:37
Choices to make in a new Rails project

Team Support

Source Code Control

git (private server)
Alternative: github

Time Tracking

Cashboard

new_order = reorder.xerox
new_order.save
current_order.destroy
current_order = new_order
current_order.status = 'open'
current_order.favorite = false
current_order.created_at = DateTime.now
current_order.updated_at = DateTime.now
current_order.mealsoft_response = nil
current_order.error_message = nil
class QueryTracer < ActiveSupport::LogSubscriber
ACCEPT = %r{^(app|config|lib)}.freeze
FRAMES = 5
THRESHOLD = 300 # In ms
def sql(event)
return unless event.duration > THRESHOLD
callers = Rails.
backtrace_cleaner.
>> case "String".class
>> when String
>> puts "I'm a string"
>> else
?> puts "I can't tell what I am"
>> end
I can't tell what I am
class Post < ActiveRecord::Base
has_many :authors
has_many :post_comments
attr_accessor :post_authors_cache, :post_comments_cache
def post_authors
@post_authors_cache ||= begin
process_post
class Post < ActiveRecord::Base
has_many :authors
has_many :post_comments
attr_accessor :post_authors_cache, :post_comments_cache
def post_authors
process_post if post_authors_cache.nil?
post_authors_cache