Skip to content

Instantly share code, notes, and snippets.

View nruth's full-sized avatar

Nicholas Rutherford nruth

View GitHub Profile
@nruth
nruth / admin_policy_spec.rb
Last active August 29, 2015 14:05
role scoped pundit spec for rspec 3
# -*- encoding : utf-8 -*-
# attempting to get something like http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec again
require 'rails_helper'
RSpec.describe AdminPolicy, type: :policy do
subject { AdminPolicy }
def self.permits(action)
permissions action do
it { expect(subject).to permit(current_admin, admin) }
@nruth
nruth / pay_stripe_helper.rb
Last active March 7, 2023 00:21
capybara selenium webdriver stripe.js checkout test helper
# -*- encoding : utf-8 -*-
module PayStripeHelpers
# must be used with driver: :selenium (or :sauce?)
def pay_stripe
sleep(0.7) # wait for the js to create the popup in response to pressing the button
within_frame 'stripe_checkout_app' do # must be selenium
# fill_in 'card_number', with: '4242424242424242' no longer works
4.times {page.driver.browser.find_element(:id, 'card_number').send_keys('4242')}
# fill_in 'cc-exp', with: '5/2018' no longer works
@nruth
nruth / screenshots.rb
Last active November 12, 2020 19:27
show_me_the_pages: screenshot various screen resolutions with capybara to review responsive design
RSpec.configure do |config|
config.filter_run_excluding :show_me_the_pages unless ENV["SHOW_ME_THE_PAGES"]
end
class Screenshots
include Capybara::DSL
attr_accessor :resolutions
attr_accessor :output_path
@nruth
nruth / routes.rb
Last active December 12, 2015 03:19
simple rails static page renderer
# last entry:
#static page routes
get '/home' => redirect('/')
root :to => 'static_pages#home'
get '*page', :to => 'static_pages#show', :constraints => {:page => %r{[\w\-\/]+}}
@nruth
nruth / Sitemap.rb
Last active December 12, 2015 03:19
basic rails sitemap generator
# -*- encoding : utf-8 -*-
class Sitemap
def self.static_view_templates
require 'fileutils'
template_paths = []
FileUtils.cd Rails.root.join 'app', 'views', 'static_pages' do
erb_html_files = File.join "**", "*.html.erb"
template_paths = Dir.glob(erb_html_files)
end
remove_html_erb!(template_paths)
@nruth
nruth / Makefile
Created April 9, 2012 14:24
gecode gcc makefile
all: send-more-money donald
# use git clean -f for make clean, but confirm action
define gitCLEAN
puts 'ARE YOU SURE (git clean -f) ? y/n'; \
puts %x(git clean -n); \
exec('git clean -f') if gets.chomp =~ /^y/i
endef
clean:
@nruth
nruth / gist:1377390
Created November 18, 2011 18:54
A poem about OTP
Old tired programmers() ->
obviously trusted prolog,
others tried perl.
Oft they pondered() ->
of teams problems,
of telcos pains.
Onward they plodded() ->
obscure to programmers,
@nruth
nruth / gist:1264245
Last active August 23, 2021 01:57
Shared Capybara (or model setup) helpers for RSpec and Cucumber
# Let's assume you're driving Capybara in both RSpec request specs & Cucumber,
# for example you're using Cucumber as a design/documentation tool, and RSpec
# for the more boring integration tests.
# You don't want to duplicate your click-this-click-that helpers to e.g.
# log_in(username, password).
# You may also have model state setup code which can be shared/reused.
# Where can it go? How can it be loaded? I've been using the following approach:
#
@nruth
nruth / rb_c_xor_file_contents_extension.rb
Created September 28, 2011 10:33
RubyInline C experimentation
# run with Piece.xor_files 'output_filepath', ['infilepath1','infilepath2']
require 'inline'
Class Piece
inline do |builder|
builder.include '<stdlib.h>'
builder.include '<stdio.h>'
builder.c_singleton <<-C_CODE
/*
rb_input_paths_array should be a ruby array of strings,
@nruth
nruth / gist:1193290
Created September 4, 2011 18:37
a few cucumber/rspec test steps for rails static page caching
Then /^a static page cache file for #{capture_model} should not exist$/ do |pickle_ref|
page = model!(pickle_ref)
page_cache_exist?(page).should be_false
end
Then /^a static page cache file for #{capture_model} should exist$/ do |pickle_ref|
page = model!(pickle_ref)
page_cache_exist?(page).should be_true
end