Skip to content

Instantly share code, notes, and snippets.

@kellysutton
kellysutton / 01-react-inline.html.erb
Created December 23, 2020 14:38
Sketch of React rendering Rails ERB
<main>
<% @posts.each do |post| %>
<!-- Under the hood, we find the right file in webpack and attach the expected object to window.Post -->
<Post
title={post.title}
description={post.description}
/>
<% end %>
</main>
<<~MSG
Squiggle
MSG
<<-MSG
No-squigs
MSG
CHONSTANT = 'CONSTANT'
@kellysutton
kellysutton / payroll_calculator_step_0.rb
Created December 7, 2017 16:51
Refactoring: Removing Tangled Control Coupling
class PayrollCalculator
def initialize(payroll, options = {})
@payroll = payroll
@skip_benefits = options[:skip_benefits]
@skip_taxes = options[:skip_taxes]
@skip_donations = options[:skip_donations]
end
def calculate
if !@skip_benefits
@kellysutton
kellysutton / extracted_slim_taxes.rb
Created September 11, 2017 22:58
Embracing Functional Programming in Ruby
def taxes
PayrollCalculator::Taxes.calculate(
@payroll.only_pay_and_location_data
)
end
@kellysutton
kellysutton / bad_comment.rb
Last active August 31, 2017 03:42
Comment Drift
# This methods adds 2 numbers together
# @param x [Number] The first number that you would
# like to add
# @param y [Number] The number you would
# like to add to the first number
# @return [Number] The result of adding `x` and `y` together
def add(x, y)
x + y
end
'use strict';
const http = require('https');
// Forward the button press onto our Heroku app
// as a POST request with a JSON body.
exports.handler = (event, context, callback) => {
const params = {
serialNumber: event.serialNumber,
event: event
require 'rails_helper'
RSpec.describe FrequentFlierCreator do
describe '.create' do
subject do
described_class.create(
customer,
starting_balance,
status_level
)
@kellysutton
kellysutton / bigger_user_spec.rb
Last active June 16, 2017 15:24
Count the Contexts
RSpec.describe User do
describe '#full_name' do
subject do
described_class.new(first_name, middle_name, last_name)
end
let(:first_name) { 'Roy' }
let(:last_name) { 'Biv' }
context 'middle name is given' do
let(:middle_name) { 'Gee' }
@kellysutton
kellysutton / best_payroll_runner.rb
Last active June 12, 2017 00:10
Replace Side Effects with Return Values
class PayrollRunner
def run!
TaxEngineWrapper.new(tax_engine).apply(commands)
computed_taxes = tax_engine.calculate_taxes
payroll.assign_taxes(computed_taxes)
end
# This is now a pure function from the view of its callers.
# Sweet!
def commands
class TaxCalculationSaver
def self.save_taxes!(payroll)
total_tax_amount = payroll.employees.map do |employee|
TaxCalculator.calculate(payroll, employee)
end.sum
PayrollSaver.save!(payroll, total_tax_amount: total_tax_amount)
end
end