Skip to content

Instantly share code, notes, and snippets.

View stevenharman's full-sized avatar

Steven Harman stevenharman

View GitHub Profile
@stevenharman
stevenharman / routes.rb
Created May 18, 2011 04:00
rspec-rails routing specs -- WTF?
# config/routes.rb
Blah::Application.routes.draw do
devise_for :users
get "/:username" => "users#show", :as => :user_profile
root :to => 'pages#home'
@stevenharman
stevenharman / git-curse.sh
Created June 7, 2011 20:45
because we all like to curse. especially me!
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --date=relative --all | grep -Ei 'shit|fuck|damn|\bass\b|asshole'
@stevenharman
stevenharman / form-inputs-and-labels.html
Created June 16, 2011 15:44
a better way to label a form input.
<!-- see: http://www.w3.org/TR/html5/forms.html#the-label-element -->
<!-- stop doing this -->
<label for="post_title">Title:</label> <input type="text" id="post_title" name="post[title]">
<!-- start doing this! -->
<label>Title: <input type="text" name="post[title]"></label>
@stevenharman
stevenharman / routes.rb
Created June 19, 2011 19:47
Rails: multiple route parameters w/a single model - can it be done?
ZomgMyThings::Application.routes.draw do
devise_for :users
scope ":username" do
resources :things #=> /some-user-name/things/42
end
get "/:username" => "users#show", :as => :user_profile
@stevenharman
stevenharman / things_controller_spec.rb
Created June 23, 2011 04:17
RSpec shared_context blocks
shared_context 'authenticated user' do
before(:each) do
@user = Factory(:user)
sign_in @user
end
end
require 'spec_helper'
describe ThingsController do
@stevenharman
stevenharman / models.rb
Created July 7, 2011 01:50
Break up multiple associations to the same Model by letting the domain lead the way...
# ====== A naïve model =======
class User < ActiveRecord::Base
has_many :widgets
has_many :purchased_widgets, :class_name => 'Widget'
end
class Widget < ActiveRecord::Base
belongs_to :seller, :class_name => 'User', :foreign_key => 'seller_id'
belongs_to :buyer, :class_name => 'User', :foreign_key => 'buyer_id'
@stevenharman
stevenharman / thing_controller.rb
Created August 23, 2011 18:14
Trying not to let ActiveRecord leak out...
# This is pretty typical, and often 'advised' in Rails apps today
class ThingController < ApplicationController
def create
@thing = current_user.things.build(params[:thing]) # YUCK!
if @thing.save
redirect_to(user_thing_path(@thing.seller, @thing))
else
render :new
end
@stevenharman
stevenharman / discussion.rb
Created August 30, 2011 02:15
Thing.first.discussions.started_by(User.first)
class Discussion < ActiveRecord::Base
belongs_to :thing
scope :started_by lambda { |user| where(:initiated_by_id => user) }
end
@stevenharman
stevenharman / constant_scopes.rb
Created September 12, 2011 15:29
Constant scoping weirdness in Ruby...
module Foo
end
module Foo::Bar
end
class Baz
end
class Quux
@stevenharman
stevenharman / new.html.erb
Created November 1, 2011 23:40
Proof of Concept: create multiple, identical, AR models from a single form.
<div>
<h1>Add a thing to your box</h1>
<%= form_for @thing, html: { class: "form-stacked" } do |f| %>
<label>Name: <%= f.collection_select :widget_id, @widgets, :id, :name,
{include_blank: true}, autofocus: true %></label>
<label>Blah: <%= f.text_field :blah %></label>
<label>Other stuff: <%= f.text_field :other %></label>
<label>Count: <%= text_field_tag :count, @thing_count %></label>