Skip to content

Instantly share code, notes, and snippets.

View coryschires's full-sized avatar
💭
Code King

Cory Schires coryschires

💭
Code King
View GitHub Profile
@coryschires
coryschires / factory_girl_example.rb
Created June 20, 2011 18:33
factory_girl_example.rb
Factory.define :user do |f|
f.sequence(:email) { |n| "foo#{n}@example.com" }
f.email_confirmation { |u| u.email }
f.password "password"
f.password_confirmation { |u| u.password }
end
Factory.define :super_admin, :parent => :user do |f|
f.after_create { |user| user.roles << Role.find_or_create_by_name('super_admin') }
end
@coryschires
coryschires / apply_role.rb
Created June 20, 2011 20:33
apply_role.rb
describe "AFTER CREATE apply_member_role" do
it "should apply the role of 'user' to the newly created user" do
user = Factory(:user)
user.role?('member').should be_true
end
end
# Any idea why this works:
named_scope :delivering_during, lambda { all(:include => {:campaign_billables => [:posting_period] }) }
# but this:
named_scope :delivering_during, all(:include => {:campaign_billables => [:posting_period] })
# raises "TypeError (Symbol as array index)"
@coryschires
coryschires / cuke_error
Created July 22, 2011 14:25
cuke_error
Background: Analyst logged in and viewing campaigns # features/campaigns.feature:6
Given campaign "ABC001" has ordered "50_000" and so far billed: # features/step_definitions/custom_steps.rb:25
| posting period | billable total | billed total |
| Jun 2009 | 10_000 | 10_000 |
| Jul 2009 | 15_000 | 15_000 |
| Aug 2009 | 20_000 | 20_000 |
And campaign "ABC002" has ordered "250_000" and so far billed: # features/step_definitions/custom_steps.rb:25
| posting period | billable total | billed total |
| Jun 2009 | 100_000 | 100_000 |
| Jul 2009 | 50_000 | 50_000 |
@coryschires
coryschires / factory_girl_question.rb
Created August 5, 2011 20:24
factory_girl_question.rb
# Having trouble getting nested factories working. Is this not supported? Seems like it would be.
#
# The associations are definitely setup correctly in the models:
# A campaign has_many :placements and a placement has_many :deliveries
#
@campaign = Factory(:campaign, :placements => [
Factory(:placement, :agency_rate => 3.00, :deliveries => [
Factory(:delivery, :quantity => 600),
Factory(:delivery, :quantity => 100)
]),
#!bash
#
# bash completion support for core Git.
#
# Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
#
# The contained completion routines provide support for completing:
#
@coryschires
coryschires / encoding_fixer.rb
Created September 20, 2011 19:08
Script to replace bad encoding with HTML entities.
class EncodingFixer
ENCODING_TO_ENTITY = {
"\222" => "&rsquo;",
"\223" => "&ldquo;",
"\224" => "&rdquo;",
"\205" => "&hellip;",
"\226" => "&ndash;",
"\227" => "&mdash;"
}
@coryschires
coryschires / question.rb
Created October 16, 2011 03:36
Grouping and ordering
Question.where(created_at: (15.days.ago)..(Time.now))
.limit(limit)
.joins(:impressions)
.group("impressions.impressionable_id")
@coryschires
coryschires / layout.rb
Created October 19, 2011 01:22
layout
# add to <tt>layout_helper.rb</tt>:
def rostra_page_title
case "#{controller_name}##{action_name}"
when "questions#show" then @question.title
when "questions#index" then
if params[:tag_search].present?
"Recent Questions"
else
"Recent Questions for tag #{params[:tag_search]}"
end
@coryschires
coryschires / answer.log
Created October 26, 2011 16:36
staging bug
cache: [POST /conversation/questions/4/answers] invalidate, pass
Started POST "/conversation/questions/4/answers" for 127.0.0.1 at 2011-10-26 11:39:29 -0500
Processing by Rostra::AnswersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"kMghcRVWRnEZHKdb5lE43qQ2uByACYLkcGLaR10Uvis=", "answer"=>{"user_id"=>"2164", "question_id"=>"4", "text"=>"<p>An answer</p>", "follow_by_email"=>"1"}, "commit"=>"Answer", "question_id"=>"4"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2164 LIMIT 1
Role Load (0.7ms) SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles"."id" = "roles_users"."role_id" WHERE "roles_users"."user_id" = 2164 AND "roles"."name" = 'super_admin' LIMIT 1
Role Load (0.8ms) SELECT "roles".* FROM "roles" INNER JOIN "roles_users" ON "roles"."id" = "roles_users"."role_id" WHERE "roles_users"."user_id" = 2164 AND "roles"."name" = 'member' LIMIT 1
Profile Load (0.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = 21