Skip to content

Instantly share code, notes, and snippets.

@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
# 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 / 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"}
@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_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
#!/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
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.
# 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

When building highly decoupled classes I often run into the problem of how to piece it all together. I have an understanding of how each piece is supposed to interact but I'm not sure how the functionality should co-exist. I have included some sample code below.

class Page < ActiveRecord::Base
has_attached_file :image
end
class DelayedAttachment
attr_reader :attachment
def initialize attachment
@attachment = attachment
end