Skip to content

Instantly share code, notes, and snippets.

# Conditional GET Support for a collection of ActiveRecord objects
#
# Useful in controllers when you want to support conditional get in
# index actions.
#
# example:
# class PostsController
# def index
# if stale?(conditional_get_collection)
# respond_with collection
require 'fundraiser_client'
# Invites registrants who want to fundraise via Everyday Hero.
#
# example:
# CreateIndividualPageInvitationService.from_order order
#
# Only invites registrations who have selected to fundraise ignoring the
# primary registrant who had to authenticate through Everyday Hero when
# registering.
#!/bin/sh
# Start and stop maintenance mode for supporter application.
#
# The maintenance page is stored with the application in version control and
# when enabled will be copied to a location known by Apache so it can be
# served instead of the application.
FROM=/var/www/apps/supporter/current/public/maintenance.html
TO=/var/www/apps/supporter/shared/public/maintenance.html
@coop
coop / 1_README.md
Last active December 24, 2015 09:09

Accessing routes outside controllers

Accessing the router outside of the controller context can be a real pain especially if you need access to _url helpers. It is even more difficult if you want to pass the router as an argument to a method or class. We had come up with a few different solutions but so far this has been the most reliable.

Accessing the routes in a class

ENV['SOME_URL'] = 'http://my.url.com'

class MyClassThatHasAccessToRoutes
@coop
coop / gist:6882489
Last active December 24, 2015 23:48
# config/routes.rb
get '/:country/welcome' => 'welcome#show'
# app/controllers/welcome_controller.rb
class WelcomeController < ApplicationController
around_filter :set_preferred_locale
@coop
coop / 1_routes.sh
Last active December 26, 2015 00:39
app(master*)$ bin/rake routes
Prefix Verb URI Pattern Controller#Action
heroix_sign_up GET /:country/sign_up(.:format) heroix/users#new {:country=>"au"}
# This is a pretty standard controller action in our codebase. Maybe it might
# get rolled up into a service object but it's unlikely. This code is smelly
# because it's making decisions based on the return value of an object it
# doesn't own.
class DonationsController < ApplicationController
def create
mailer.notify_supporter donation
if charity.active?
mailer.receipt donation
mailer.notify_charity donation
@coop
coop / donations.js
Last active December 27, 2015 03:49
//= require jquery.payment
jQuery(function($) {
var $form = $('form#donation-form'),
$submit = $('input[type=submit]', $form);
// Fields can be invalid in two ways:
//
// * do not contain a value
// * fail $.payment checks
class Donation
def move_donation new_page
raise CannotMoveError if charity != new_page.charity
send_event :donation_moved, from_page_id: page.id, to_page_id: new_page.id
end
end
#!/usr/bin/python
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of