Skip to content

Instantly share code, notes, and snippets.

View eamonn-webster's full-sized avatar

Eamonn Webster eamonn-webster

View GitHub Profile
@eamonn-webster
eamonn-webster / lock.rb
Last active December 16, 2015 02:39
rails mysql database backed lock
class TimeoutError < StandardError
end
# used to prevent code running on the same object twice
def answer
begin
eq = EnrollmentQuestion.find_by_enrollment_id_and_question_id(@enrollment.id, @question.id)
Lock.new(eq,0).synchronize do
if @enrollment.is_valid_attempt?(@question)
...
@eamonn-webster
eamonn-webster / gist:5482299
Created April 29, 2013 15:22
Using rack-mini-profiler
# Gemfile
# gem 'rack-mini-profiler'
# config/environment.rb
if Rails.env.development?
# profile all middleware
Qstream::Application.middleware.each do |mw|
begin
::Rack::MiniProfiler.profile_method(mw.klass, :call) {|app| "#{mw.klass} call"}
@eamonn-webster
eamonn-webster / trove_migrations.rb
Last active December 18, 2015 04:19
find and execute trove migrations
# load all migrations
Dir[Rails.root.join('db/migrate/*.rb')].each { |f| require f }
#find the trove migrations classes
# and the corresponding file
# sort by file
# instantiate
# and invoke change
# rescuing from any exception.
@eamonn-webster
eamonn-webster / set_question_records_attempt.rb
Last active December 28, 2015 05:39
set record_questions attempt deal with those question_records that have a schedule_id instead of an enrollment_id
# could get a speed up if we initialized to 1 instead of 0
# but then we would have to compute all, no way to lazy compute.
# where ar we using the value?
def set_question_records_attempt_2(enterprise_id)
previous_enrollment_id = nil
previous_question_id = nil
attempt = 0
QuestionRecord.where(enterprise_id: enterprise_id).where('enrollment_id is not null').order(:enrollment_id, :question_id, :created_at).each do |qr|
@eamonn-webster
eamonn-webster / 0_reuse_code.js
Created October 30, 2015 08:14
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console