Skip to content

Instantly share code, notes, and snippets.

View jparker's full-sized avatar

John Parker jparker

View GitHub Profile
@jparker
jparker / ordinalize.coffee
Last active February 26, 2018 08:03
coffeescript implementation of extending Number with an ordinalize() method
Number::ordinal = ->
return 'th' if 11 <= this % 100 <= 13
switch this % 10
when 1 then 'st'
when 2 then 'nd'
when 3 then 'rd'
else 'th'
Number::ordinalize = ->
this + this.ordinal()
# spec/support/autocompletion.rb
module Autocompletion
def select_autocompletion(value, from:, with:, multiple: false)
input_field = find_field(from)
input_value = multiple ? "#{input_field.value}#{with}" : with
# fill_in input[:id], with: input_value
# input_field.trigger(:keydown)
page.execute_script "$('##{input_field[:id]}').val('#{input_value}').trigger('keydown')"
@jparker
jparker / gist:9379914
Created March 6, 2014 00:41
Load remote data as DataTable for Google Chart
jQuery.ajax
async: false
dataType: 'json'
url: '/path/to/data.json'
success: (data) ->
google.load 'visualization', '1', {packages: ['corechart']}
google.setOnLoadCallback ->
table = new google.visualization.DataTable()
table.addColumn 'datetime', 'Timestamp'
table.addColumn 'number', 'Memory Total'
@jparker
jparker / RunFile.vim
Created February 20, 2014 00:00
Vim mappings for executing Ruby or Python
function! RunFile()
if match(@%, '.rb$') != -1
let argv = input('!ruby % ')
exec '!ruby % ' . argv
elseif match(@%, '.py$') != -1
let argv = input('!python % ')
exec '!python % ' . argv
else
echo '<< ERROR >> RunFile() only supports ruby and python'
endif
@jparker
jparker / example.rb
Last active December 26, 2015 03:19
Representing query parameters as a first-class object that is interchangeable with an ActiveModel object
QuoteReport.new(:quarter, ReportFilter.new)
# => Quote.all
QuoteReport.new(:quarter, Composer.find(42))
# => Composer.find(42).quotes
QuoteReport.new(:quarter, ReportFilter.new(composer_id: 42))
# => Quote.joins(cue: :composer).where(cue: {composer_id: 42})
QuoteReport.new(:quarter, ReportFilter.new(composer_id: [42, 101]))
# => Quote.joins(cue: :composer).where(cue: {composer_id: [42, 101]})
[1] pry(main)> Base64.encode64 'a' * 64
=> "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh\nYWFhYWFhYWFhYWFhYWFhYWFhYQ==\n"
[2] pry(main)> Base64.strict_encode64 'a' * 64
=> "YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ=="
@jparker
jparker / rspec.vim
Created February 12, 2013 19:35
Mappings and a function for running RSpec within Vim.
noremap <Leader>rs :call RunSpec('spec', '-fp')<CR>
noremap <Leader>rd :call RunSpec(expand('%:h'), '-fd')<CR>
noremap <Leader>rf :call RunSpec(expand('%'), '-fd')<CR>
noremap <Leader>rl :call RunSpec(expand('%'), '-fd -l ' . line('.'))<CR>
function! RunSpec(spec_path, spec_opts)
let speccish = match(@%, '_spec.rb$') != -1
if speccish
exec '!bundle exec rspec ' . a:spec_opts . ' ' . a:spec_path
else
@jparker
jparker / user.rb
Created November 12, 2012 23:05
Validate :password in addition to :password_digest
[1] pry(main)> user = User.new(password: nil, password_confirmation: nil)
=> #<User id: nil, username: nil, email: nil, name: nil, password_digest: nil, created_at: nil, updated_at: nil>
[2] pry(main)> user.valid?
=> false
[3] pry(main)> user.invalid?(:password)
=> true
[4] pry(main)> user.errors[:password]
=> []
[5] pry(main)> user.errors[:password_confirmation]
=> []
@jparker
jparker / authlogic.rb
Last active October 12, 2015 17:37
Snippet to be placed in test_helper.rb or spec_helper.rb to trick has_secure_password into using lower cost bcrypt encryption than the default.
# Place in test_helper.rb or spec_helper.rb
AuthLogic::CryptoProviders::BCrypt.cost = 1
@jparker
jparker / assets.rake
Created March 23, 2012 23:03
Rake task for compiling assets and uploading them to S3
# RAILS_ROOT/lib/tasks/assets.rake
namespace :assets do
desc 'Precompile assets and upload to S3'
task :upload, [:noop] => ['assets:clean', 'assets:precompile'] do |_, args|
args.with_defaults(noop: false)
Fog.credentials_path = "#{Rails.root}/config/fog_credentials.yml"
Dir.chdir("#{Rails.root}/public") do
assets = FileList['assets',"assets/**/*"].inject({}) do |hsh, path|