Skip to content

Instantly share code, notes, and snippets.

View nthj's full-sized avatar

Nathaniel Jones nthj

  • Orlando, FL
View GitHub Profile
module AccessibleBlock
extend ActiveSupport::Concern
module ClassMethods
@accessible_block = false
def key(*arguments, &block)
super
attr_accessible arguments[0] if @accessible_block
end
@nthj
nthj / application_controller.rb
Created April 1, 2011 19:32
Rails controller before/after shortcuts (ruby 1.9.2 compatible)
class ApplicationController < ActionController::Base
extend Filterable
after_update -> { user.clear_cache! }
end
@nthj
nthj / string.rb
Created April 5, 2011 00:53
Ruby string extensions for easily defining questionable methods like "email?"
class String
def questionable
questionable? ? to_s : "#{to_s}?"
end
def questionable!
replace questionable
end
def questionable?
@nthj
nthj / object.rb
Created April 5, 2011 01:27
Ruby object extension for use with HAML
# Easily add id and class attributes to HAML elements
#
# %li{ account.element.to_hash } ->
# <li class='account' id='account-7'>
#
# %span{ account.element.button.to_hash } ->
# <span class='account-button' id='account-7-button'>
#
# %span{ :class => account.element.save.button.class } ->
# <span class='account-save-button'>
@nthj
nthj / hash.rb
Created April 5, 2011 01:31
Ruby Hash filter (Array#map for Hashes)
# This isn't mine
# I can't remember where I found it, but mad props to whoever created it
class Hash
def filter
result = self.map do |k, v|
r = yield v
[k, r]
end
Hash[*result.flatten]
@nthj
nthj / constituent_importer.rb
Created June 6, 2011 03:58
Constituent Importer for Capvoice
# constituent_importer.rb
#
# When the customer uploads an Excel file,
# we generate a unique ID for the file and
# store it on our AWS/S3 jobs bucket.
#
# Then we create a job with that unique ID,
# and our background workers execute the job as follows
class ConstituentImporter < Struct.new(:job_object_id)
@nthj
nthj / password.rb
Created June 6, 2011 04:15
Password value object for MongoMapper documents
# In a MongoMapper::Document,
# simply add
# key :password, Password
# your password will be automatically hashed,
# and you can compare plain-text passwords with the user's hashed password, ruby style:
# User.first.password == '1234'
# => true
#
# Also, prevents passwords from being leaked onto the console or error messages:
# User.first.password
@nthj
nthj / filterable.rb
Created June 6, 2011 04:29
Add Rails Controller filters aliases, like after_show, before_destroy
module Railings
module Filterable
[:after, :before].each do |context|
[:index, :show, :new, :create, :update, :destroy].each do |action|
define_method "#{context}_#{action}", ->(filter, options = { }, &block) do
send "#{context}_filter", filter, options.merge!(:only => action), &block
end
end
end
end
@nthj
nthj / gist:1009755
Created June 6, 2011 04:41
Create thumbnail of provided key, and save it as {key name}_medium.jpg
# if "key" equals "bird.jpg",
# then this code creates a smaller thumbnail of the bird,
# sized to 270 by 167 pixels,
# adding a watermark,
# compressing the thumbnail to save space,
# and saving the new thumbnail as "bird_medium.jpg"
Original.find(key).compress(70).resize(270, 167).to(:fit).watermark.save(:medium)
@nthj
nthj / gist:1041160
Created June 22, 2011 20:55
npa function for calculating gross receipts when adding customers over a certain duration
# n = number of additional customers per period
# p = amount each customer is charged per period
# a = duration of period to calculate total gross for
#
# example: customer every 2 weeks, 25 per month, after 6 months: (fn).call(2, 25, 6)
# => 1050
->(n, p, a) { t = 0; a.times { |i| t += (i + 1) * p * n }; t }