Skip to content

Instantly share code, notes, and snippets.

View amiel's full-sized avatar

Amiel Martin amiel

View GitHub Profile
@amiel
amiel / application_decorator.rb
Created November 3, 2011 22:41
Some handy I18nization for my ApplicationDecorator
class ApplicationDecorator < Draper::Base
# See +ApplicationDecorator.humanize+
def humanize(attribute, key = model.send(attribute), default = key.to_s.humanize)
self.class.humanize attribute, key, default
end
# By default, humanize the attributes listed.
#
# Contrived Example:
@amiel
amiel / 1.html.erb
Created November 4, 2011 19:55
Using I18n and Draper to Render Database Attributes Blog Post
# Show a user-friendly version of our identifier
Status: <%= current_user.status.humanize %>
# Now we need to customize some of them, use I18n
Status: <%= t current_user.status, default: current_user.status.humanize %>
# But that's polluting our I18n namespace
Status: <%= t :"user.status.#{ current_user.status }", default: current_user.status.humanize %>
# Ok, this is getting out of hand, lets refactor this to the model
@amiel
amiel / 1_instead_of.rb
Created November 10, 2011 19:27
PROTIP
# Instead of this
after_create :advance_plan
def advance_plan
plan.advance if plan
end
SHIP IT
@amiel
amiel / gist:1477839
Created December 14, 2011 18:29
Work-around for for disappearing HTML output section in TextMate 2

Avian hidden HTML Output Work Around

Open ~/Library/Application Support/TextMate/Session/Info.plist (the XCode plist editor makes it nice and easy, but you can open it in textmate as well if you are comfortable editing plists).

You are looking for htmlOutputHeight. Mine was set to a negative value, setting it to a positive value should fix the issue (it did for me).

@amiel
amiel / custom_validation_matcher.rb
Created December 24, 2011 03:32
I was fed up with "expected valid? to return true". Now you get errors
module CustomValidationMatcher
class IsValid
def matches?(target)
@target = target
@target.valid?
end
def failure_message
"expected #{ @target.class.name } to be in valid, errors were: #{ @target.errors.full_messages.inspect }"
@amiel
amiel / my_model.rb
Created February 4, 2012 04:40
A nice pattern for validating the type in STI
class MyModel < ActiveRecord::Base
include ActiveSupport::DescendantsTracker
validates_inclusion_of :type, :in => direct_descendants.map(&:name)
# ...
end
@amiel
amiel / capybara_select_dates_and_times.rb
Created March 7, 2012 01:02 — forked from szimek/capybara_select_dates_and_times.rb
Cucumber steps for selecting time and date (using Capybara)
require "xpath" # XPath is a separate gem now
module Cucumber
module Rails
module CapybaraSelectDatesAndTimes
def select_date(field, options = {})
date = Date.parse(options[:with])
selector = %Q{.//fieldset[contains(./legend, "#{field}")]}
within(:xpath, selector) do
find(:xpath, './/select[contains(@id, "_1i")]').find(:xpath, ::XPath::HTML.option(date.year.to_s)).select_option
@amiel
amiel / gist:2288698
Created April 3, 2012 01:49
Metadata pattern
class SomeModel < ActiveRecord::Base
# These are all the things I tend to add to my activerecord model for the metadata (extension table) pattern
has_one :metadata, autosave: true, dependent: :destroy, class_name: 'SomeModelMetadata'
def metadata_with_creation
metadata_without_creation || build_metadata
end
alias_method_chain :metadata, :creation
delegate :html, :html=, :html?,
:start_at, :start_at=, :start_at?,
@amiel
amiel / gist:2295633
Created April 3, 2012 21:33
I wonder if this is causing problems
$ for g in $(for p in `echo $PATH|sed 's/:/ /g'`;do if [ -f $p/git ];then echo $p/git; fi;done|sort|uniq);do echo $g;$g --version;done
/usr/bin/git
git version 1.7.7.5 (Apple Git-26)
/usr/local/bin/git
git version 1.7.9.5
/usr/local/git/bin/git
git version 1.7.6