Skip to content

Instantly share code, notes, and snippets.

View damien's full-sized avatar
🤝
Open for business! Hire me for your project at https://mindglob.com

Damien Wilson damien

🤝
Open for business! Hire me for your project at https://mindglob.com
View GitHub Profile
@damien
damien / git-log.txt
Created April 1, 2020 18:59
Git log after git subtree split+add
damien@Apollonius ~/workspace/Cataclysm-DDA [dinomod-subtree]
± % git log --oneline --decorate -- data/mods/DinoMod | wc -l
54
damien@Apollonius ~/workspace/Cataclysm-DDA [dinomod-subtree]
± % git log --oneline --decorate -- data/mods/DinoMod | cat
2b43ec698b (HEAD -> dinomod-subtree, origin/dinomod-subtree) Add 'data/mods/DinoMod/' from commit '0d385a83106bec4a9fc73d5576fe5b61974d0db9'
bfa16029be Prepare DinoMod for git subtree inclusion
5d76bd145b Aftershock and dinomod monsters name to object (#38963)
47499c5055 Change maintainer of DinoMod to @damien (#37490)
@damien
damien / date_time_comparisons_in_active_support.md
Last active July 15, 2016 14:15
Quick dive into how Rails/ActiveSupport does Date, Time, and DateTime comparisons

Date and Time comparisons in ActiveSupport

Question: How do we safely compare a date to an unknown value within a Rails application? Example: params[:date_field] > 2.years.ago

Doing a bit of digging, it looks like 2.years.ago returns an instance of ActiveSupport::TimeWithZone. In ruby, whenever you do a comparison of any sort ruby will follow the rules outlined in the Comparable module of stdlib.

ActiveSupport::TimeWithZone will create a instance of Time in a UTC time zone when doing comparisons with ActiveSupport::TimeWithZone#<=>.

@damien
damien / recurse.rb
Last active August 29, 2015 14:23
Utilities for performing recursive operations on an object
module Recurse
# Iterate over obj and all of it's values, copying them into a flat array
# @return [Array]
# @example Flattening a nested hash
# { foo: { bar: { fizz: 'buzz' } } }.flatten_recursively
# # => [:foo, :bar, :fizz, "buzz"]
#
# @example Flattening an array of mixed objects
# [{ fizz: :buzz }, { foo: :bar }, [1, [2, 3]]].flatten_recursively
# # => [:fizz, :buzz, :foo, :bar, 1, 2, 3]
@damien
damien / prime.rb
Created April 14, 2015 15:20
Lazily generate primes in ruby
# Functional implementation of the Sieve of Eratosthenes
#
# Once initialized, an instance of this class may be used to generate an
# infinite number of primes.
#
# @see https://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
# @see http://raganwald.com/2013/02/23/sieve.html
#
# Example:
# @sieve = EratosthenesIncrementalSieve.new
@damien
damien / as_logger.rb
Created January 23, 2015 23:10
Nicely formatted logger
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = -> (severity, datetime, progname, msg) { format('[%-5.5{severity} %{timestamp}] %{msg}', severity: severity, timestamp: datetime.to_s(:db), msg: msg) + "\n" }
logger.unknown 'foo'
logger.fatal 'bar'
logger.error 'fizz'
logger.warn 'buzz'
logger.info 'alice'
logger.debug 'bob'
@damien
damien / course_code.rb
Created December 18, 2014 17:20
Course code parser
# A parser for course code strings
#
# Course codes are unique identifiers used to identify particular classes instructed at an edicuational institution.
#
# There isn't any real standard for course codes, but the examples below demonstrate common formats for course codes
# in the places where this parser is used.
#
# The parser itself is generated using [Treetop](https://github.com/nathansobo/treetop), so look there if you're interested
# in the underlying framework that generates the CourseCodeParser class when we load up our parser grammar.
#
@damien
damien / team_membership.rb
Last active April 28, 2021 12:50
Using MiniTest to mock and test ActiveRecord callbacks in Rails 4.2
class TeamMembership < ActiveRecord::Base
# A proc that will enqueue `NotificationMailer.team_invitation`
DEFAULT_NOTIFIER = proc do |user, team|
NotificationMailer.team_invitation(team, user).deliver_later
end
class << self
# This is a class level attribute that is mainly used for testing.
# Defaults to {TeamMembership::DEFAULT_NOTIFIER}
attr_accessor :notifier
class SearchController < ApplicationController
def index
if search_params.blank? && where_params.blank?
@portfolios = Portfolio
.includes([:user, :portrait])
.where(published: true)
.order('created_at DESC')
else
@portfolios = search_portfolio search_params, where_params