Skip to content

Instantly share code, notes, and snippets.

Adam Cooper adamcooper

Block or report user

Report or block adamcooper

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View request_spec.rb
ABOUT TO CALL POST FROM TESTS WITH THIS DATA
data: {}
source=rack-timeout id=40677bbc82995734013bd5f9f961a8d8 timeout=20000ms state=ready
Started POST "/api/purchases" for 127.0.0.1 at 2015-06-25 11:49:41 -0700
source=rack-timeout id=40677bbc82995734013bd5f9f961a8d8 timeout=20000ms service=5ms state=active
Processing by Api::PurchasesController#create as HTML
Parameters: {"purchase"=>{}}
@adamcooper
adamcooper / example_controller.rb
Last active Aug 29, 2015
Sample Service Base Class and Result Class
View example_controller.rb
class UserController < ActionController::Base
def new
@user = User.new
end
def create
result = SignupUser.call(params[:user])
if result.successful?
redirect_to root_url, notice: "A welcome e-mail is on it's way!"
View tennis_game.rb
class TennisGame
def player_one_scores
end
def player_two_scores
end
def score
"Love All"
@adamcooper
adamcooper / _grade_students.rb
Last active Aug 29, 2015
Students and Courses
View _grade_students.rb
class GradeStudents
def self.call(teacher_name, course_name, grade_list)
course = CourseRegistry.lookup(course_name)
teacher = TeacherRegistry.lookup(teacher_name)
course.for_each_student do |student|
if grade = grade_list[student.name]
student.complete_course(course, grade)
end
end
end
View course.rb
class Course
attr_accessor :title, :location
def initialize(params = {}); end
def add_teacher(teacher); end
def add_student(student); end
def for_each_student; end
private
attr_accessor :teachers, :students
View input.rb
gem 'rails', '3.2.13' # change as required
require 'active_record'
# Print out what version we're running
puts "Active Record #{ActiveRecord::VERSION::STRING}"
# Connect to an in-memory sqlite3 database (more on this in a moment)
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
@adamcooper
adamcooper / gist:5315262
Created Apr 4, 2013
Don't use "and" in ruby.
View gist:5315262
>> [1,2,3, 4].each {|x| puts x; break if x == 1}
1
=> nil
>> [1,2,3, 4].each {|x| puts x; puts 'broken' and break if x == 1}
1
broken
2
3
4
View test.rb
class Redis
attr_accessor :server
end
class RedisNamespace
attr_accessor :redis
def initialize(namespace, options = {})
self.redis = options[:redis] || :internal
end
View 0001-fixing-the-bundle-install-issue.patch
From 003273df39c7e65b27197930064c77f2141969d5 Mon Sep 17 00:00:00 2001
From: Adam Cooper <adam.cooper@gmail.com>
Date: Sun, 16 Oct 2011 18:21:15 -0700
Subject: [PATCH] fixing the bundle install issue
---
hubruby.gemspec | 2 +-
lib/hubruby.rb | 2 +-
lib/version.rb | 3 +++
3 files changed, 5 insertions(+), 2 deletions(-)
View 20110811124516_migrate_product_type_category_groups.rb
ProductType.all.each do |product_type|
# Check if product type has banyan category group
product_type_group = product_type.category_group
# Create banyan category group for product type if not exist
if product_type_group.nil?
product_type_group = Banyan::CategoryGroup.new(:name => product_type.name + " Category Group")
product_type_group.group_categorizable = product_type
product_type_group.save!
end
You can’t perform that action at this time.