Skip to content

Instantly share code, notes, and snippets.

View laserlemon's full-sized avatar

Steve Richert laserlemon

View GitHub Profile
@laserlemon
laserlemon / introduction.md
Last active February 12, 2021 10:04
Rendering form errors for non-database-backed changesets

Introduction

Disclaimer: I'm a one-day-old Elixir/Phoenix developer.

In Sonny's training yesterday, we used an Ecto.Changeset to handle user registration (a database-backed operation). This worked perfectly for rendering the intial form and re-rendering the form with validation errors.

This is because our Workshop.RegistrationController is calling Repo.Insert which sets the changeset's :action, whether or not the insertion succeeds. Then Phoenix.HTML's form_for function appropriately sets errors on the form so they can be rendered on the page.

The Problem

@laserlemon
laserlemon / technical-debit.md
Last active July 23, 2020 09:03
Keeping Track of Technical Debit

Keeping Track of Technical Debt

Don't forget your debt! Developers have a tendency to forget the bad or ugly code they write. In the Real World™, there are times when sacrifices in code quality need to be made in the interest of time. We call it "cowboy coding" and it's a slippery slope that can lead to loads of technical debt.

A big problem is that it's too easy for technical debt to be out of sight and out of mind. There are a few good ways to increase your debt's visibility.

Code Comments

Your first step should be to add code comments where appropriate to document technical debt as it arises. Fight the urge to deny the fact that you're writing less-than-awesome code. Try to include what's wrong with the code in question as well as at least one potential solution.

@laserlemon
laserlemon / ransack.rb
Created June 16, 2016 18:43
Ransack Sort Scopes
require "ransack/visitor"
require "ransack/adapters/active_record/context"
module Ransack
Visitor.class_eval do
def visit_Ransack_Nodes_Sort(object)
# The first half of this conditional is the original implementation in
# Ransack, as of version 1.6.6.
#
# The second half of the conditional kicks in when the column name
@laserlemon
laserlemon / Rails 2 Environment
Created October 1, 2010 12:11
Snow Leopard, Homebrew, Git, RVM, Ruby, Passenger, Apache, MySQL, Memcached and ImageMagick
# 2010-10-01
#
# Mac OS X 10.6.3
# Homebrew 0.7
# Xcode 3.2.4
# Git 1.7.3.1
# RVM 1.0.12
# Ruby 1.8.7, 1.9.2
# Passenger 2.2.15
# MySQL 5.1.49
@laserlemon
laserlemon / update_user.rb
Created November 14, 2017 16:28
Updating a record with rollback to original attributes
class UpdateUser
include Interactor
# Receives: user - A User record to update.
# new_attributes - A hash of attributes to be saved to the user.
# Updates: user
# Provides: old_attributes - A hash of only those user attributes that
# changed and their original values.
def call
@laserlemon
laserlemon / Rails 3 Environment
Created October 1, 2010 12:17
Snow Leopard, Homebrew, Git, RVM, Ruby, Rails, Passenger, Apache, MySQL, Memcached and ImageMagick
# 2010-10-01
#
# Mac OS X 10.6.3
# Homebrew 0.7
# Xcode 3.2.4
# Git 1.7.3.1
# RVM 1.0.12
# Ruby 1.9.2, 1.8.7
# Rails 3.0.0
# Passenger 3.0.0.pre4
@laserlemon
laserlemon / Rainbow.ino
Created January 23, 2014 19:26
Single LED color chaser
#include <Bridge.h>
#include <Console.h>
int red = 11;
int green = 10;
int blue = 9;
int redLevel = 255;
int greenLevel = 0;
int blueLevel = 0;
int defaultStep = 5;
@laserlemon
laserlemon / balanced_error.rb
Created November 23, 2013 14:45
How to create Balanced errors with Factory Girl
FactoryGirl.define do
trait :balanced_error do
skip_create
initialize_with do
new(body: attributes)
end
end
factory :balanced_payment_required, class: Balanced::PaymentRequired do
@laserlemon
laserlemon / should_deliver.rb
Created May 29, 2013 14:55
A matcher for email delivery
require "rspec/mocks"
module ShouldDeliver
class Expectation
include RSpec::Mocks::ExampleMethods
def initialize(klass, email_name)
email = mock(email_name).as_null_object
email.should_receive(:deliver).once
@laserlemon
laserlemon / Gemfile
Last active December 14, 2015 16:48
Attempt to reproduce Figaro issue re: Bundler.with_clean_env
source 'https://rubygems.org'
gem 'rails', '3.2.12'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'