Skip to content

Instantly share code, notes, and snippets.

View amiel's full-sized avatar

Amiel Martin amiel

View GitHub Profile
@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
@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 / 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:
module ApplicationHelper
def current_user
super.decorator
end
end
class Asset < AR
end
class Alert < AR
belongs_to :asset
has_many :logs
accepts_nested_attributes_for :logs
end
class Log < AR
@amiel
amiel / README.md
Created November 1, 2011 20:36
Can Haz Emoji?

🐻 :octocat: 🐨

@amiel
amiel / README.md
Created October 28, 2011 19:08
post-commit git hook for simple deploys

Put the ruby file in .git/hooks/post-commit

Create a deploy script as .deploy_*

Example:

# File: .deploy_production
scp -R html example.com:public_html

Then use "deploy *" in your commit message.

@amiel
amiel / helpers.rb
Created October 27, 2011 21:59 — forked from adrianpike/helpers.rb
Page title helper for I18n with to_s for :show!
def page_title()
object_name = controller_name.singularize
if controller.instance_variables.include?(('@' + object_name).to_sym)
locals = {
object_name.to_sym => controller.instance_variable_get('@' + object_name).to_s
}
else
locals = {}
end
@amiel
amiel / sweeper.rb
Created May 6, 2011 21:49
DataMapper::Sweeper
module DataMapper
# DataMapper::Sweeper provides an observer to model changes that have access
# to the controller invoking the changes.
#
# The api for DataMapper::Sweeper is similar to DataMapper::Observer, but
# there is one important difference: in DataMapper::Observer .before and
# .after blocks are evaluated in the context of the resource instance.
# However, DataMapper::Sweeper .before and .after blocks are evaluated in the
# context of an instance of your Sweeper class, and missing methods are
@amiel
amiel / Use of the ! operator
Created April 28, 2011 21:18
not_operator.rb
# Some values, they are themselves
true # => true
false # => false
"value" # => "value"
0 # => 0
nil # => nil