Skip to content

Instantly share code, notes, and snippets.

@robwilliams
robwilliams / interactor_broadcast.rb
Created July 24, 2015 11:09
Generic broadcast for interactors
module InteractorBroadcast
extend ActiveSupport::Concern
included do
after :broadcast_on_success
around :broadcast_on_failure
end
private
def broadcast_on_success
@robwilliams
robwilliams / add_item.rb
Last active August 29, 2015 14:25
Single action per controller ideas
# app/actions/cart/add_item.rb
class Cart::AddItem
include Interactor
include InteractorBroadcast
include Wisper::Publisher
delegate :item, :checkout, :variant_id, :quantity, to: :context
def call
@darwinrc
darwinrc / warden_lotus.md
Last active August 29, 2015 14:24
Warden implementation in Lotus

apps/web/application.rb

controller.prepare do
        use YourApp::WardenImpl::WebManager
        include YourApp::WardenImpl::WardenHelper
        authentication_via :web_strategy
        auth_failure_to "/login"
        before :authenticate!    # run an authentication before callback
      end
require 'freegeoip'
require 'ecb_rates'
module ExternalServices
module_function
def freegeoip
@freegeoip ||= Freegeoip.new(ENV.fetch('FREEGEOIP_URL'))
end
@dbalatero
dbalatero / 00_README.md
Last active March 23, 2022 17:04
This is an example of how I combine interaction/service classes with Wisper event broadcasting inside Rails.

This is an example of how I combine interaction/service classes with Wisper event broadcasting in Rails.

In this example, I show a UsersController#create API, a corresponding service object, and all the test code/listeners to make it all happen.

The outcome is:

  • Concepts in your system ("Signing up a user", "Creating an order") have a single entry point in your codebase, vs. making raw ActiveRecord calls to object.save in dozens of places.
  • Since your concept has one entry point (the service class), you can easily grep for usage of it.
  • Stupid easy to attach listeners to the service class
  • All event listeners are very small and easily unit tested

I highly suspect that the RSpec core team all use black backgrounds in their terminals because sometimes the colors aren’t so nice on my white terminal

I certainly use a black background. I'm not sure about the other RSpec core folks. Regardless, if there are some color changes we can make that would make output look good on a larger variety of backgrounds, we'll certainly consider that (do you have some suggested changes?). In the meantime, the colors are configurable, so you can change the colors to fit your preferences on your machine. First, create a file at

@joonty
joonty / capistrano.rb
Created February 5, 2014 11:20
Capistrano local asset compilation
namespace :assets do
desc 'Run the precompile task locally and rsync with shared'
task :precompile, :roles => :web, :except => { :no_release => true } do
unless ENV['SKIP_ASSETS']
if ENV['FORCE_ASSETS'] || releases.length <= 1 || capture("cd #{latest_release} && #{source.local.log(source.next_revision(current_revision))} vendor/assets/ app/assets/ | wc -l").to_i > 0
system('bundle exec rake assets:precompile')
puts "syncing assets with shared directory..."
%x{rsync --recursive --times --rsh=ssh --compress --human-readable --progress public/assets #{user}@#{host}:#{shared_path}}
system('bundle exec rake assets:clean')
else
@sentientmonkey
sentientmonkey / test-logging.rb
Last active December 15, 2015 04:09
hourly log rotation in ruby logger via rotatelogs
#!/usr/bin/env ruby
require 'logger'
# rotatelogs required...
# http://httpd.apache.org/docs/2.2/programs/rotatelogs.html
logger = Logger.new("|rotatelogs ./foo.log.%Y-%m-%d-%H 3600", 0, 0)
10.times do
logger.error "testing..."
@PatrickTulskie
PatrickTulskie / unicorn
Created October 19, 2012 13:54
Unicorn init.d Script
#!/bin/sh
set -e
# Setup current directory path
APP_ROOT=/path/to/your/app/current
# Unicorn needs to know to write the PID file here. Adjust this for your own setup.
PID=$APP_ROOT/tmp/pids/unicorn.pid
class Handlers
constructor: ->
@handlers = {}
register: (handler, registered_class) ->
@handlers[handler] = registered_class
instantiate: (handlers, element) ->
handlers = handlers.replace(/\s/g, '').split(',')
element.handlers = []