Skip to content

Instantly share code, notes, and snippets.

View paulnsorensen's full-sized avatar

Paul Sorensen paulnsorensen

View GitHub Profile
@paulnsorensen
paulnsorensen / issues_with_modules.md
Created June 1, 2016 05:51 — forked from ryanb/issues_with_modules.md
Points on how modules can make code difficult to read.

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article < ActiveRecord::Base
hotel_ids
=> [24054, 27978, 28132, 2698, 3168, 2752, 17804, 20826, 19918, 28077, 629, 25047, 23685, 26019, 21146, 26203, 23357, 28095, 19652, 27644, 1242, 1888, 1338, 2131, 2591, 2723, 24090, 24166, 5665, 3057, 1890, 28446, 1889, 28260, 27592, 28170, 28403, 20940, 2027, 24059, 1362, 2036, 2242, 5598, 1324, 24287, 27548, 3346, 2011, 21151, 1730, 1695, 2758, 17157, 19496, 26948, 5777, 3103, 6301, 2769, 4715]
@paulnsorensen
paulnsorensen / recipe.md
Last active August 29, 2015 14:14 — forked from harlow/recipe.md

Mezcal Bone Crusher

Here is a variation of a Bone Crusher, a drink originally inspired by acclaimed cocktail writer David Wondrich's Bone cocktail made from rye whiskey, lime juice, sugar and a few dashes of Tabasco.

Here, this spicy, earthy, smoky drink was made from Del Maguey's Single Village Mezcal (Minero), El Tesoro Reposado Tequila, demerara sugar simple syrup, lime and Tabasco.

The Breakdown

3 Fresh Limes
@paulnsorensen
paulnsorensen / _spec_performance.html.erb
Created June 6, 2014 16:10
Disabling CSS animations in tests
<%# http://www.venombytes.com/2013/03/11/faster-browser-specs %>
<% if Rails.env.test? -%>
<style type="text/css">
.notransition * {
/*CSS transitions*/
-webkit-transition: none !important;
-moz-transition: none !important;
-o-transition: none !important;
-ms-transition: none !important;
for i in app/models/*.rb ; do sed "s/\s\+def \(self\.\)\?\([^( ]*\).*\|\s\+scope :\([^,]*\).*/\2\3/;tx;d;:x" $i | while read j ; do echo $(grep -R $j app/ lib/ config/ | wc -l) $j ; done ; done | sort -rn
# Instructions for this test:
# 1. Please clone this gist as a git repo locally
# 2. Create your own github repo called 'rubytest' (or a name of your choice) and add this repo as a new remote to the cloned repo
# 3. Edit this file to answer the questions, and push this file with answers back out to your own 'rubytest' repo.
# Problem 1. Explain briefly what this code does. Create a RSpec unit test for
# it, fix any bugs, then clean up the syntax to be more idiomatic Ruby and make
# sure your tests still pass.
#
# Your spec should have at least 3 contexts/conditional tests (what are they?)
@paulnsorensen
paulnsorensen / foo.rb
Last active September 5, 2019 19:05
UPDATE: I have way better code that accomplishes this now at [https://github.com/paulnsorensen/lifesaver] Module to ease associated models that are indexed by tire. Use this to trigger reindexing instead of having to touch all the files.
#
# Please go to https://github.com/paulnsorensen/lifesaver
#
class Foo < ActiveRecord::Base
has_many :bars
belongs_to :baz
include IndexingHandler
indexed_associations :bars, :baz
end