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 / application.html
Last active August 29, 2015 14:28
capybara ensure page changed
<!DOCTYPE html>
<html lang="en">
<head>
<# only include in the test env to avoid uuid generation slowing down every rendered page in production %>
<% if Rails.env.test? %>
<meta name='test-page' content="<%= SecureRandom.uuid %>">
<% end %>
<%# etc, all your usual content %>
</head
</html>
Given I am on the page links page
When I follow "New Link"
Then I should be on the new page link page
When I fill in "Text" with "Fresh Fruit"
And I fill in "Path" with "fruit"
And I press "Create"
Then 1 page link should exist
When I go to the homepage
@nruth
nruth / alias_step.rb
Created November 20, 2009 18:27
Cucumber Step general alias for Then/When/Given
#defines Step, which is the generic When/Then/Given, since they actually have no meaning to cucumber, they're just fluff for human eyes
#written by Ian White, observed by yours truly
module AliasStep
def Step(arg)
When arg
end
end
World(AliasStep)
@nruth
nruth / dynamic_show_hide_help_boxes.feature
Created May 24, 2010 02:10
capybara hidden element has_content example
@ignore-hidden-elements
Feature: Div help content hiding and revealing
In order to keep help text available but neatly tucked away when not needed
As a designer
I want javascript to detect div.help and hide it and its contents, replacing with a link to reveal it again
Background:
#this is a rails app with a Page model and a path_to entry for visiting them in features like this (using Pickle for cucumber / machinist integration)
Given a page exists with content: "<div class='help'>foobar</div>"
Given /^#{capture_model} has a question: "([^\"]*)"$/ do |model_name, question|
model = model! model_name
store_model "question", '', model.questions.make(:question => question)
end
Given /^#{capture_model} has a question$/ do |model_name|
model = model! model_name
factory, name_or_index = *parse_model(%Q(question)) #pickle internals
store_model factory, name_or_index, model.questions.make
end
@nruth
nruth / Rakefile
Created July 18, 2010 17:11
Heroku bundle downloader -- just add cron
require 'rake'
APP_NAME = 'heroku_app_name'
BACKUP_PATH = '.'
namespace "heroku" do
desc "create a new Heroku bundle and download it, name with timestamp - only use with single bundle addon"
task "backup" do
require 'fileutils'
@nruth
nruth / gist:501517
Created July 30, 2010 23:54
rspec 1 before / let for context
describe "3 users exist"
let(:users) {(1..3).map {User.make}}
specify "user count should be 3" do
pending "this will fail because users hasn't been called" do
User.count.should == 3
end
end
specify "3 users should exist" do
@nruth
nruth / controller_spec.rb
Created August 21, 2010 16:32
Capybara rack-test response status code checking
describe "when some redirect mappings are present in the Medify::PAGE_REDIRECTS hash" do
before(:each) do
Medify::PAGE_REDIRECTS['/abstract-reasoning-subtest-advice-by-medify'] = '/advice-pages/abstract-reasoning-subtest-advice-by-medify'
Medify::PAGE_REDIRECTS['/decision-analysis-subtest-advice-by-medify'] = '/advice-pages/decision-analysis-subtest-advice-by-medify'
end
describe "GET :show, :path => abstract-reasoning-subtest-advice-by-medify" do
subject {get :show, :path => 'abstract-reasoning-subtest-advice-by-medify'}
it {should be_redirect}
it {should redirect_to('/advice-pages/abstract-reasoning-subtest-advice-by-medify')}
@nruth
nruth / email-screener-google-safebrowsing.yml
Last active September 14, 2015 19:56
ruby/rails user signup email screening with google safebrowsing api -- catch phishing domain typos in user email addresses
---
http_interactions:
- request:
method: get
uri: https://sb-ssl.google.com/safebrowsing/api/lookup?appver=1&client=your-app-name-here&key=abc123fakekey&pver=3.1&url=gmai.com
body:
encoding: US-ASCII
string: ''
headers:
Accept: