Skip to content

Instantly share code, notes, and snippets.

View robwilliams's full-sized avatar

Rob Williams robwilliams

View GitHub Profile
defmodule Chop do
def guess(actual, range = low..high) do
_guess(actual, div(low+high, 2), range)
end
defp _guess(actual, actual, _) do
"YOU GOT IT #{actual} it is!"
end
defp _guess(actual, attempt, low.._) when actual < attempt do
module Algolia
module Factory
class VariantAssociation
def initialize(association, mapper=nil)
@association = association
@mapper = mapper || Mapper::Variant.new
end
def build
objects = association.variants.map do |variant|
@robwilliams
robwilliams / application_helper_spec.rb
Created February 23, 2016 12:01 — forked from cblunt/application_helper_spec.rb
Testing content_for Helper Methods with RSpec
# Solution from http://www.jamiepenney.co.nz/2011/09/23/testing-content_for-in-rails-3-x-helpers-with-rspec/
describe ApplicationHelper do
describe "title" do
it "should return an h2 tag with the given title" do
helper.title("Lorem Ipsum")
helper.content_for(:page_title).should eql "<h2>Lorem Ipsum</h2>"
end
it "should return the supplied block if a block was given" do
@robwilliams
robwilliams / gist:a99ec5a4cb63fcbd4566
Last active January 20, 2016 00:34
Alternative block syntax for operations in Trailblazer
# original
def create
run Admin::Product::Create do |op|
redirect_to admin_product_path(op.model)
return
end
render :new
end
@robwilliams
robwilliams / generate_pdf.md
Last active October 16, 2015 22:19
Generate PDF from url
@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
host = ENV.fetch('HOST', 'localhost:3000')
config.action_mailer.default_url_options = { host: host }
config.action_mailer.asset_host = "http://#{ENV['HOST']}"
class CurrentCountry
def initialize(session, env, freegeoip)
@session = session
@env = env
@freegeoip = freegeoip
end
def fetch
(
Country.find_by(id: session[:country_id]) ||
require 'freegeoip'
require 'ecb_rates'
module ExternalServices
module_function
def freegeoip
@freegeoip ||= Freegeoip.new(ENV.fetch('FREEGEOIP_URL'))
end