Skip to content

Instantly share code, notes, and snippets.

View darrenterhune's full-sized avatar

Darren Terhune darrenterhune

  • Canada
View GitHub Profile
# frozen_string_literal: true
require 'rails_helper'
feature 'Updating Admin Clinics feature' do
let(:admin) { create(:admin) }
let!(:clinic) { create(:clinic) }
before do
login_as(admin, scope: :user)
# Found from https://coderwall.com/p/j9btlg/reset-the-mysql-5-7-root-password-in-ubuntu-16-04-lts
# Stop MySQL
sudo service mysql stop
# Make MySQL service directory.
sudo mkdir /var/run/mysqld
# Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
# Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables --skip-networking &
@darrenterhune
darrenterhune / capybara_cheetsheet.rb
Last active July 10, 2017 18:21
Capybara cheetsheet
# SCOPING
within(*find_args) # within('div#delivery-address') do
within(a_node) # fill_in('Street', with: '12 Main Street')
# end
# MATCHERS
have_select(locator, options = {}) # expect(page).to have_select('id', options: ['Yes', 'No'])
# expect(page).to have_select('id', options: [1,2,3], selected: 2)
have_field(locator, options = {}) # expect(page).to have_field('id', placeholder: 'What city do you live in?')
# expect(page).to have_field('id', with: 'Some value', visible: false)
# routes.rb (checks if subdomains is present in the request object then sends it off to the correct location)
Rails.application.routes.draw do
constraints MicrositeConstraint do
root to: 'microsites#index', as: 'microsite_root'
end
end
class MicrositeConstraint
def self.matches?(request)
taken = %w(
@darrenterhune
darrenterhune / rails_status_codes.rb
Created January 24, 2017 01:40
List of rails status codes
100 = :continue
101 = :switching_protocols
102 = :processing
200 = :ok
201 = :created
202 = :accepted
203 = :non_authoritative_information
204 = :no_content
205 = :reset_content
206 = :partial_content
@darrenterhune
darrenterhune / digitalocean.md
Last active April 14, 2016 00:43
Digital Ocean [Virtual Hosts, MySQL] Setup

Setup Virtual Hosts

Execute the following commands after ssh into vps:

sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
cd /var/www/
sudo chown www-data:www-data * -R 
sudo usermod -a -G www-data username-logged-in-as
cd
@darrenterhune
darrenterhune / order_ranking_algorithm.rb
Created October 3, 2011 22:57
ranking algorithm in mysql
# I hate this... it's fugly:
order("CEIL((artworks.votes + POW(subscriptions.amount, 2)) / POW(((DATEDIFF(CURDATE(), artworks.created_at)) + 1), 1.5))")
# Would love to be able to do something similar to this (although I like typing *POW!*):
order(((artworks.votes + subscriptions.amount)**2) / (((Time.parse(DateTime.now.to_s) - Time.parse(created_at.to_s) + 1) / 86400).round + 1)^1.5)
# Code could have some boo boos init... this is just as an example fyi
require 'hmac-sha1'
require 'net/https'
require 'base64'
desc "Invalidate files from cloudfront distribution"
task :files, :filenames do |task, args|
CONFIG = YAML.load_file(File.join(RAILS_ROOT, 'config', 'amazon_s3.yml'))[RAILS_ENV]
s3_access = CONFIG['access_key_id']
@darrenterhune
darrenterhune / Bitfields
Created October 29, 2010 08:12
Example bitfield calculations in ruby with thinking-sphinx
# For bitfield calculations...
#------------------------------------------------------------------------
# flag | sale | lease | charter | hide | vertical
#------------------------------------------------------------------------
# booleans | 1 | 1 | 1 | 1 | 1
#------------------------------------------------------------------------
# position | 1 | 2 | 3 | 4 | 5
#------------------------------------------------------------------------
# value | 1 | 2 | 4 | 8 | 16
#
@darrenterhune
darrenterhune / move logic to model
Created February 24, 2010 09:23
csv import example in model
# controller
def proc_csv
import = Import.find(params[:id])
if import.load_csv
flash[:notice] = "woo"
redirect_to some_url
else
flash[:error] = "ohoh"
end
end