Skip to content

Instantly share code, notes, and snippets.

View mikecmpbll's full-sized avatar
🎯
Focusing

Mike Campbell mikecmpbll

🎯
Focusing
  • Skipton, North Yorkshire
View GitHub Profile
races = Race.where(runs_at: (Date.today - 12.months)..Date.today)
variants_queue = # big queue!
workers = []
results = []
best_strike_rate = [0, nil]
best_avg = [100, nil]
mutex = Mutex.new
class GenericLicence < ActiveRecord::Base
self.abstract_class = true
# blah blah blah
end
class FooLicence < GenericLicence
end
class BarLicence < GenericLicence
@mikecmpbll
mikecmpbll / is_active.rb
Created February 16, 2015 14:28
active?
# # #
# If you pass:
# - String, then it highlight for that controller name
# - Array, then it will highlight for those actions
# - Hash, then it will highlight for only those controller and action combinations
#
def is_active?(controller_action_hash_or_array_or_string)
controller = params[:controller]
action = params[:action]
validate :ensure_past_expiration_date
def ensure_past_expiration_date
unless expiration_date > Date.today || expiration_date_was < Date.today
errors.add(:expiration_date, "can't be in the past")
end
end
@mikecmpbll
mikecmpbll / new.html.erb
Created February 3, 2015 11:03
Multiform setup controller
<%= render "#{@step}_form" %>
@mikecmpbll
mikecmpbll / argh.rb
Created December 12, 2014 10:11
Threaded query to multiple DBs
def run(scope, dbs)
results = {}
threads = []
dbs.each do |db|
threads = Thread.new do
while_connected_to(db) do |conn|
query_model = scope.model
puts "USING CONN: #{conn.object_id} as opposed to #{Student.connection.object_id}"
result_set = conn.select_all(scope.to_sql, "My Super Special Fab Load")
@mikecmpbll
mikecmpbll / console.rb
Created November 13, 2014 13:47
Explanation of trim level differences between Erubis and the ERB when Rails used both.
irb(main):045:0> ERB.new("<% if true -%>\nhi\n<% else %>\nbye\n<% end %>", nil, "-").result # old rails with '-'
=> "hi\n"
irb(main):047:0> ERB.new("<% if true %>\nhi\n<% else %>\nbye\n<% end %>", nil, "-").result # old rails without '-'
=> "\nhi\n"
irb(main):048:0> Erubis::Eruby.new("<% if true %>\nhi\n<% else %>\nbye\n<% end %>").result # new rails with Erubis default trim
=> "hi\n"
irb(main):049:0> Erubis::Eruby.new("<% if true %>\nhi\n<% else %>\nbye\n<% end %>", trim: false).result # Erubis with trimming turned off
=> "\nhi\n"
module ScopeValidators
extend ActiveSupport::Concern
module ClassMethods
def validates(*args)
options = args.extract_options!
if options.key?(:uniqueness)
case options[:uniqueness]
when Hash
options[:uniqueness].merge!(scope: :entity_id)
@mikecmpbll
mikecmpbll / delayed-job.conf
Created November 7, 2014 14:08
Delayed Job upstart script
description "DELAYED JOB"
start on (local-filesystems
and net-device-up
and runlevel [2345])
stop on runlevel [!2345]
chdir /home/apps/app_name/current
exec /usr/bin/env RAILS_ENV=production HOME=/home/apps /home/apps/.rvm/wrappers/ruby-2.0.0-p353/rake jobs:work >> /tmp/upstart.log 2>&1
module Test2
class << self
@var = "foo"
def test_method
@var
end
end
end