Skip to content

Instantly share code, notes, and snippets.

Avatar

Tyler Rick TylerRick

  • USA
View GitHub Profile
@TylerRick
TylerRick / content_for_inside_cache.rb
Last active Jun 11, 2020 — forked from stackng/rails content_for caching
Add ability to use content_for within a fragment cache block (Rails 5.2)
View content_for_inside_cache.rb
# config/initializers/content_for_inside_cache.rb
module AbstractController
class Base
attr_internal :cached_content_for
end
module Caching
# actionpack/lib/action_controller/caching/fragments.rb
module Fragments
@TylerRick
TylerRick / detailed_hash_diff.rb
Last active May 26, 2020 — forked from fabriciofreitag/detailed_hash_diff.rb
Custom RSPec matcher for detailed hash compasion and diff
View detailed_hash_diff.rb
require 'facets/hash/recurse'
# Usage:
# expect(actual).to match_hash(expected)
#
RSpec::Matchers.define :match_hash do |expected|
match do |actual|
# Sort hashes before comparing so that the diff only shows actual changes between keys and
# values.
actual = actual.recurse {|h| h.sort_by {|k,v| k.to_s }.to_h }
@TylerRick
TylerRick / action_mailer-dont_log_attachments.rb
Last active May 13, 2020
Override the version from the actionmailer gem in order to not log attachments.
View action_mailer-dont_log_attachments.rb
ActionMailer::Base.class_eval do
class << self
protected
# Override the version from the actionmailer gem in order to not log attachments.
#
# Note: This depends on the patch from https://github.com/mikel/mail/pull/858, which adds
# parts.inspect_structure and fixes an issue with mail.without_attachments!
#
@TylerRick
TylerRick / delegate_to_all.rb
Last active Mar 29, 2020
DelegateToAll. Like delegate.rb from Ruby's std lib but lets you have multiple target/delegate objects.
View delegate_to_all.rb
# DelegateToAll. Like delegate.rb from Ruby's std lib but lets you have multiple target/delegate objects.
require 'delegate'
class DelegatorToAll < Delegator
# Pass in the _obj_ to delegate method calls to. All methods supported by
# _obj_ will be delegated to.
#
def initialize(*targets)
__setobj__(targets)
@TylerRick
TylerRick / .gitignore
Created Jan 19, 2018 — forked from ZJONSSON/force_labels.js
Automatic floating labels using d3 force-layout
View .gitignore
d3-force-labels.js
@TylerRick
TylerRick / main.rb
Created Jan 16, 2018 — forked from coorasse/main.rb
CanCanCan Issue
View main.rb
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '5.1.4'
@TylerRick
TylerRick / group-required-by-name.js
Created May 22, 2012
'group-required-by-name' to require at least one checkbox in a group to be checked
View group-required-by-name.js
//--------------------------------------------------------------------------------------------------
// This is based on the 'group-required' rule from js-webshim/dev/extras/custom-validity.js but
// allows you to specify a group name, in case you need checkboxes with different names in the same
// group.
var groupTimer = {};
$.webshims.addCustomValidityRule('group-required-by-name', function(elem, val){
var $elem = $(elem);
var name = $elem.data('group-required');
var filter = '[data-group-required="' + name + '"]'
if (!name || elem.type !== 'checkbox') { return; }
@TylerRick
TylerRick / bootstrap-rvm-erb.sh
Created Mar 2, 2012
Bootstrap script for chef-solo that installs rvm, ruby, bundler, and chef (within a gemset)
View bootstrap-rvm-erb.sh
#!/bin/bash
# Actual filename: bootstrap/rvm.erb
# vim: set ft=sh
# This script automatically bootstraps the system with everything it needs to run chef-solo.
# Note: This should only do the absolute minimum necessary to get chef-solo (and your recipes)
# working. All else should be done via chef recipes.
#===================================================================================================
# Config
@TylerRick
TylerRick / example.rb
Last active May 2, 2016
Allows you to define non-persisted attributes in an ActiveRecord model, the same way you can do in a regular ActiveModel model. This lets you use conveniences like my_method_changed? from ActiveModel::Dirty, for example, on your non-persisted attributes. Tested with Rails 3.2.11
View example.rb
# Example usage:
require 'active_record/nonpersisted_attribute_methods'
class Node < ActiveRecord::Base
include ActiveRecord::NonPersistedAttributeMethods
define_nonpersisted_attribute_methods [:bar]
# Example of using with ancestry and getting :parent to show up in changes hash.
has_ancestry
define_nonpersisted_attribute_methods [:parent]
@TylerRick
TylerRick / reduce_precision_of_datetime_columns.rb
Last active Dec 20, 2015
Change all datetime columns to have a precision of 0, to match the default precision of these columns in MySQL. This is useful if you migrate a database from MySQL to PostgreSQL and don't need the extra microsecond precision (precision: 6) that is the default in PostgreSQL (see http://www.postgresql.org/docs/9.2/static/datatype-datetime.html).
View reduce_precision_of_datetime_columns.rb
# Change all datetime columns to have a precision of 0, to match the default precision of these
# columns in MySQL.
#
# This is useful if you migrate a database from MySQL to PostgreSQL and don't need the extra
# microsecond precision (precision: 6) that is the default in PostgreSQL
# (see http://www.postgresql.org/docs/9.2/static/datatype-datetime.html).
#
# Before:
# > some_record.created_at.strftime('%Y-%m-%d %H:%M:%S.%N')
# => "2013-07-25 11:49:33.270032000"