Skip to content

Instantly share code, notes, and snippets.

@gavingmiller
gavingmiller / Advanced Memoization
Created September 10, 2014 15:05
Clearing memoization
def current_user
@current_user ||= User.find(session[:user_id])
end
# Call to clear memoized current user
def expire_current_user
@current_user = nil
end
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Update Your Payment Information — PetroFeed</title>
<link rel="stylesheet" href="css/main.css" type="text/css" />
</head>
<body style="margin: 0;">
<iframe id="form" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden;"></iframe>
</body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Update Your Payment Information — PetroFeed</title>
<link rel="stylesheet" href="css/main.css" type="text/css" />
</head>
<body style="margin: 0;">
<iframe id="form" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden;"></iframe>
</body>
def payment_required?(exception)
follow_limit_reached? && payable_exception?(exception)
end
# Did the exception occur due to a follow action on a Well?
# Otherwise, exception is 401, not 402.
def payable_exception?(exception)
exception.action == :follow && exception.subject.kind_of?(Well)
end
class BaseController < ApplicationController
# Access Errors (401, 402)
rescue_from CanCan::AccessDenied do |exception|
if current_ability.payment_required?(exception)
general_error(exception.message, 402)
else
general_error(exception.message, 401)
end
end
end
class WellsController < BaseController
def follow
@well = Well.find(params[:id])
if params['follow'] == 'true'
authorize! :follow, @well
current_user.follow(@well)
else
# Allow a user to unfollow a well even
# if they've reached their limit
@gavingmiller
gavingmiller / ability.rb
Last active December 23, 2015 11:19
A snapshot of PetroFeed's authorization code
class Ability
include CanCan::Ability
def initialize(user)
@user = user || User.new # guest user (not logged in)
authorize_payment_limits
# Other access code
end
@gavingmiller
gavingmiller / deploy.rake
Last active December 15, 2015 09:49 — forked from njvitto/deploy.rake
# Deploy and rollback script for Heroku on staging and/or production
# Modified from: https://gist.github.com/njvitto/362873
namespace :deploy do
PRODUCTION_APP = 'YOUR_PRODUCTION_APP_NAME_ON_HEROKU'
STAGING_APP = 'YOUR_STAGING_APP_NAME_ON_HEROKU'
desc 'Deploy to Staging on Heroku'
task :staging => ['deploy:set_staging_app', 'deploy:push', 'deploy:restart', 'deploy:tag']
desc 'Deploy to Production on Heroku'

Capybara

save_and_open_page

Matchers

have_button(locator)