Skip to content

Instantly share code, notes, and snippets.

View ryenski's full-sized avatar
🎯
Focusing

Ryan Heneise ryenski

🎯
Focusing
  • Austin, TX
View GitHub Profile
@ryenski
ryenski / sermons
Created May 2, 2012 18:55
wpcajax.org Sermons
//<![CDATA[
//-----Classes-----
function SermonProperty(data)
{
this.data = data;
this.html = data;
this.compareTo = function(obj)
{
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
def render_404
raise "A Page was not found!"
end
end
Verifying that "crispinheneise.id" is my Blockstack ID. https://onename.com/crispinheneise
@ryenski
ryenski / amount_cents.rb
Last active April 11, 2017 15:51
A simple way to deal with monetary amounts in cents, without using a library
# amount_cents :integer default("0")
def amount=(amount)
amount = amount.gsub(/[^\d\.]/, '').to_f if amount.is_a?(String)
self.amount_cents = amount * 100
end
def amount
amount_cents / 100.0
end
@ryenski
ryenski / activity_creator.rb
Last active April 27, 2017 15:18
A service object that takes a trackable object, action, and parameters, and creates an Activity record for action auditing.
class ActivityCreator
def initialize(action, trackable, contact, comment=nil)
@trackable = trackable # the affected object (could be the same as contact)
@action = action # what was done
@comment = comment
@changes = collect_changes
end
def call
@ryenski
ryenski / trackable.rb
Last active April 27, 2017 14:36
An ActiveSupport concern that adds commit hooks for action auditing.
module Trackable
extend ActiveSupport::Concern
included do
has_many :activities, -> {order("created_at DESC")}, as: :trackable, dependent: :nullify
after_commit :track_activity
before_destroy :track_destroy
end
def track_destroy
@ryenski
ryenski / activity.rb
Created April 27, 2017 14:37
An ActiveRecord model that stores the tracked changes for our action auditor.
# == Schema Information
#
# Table name: activities
#
# id :uuid not null, primary key
# user_id :uuid
# tenant_id :uuid
# contact_id :uuid
# trackable_type :string
# trackable_id :uuid
@ryenski
ryenski / trackable_model.rb
Created April 27, 2017 14:39
Including the Trackable concern in our ActiveRecord model.
class Message < ApplicationRecord
include Trackable
# ...
end
@ryenski
ryenski / numbers.rb
Created May 4, 2017 15:45
kata: Write a function named numbers that returns true if all the parameters it is passed are a number. Otherwise return false.
# numbers.rb
def numbers(*ary)
ary.all?{|a| a.is_a?(Numeric)}
end
numbers(1,2,3.0)
# => true
numbers(1,2,'foo')
# => false
class PronounceablePassword
WORDS = (IO.readlines(File.join File.dirname(__FILE__), 'words.txt')).each { |w| w.chop! }
class << self
def random_pronouncable_password(options={})
options={
:rand_seed => 999
}.update(options)
words = WORDS.dup
[words.delete_at(rand(words.length)), rand(options[:rand_seed]), words[rand(words.length)]].join("-")