Skip to content

Instantly share code, notes, and snippets.

View galtenberg's full-sized avatar

Christopher Galtenberg galtenberg

View GitHub Profile
@scrogson
scrogson / config-initializers-sandbox_email_interceptor.rb
Last active August 29, 2015 14:00
Intercept outgoing email in environments outside of production.
# config/initializers/sandbox_email_interceptor.rb
ActionMailer::Base.register_interceptor(SandboxEmailInterceptor) unless Rails.env.production?
@stephencelis
stephencelis / minidress.rb
Created January 10, 2010 16:18
More "proper" than a miniskirt (http://gist.github.com/273579).
# More "proper" than a miniskirt (http://gist.github.com/273579).
class Minidress
@@factories = {}
class << self
def define(name, &block)
@@factories[name.to_s] = block
end
def build(name, attrs = {})
new(name.to_s.classify.constantize.new, &@@factories[name.to_s]).record
@josevalim
josevalim / _yerb.rb
Created March 18, 2010 16:41
Who needs HAML when you have YAML + ERB?
# = YERB
#
# Who needs HAML when you have YAML + ERB? ;)
#
# See example.yaml below for an example. You can run this code
# by cloning this gist and then `ruby _yerb.rb example.yaml`.
#
# Notice that you need Ruby 1.9 so the hash order is preserved.
# Obviously, this is just for fun. Definitely slow as hell.
#
# More "proper" than a miniskirt (http://gist.github.com/273579).
class Minidress
@@factories = {}
class << self
def define(name, &block)
@@factories[name.to_s] = block
end
def build(name, attrs = {})
new(name.to_s.classify.constantize.new, &@@factories[name.to_s]).record
# Factory girl, relaxed.
#
# Factory.define :user do |f|
# f.login 'johndoe%d' # Sequence.
# f.email '%{login}@example.com' # Interpolate.
# f.password f.password_confirmation('foobar') # Chain.
# end
#
# Factory.define :post do |f|
# f.user { Factory :user } # Blocks, if you must.
@cavalle
cavalle / cohesion-and-big-models.md
Last active December 11, 2015 21:09
Cohesion and Big ActiveRecord Models

Cohesion and Big ActiveRecord Models

One of the problems of big ActiveRecord models is their low cohesion. In Rails we group behaviour around the entities of the domain we're modelling. If we use only ActiveRecord models for that, we'll probably end up with classes full of actions, in most cases, completely unrelated to each other (except for the fact that they act on the same domain entity).

To illustrate the issue let's imagine a big Post AR model of a blog application. In this app, users can add tags to their posts. Since this is the only class with this ability, I decide to put the related logic in the Post model itself. For this, presumably at the beginning of the file, I write a couple of has_many entries for tags and taggings. Then, fifty lines below, along with the rest of scopes, I add one more to find posts by tag name. A couple of hundred lines later, I put a virtual attribute tag_list which will be used to update the associations from a string of tag names separated by commas. Fin

@swistak35
swistak35 / dci_with_hacks.rb
Last active December 14, 2015 11:08
DCI using EVIL.RB, Wroc_Love.rb 2013
# Firstly, install gems: 'evilr' and 'include'
# Example with User in a shop application.
# He has two roles
# - buyer (with money on the account and so on)
# - reviewer (he can review books he bought)
require 'evilr'
require 'include'
@sentientmonkey
sentientmonkey / test-logging.rb
Last active December 15, 2015 04:09
hourly log rotation in ruby logger via rotatelogs
#!/usr/bin/env ruby
require 'logger'
# rotatelogs required...
# http://httpd.apache.org/docs/2.2/programs/rotatelogs.html
logger = Logger.new("|rotatelogs ./foo.log.%Y-%m-%d-%H 3600", 0, 0)
10.times do
logger.error "testing..."
@thbar
thbar / Guardfile
Last active December 15, 2015 10:28
How to automatically restart the simulator in RubyMotion when code is updated (beta version!)
require 'childprocess'
guard 'shell' do
watch %r{^app/(.+)\.rb$} do |m|
`killall rake`
# Why this:
# - spawn a child process to avoid locking Guard
# - make sure that the child process has stdout and stdin otherwise it crashes
# - bonus point: get REPL access in the simulator!
@gustafnk
gustafnk / poll_twitter_favorites.js
Last active December 28, 2015 12:09
List all your twitter favorites - slow and dirty. You must be on your twitter favorites page. When done, do "Inspect Element" and copy the html tag, then paste it into a text editor and save.
var poll = function(){
window.scrollTo(0, document.body.scrollHeight);
setTimeout(function(){ poll() }, 500);
};
poll();