Skip to content

Instantly share code, notes, and snippets.

View eiwi1101's full-sized avatar

William Eisert eiwi1101

View GitHub Profile
@eiwi1101
eiwi1101 / application_record.rb
Created October 15, 2018 19:58
Log validation errors and changes for all ActiveRecord updates.
class ApplicationRecord << ActiveRecord::Base
#...
after_validation :log_validation_errors
private
def log_validation_errors
Rails.logger.tagged self.class.name do
if self.errors.empty?
@eiwi1101
eiwi1101 / namespace.js
Last active December 21, 2017 14:05
It's like `mkdir -p` but for Javascript objects! (Probably an anti-pattern.)
// I often do stuff like (coffeescript):
//
// class @Forms.Select extends React.Component
//
// Which, often requires an ugly @Forms ||= {} somewhere, or this will
// explode. Now, I know there's better ways to do this, and CommonJS has
// its nifty resource stuff, but I am old and stuck in my ways and stuff.
//
// With this snippet (if you're in rails, you can include it in application.js
// before your components dir), you can write (again, coffee):
# Consider this common pattern...
if (user = User.find params[:id])
user.profile_viewed
end
# Would be a bit less awkward if...
with User.find params[:id] do |user|
user.profile_viewed
@eiwi1101
eiwi1101 / DateFormat.jsx.coffee
Created October 5, 2017 18:02
DateFormat react component for formatting timestamps!
@DateFormat = React.createClass
propTypes:
timestamp: React.PropTypes.number.isRequired
fuzzy: React.PropTypes.bool
dateOnly: React.PropTypes.bool
short: React.PropTypes.bool
className: React.PropTypes.string
getInitialState: ->
timer: null
@eiwi1101
eiwi1101 / items.rb
Last active July 12, 2018 21:58
Some noodling for an RPG backend.
class Items::Flour < Item
name "Flour"
description "Looks like crushed up wheat to you. Keep dry."
value 2.copper
stack_size 20
# Use with a Bucket of Water to create Dough. Requires
# level 1 or greater cooking.
use_with Item::BucketOfWater, create: [Item::Dough, Item::Bucket], skills: { Skill::Cooking => 1 }
@eiwi1101
eiwi1101 / App.js.jsx.coffee
Created April 2, 2017 21:07
Personal React style guide 0.1
@App = React.createClass
#== Validations
propTypes: {}
contextTypes: {}
#== Initialize
# As if Ruby didn't make dates any easier, this!
# (#method_missing is disgusting and I love it.)
#
# 25.december 2015 #=> Fri, 25 Dec 2015
#
class Fixnum
def method_missing(month, year=nil)
Date::MONTHNAMES.each_with_index do |m, i|
return Date.new year, i, self if m && month.to_s.casecmp(m)==0
end
@eiwi1101
eiwi1101 / has_guid.rb
Created December 12, 2016 17:24
HasGuid a concern for automatically adding and maintaining a GUID/UUID/Token etc on your objects.
module HasGuid
extend ActiveSupport::Concern
included do
before_validation :generate_guid
class_attribute :guid_column_name
class_attribute :guid_options
validate :validate_guid
module CollectionHelper
#...
# This does a shitload of magic. It handles sort, order, filter, page and search.
#
def self.filter_scope(scope, params = {})
unpermitted_params = []
params = params.with_indifferent_access
params[:sort] ||= 'id'
@eiwi1101
eiwi1101 / post.rb
Last active April 4, 2016 15:50
Sluggable : An ActiveSupport Concern for generating URL slugs on a Model.
# === Table Schema
#
# id: integer
# title: string
# body: string
# author_id: integer
#
class Post < ActiveRecord::Base
include Sluggable
belongs_to :author