Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam Cooper adamcooper

View GitHub Profile
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
@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 tennis_game.rb
class TennisGame
def player_one_scores
end
def player_two_scores
end
def score
"Love All"
@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 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"=>{}}
View gist:30580
class User < ActiveRecord::Base
has_many :cars
end
class Car < ActiveRecord::Base
belongs_to :user, :counter_cache => true
end
View Paperclip and VestalVersions
This is a brief description of how to get VestalVersions and Paperclip to play nicely.
The paperclip_initializer.rb extends paperclip for a new option :keep_old_files which means that paperclip won't remove the file when a new file is uploaded. In order for this to work, the model also needs to have to have specific settings:
#1. The version needs to be included in the file path
#2. A method to get the actual path to the file. (The file may not be at the current version)
You can view both these settings in the model_files.rb file below.
Note: If you dealing with public files then you will need to include the :id_and_version option in the :url and :path. Then in the view you need to reference the model_file.versioned_file_path.
@adamcooper
adamcooper / resque.rb
Created Nov 8, 2010
Process multi-jobs per fork of resque
View resque.rb
Resque.after_fork do |job|
jobs_performed = 0
kill_fork_at = Time.now.to_i + (ENV['MINUTES_PER_FORK'].to_i * 60)
worker = job.worker
first_job = job
worker.procline "Processing #{job.queue} since #{Time.now.to_i} until #{kill_fork_at}"
first_job.perform
# rely on parent error handling
View development.smtp.example.yml
---
:domain: "yourdomain.com"
:perform_deliveries: true
:address: 'relay.smtpserver.com'
:port:
:user_name:
:password:
:authentication: :plain
:enable_starttls_auto: true
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