Skip to content

Instantly share code, notes, and snippets.

View markandrewj's full-sized avatar

Mark Jackson markandrewj

  • Canada
View GitHub Profile
@markandrewj
markandrewj / factory_test
Last active November 18, 2015 00:30
Testing your factories
# Rakefile
if defined?(RSpec)
desc 'Run factory specs.'
RSpec::Core::RakeTask.new(:factory_specs) do |t|
t.pattern = './spec/factories_spec.rb'
end
end
task spec: :factory_specs
@markandrewj
markandrewj / gist:d9ece855bad865683774
Last active March 2, 2016 23:39
RVM/Bundler vendor dir and aliases
bundle install --path vendor
bundle package
echo 'vendor/ruby' >> .gitignore
alias b="bundle"
alias bi="b install --path vendor"
alias bil="bi --local"
alias bu="b update"
alias be="b exec"
alias binit="bi && b package && echo 'vendor/ruby' >> .gitignore"
@markandrewj
markandrewj / gist:9270d311c3a14b3c3565
Last active August 17, 2022 04:30
How To Stub authentication in Devise controller specs

https://github.com/plataformatec/devise/wiki/How-To:-Stub-authentication-in-controller-specs

These are instructions for allowing you to use resource test doubles instead of actual ActiveRecord objects for testing controllers where Devise interactions are important. This means there's even less reliance on the database for controller specs, and that means faster tests!

This approach certainly needs some evolution, but it's a start.

To stub out your user for an action that expects an authenticated user, you'll want some code like this (once you've got Devise::TestHelpers loaded):

 user = double('user')
@markandrewj
markandrewj / gist:c0b6db1a206c371deb2d
Created November 10, 2015 08:45
Basecamp-style subdomains in Rails: DHH
# 1) Point *.example.com in your DNS setup to your server.
#
# 2) Setup an Apache vhost to catch the star pointer:
#
# <VirtualHost *:80>
# ServerName example.com
# ServerAlias *.example.com
# </VirtualHost>
#
# 3) Set the current account from the subdomain
@markandrewj
markandrewj / pxtoem.scss
Last active September 9, 2015 00:48
Pixel to EM
// Enter a base pixel size in px (without the px)
$basepx: 16;
// Convert PX to EM global function
@function pxtoem($px, $basepx){
@return ($px/$basepx)+0em;
}
//Then on a class or element you can use:
h1 { font-size: pxtoem(36, $basepx); }
<?xml version="1.0"?>
<regex>
<name>url</name>
<pattern><![CDATA[^((((https?|ftps?|gopher|telnet|nntp)://)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';/?:@&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$]]></pattern>
<description>A valid URL per the URL spec.</description>
</regex>
<regex>
<name>IP</name>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<canvas id="buddhabrot" width="1000" height="1000"></canvas>
<div id="images"></div>
<script type="text/javascript">
var canvas = document.getElementById('buddhabrot'),

#Simple Authentication with Bcrypt

This tutorial is for adding authentication to a vanilla Ruby on Rails app using Bcrypt and has_secure_password.

The steps below are based on Ryan Bates's approach from Railscast #250 Authentication from Scratch (revised).

You can see the final source code here: repo. I began with a stock rails app using rails new gif_vault

##Steps

class DataBinder extends jQuery
pubSub: null
constructor: (objectid) ->
@pubSub = $ {}
attribute = "data-bind-#{objectid}"
message = "#{objectid}:change"
$(document).on "change", "[#{attribute}]", (event) =>
$elem = $(event.target)