Skip to content

Instantly share code, notes, and snippets.

View emdeeeks's full-sized avatar

Gareth Griffiths emdeeeks

View GitHub Profile
class Test::Unit::TestCase
# assert_form posts_url, :put do
# assert_text_field :post, :title
# assert_text_area :post, :body
# assert_submit
# end
def assert_form(url, http_method = :post)
http_method, hidden_http_method = form_http_method(http_method)
assert_select "form[action=?][method=#{http_method}]", url do
if hidden_http_method
#recursively require all files in directory (and subdirectories)
Dir["#{File.dirname(__FILE__)}/squat/**/*.rb"].each {|file| require file }
#recursively require all files in directory but skip paths that match a pattern
Dir["#{File.dirname(__FILE__)}/squat/**/*.rb"].each do |file|
require file unless file =~ /\/model\//
end
@dira
dira / omniauth.rb
Created December 1, 2010 01:56
OmniAuth strategy for a custom provider
# config/initializers/omniauth.rb
module OmniAuth
module Strategies
# tell OmniAuth to load our strategy
autoload :Pixelation, 'lib/pixelation_strategy'
end
end
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitter, "app_name", "secret"
@samuelkadolph
samuelkadolph / application_model.rb
Created December 29, 2010 23:08
How to safely add a class method to your models that affects attributes
class ApplicationModel < ActiveRecord::Base
self.abstract_class = true
class_attribute :formatted_attributes_options
self.formatted_attributes_options = {}
def self.formatted_attributes(*attributes)
options = attributes.extract_options!
attributes.each do |attribute|
formatted_attributes_options[attribute] = options
@myobie
myobie / application_helper.rb
Created May 19, 2011 17:11
My try at a good presenter pattern with rails
module ApplicationHelper
def present(model, items = {})
name = model.is_a?(String) ? model : model.class.model_name.underscore
items.merge!(name.to_sym => model) unless model.is_a?(String)
"#{name.classify}Presenter".constantize.new(controller, items) do |presenter|
yield(presenter) if block_given?
end
@schowdhury
schowdhury / gist:1017462
Created June 9, 2011 19:07
vanilla ruby class with list of activerecord objects integrates with rails form helpers
#I want to create many of a thing in one shot where thing is an activerecord class with
#validations. I can use the following where a listofthings is a vanilla ruby class. on
#valid? this class calls all the contained object's valid? method. and a naive save that
#calls save on each of the contained activerecord objects. There's some syntatic sugar,
#methods that hide the underlying list. This is minimal code for explanation.
#My thing is an activerecord class
#ListOfMyThings is a vanilla ruby class
#thing to note for form helpers to work you need
# _attributes=(attributes) method defined.
@oriolgual
oriolgual / Guardfile
Created July 21, 2011 12:18
Example Guardfile with annotate, cucumber, minitest and auto-migrate test database
guard 'annotate', notify: false do
watch( 'db/schema.rb' )
end
guard 'cucumber', cli: '--format progress --no-profile', all_on_start: false do
watch(%r{features/.+\.feature})
watch(%r{features/step_definitions/(.+)_steps\.rb}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] }
end
guard 'minitest', drb: false do
@jeffreyiacono
jeffreyiacono / gist:1114452
Created July 29, 2011 18:41 — forked from dhh/gist:1014971
Use concerns to keep your models manageable (fix unscoped issue w/ trashed scope)
# autoload concerns
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(
#{config.root}/app/controllers/concerns
#{config.root}/app/models/concerns
)
end
end
@caleon
caleon / core_utilities.rb
Created August 29, 2011 12:16
Some of my favorite core extensions for Ruby and Rails used on practically all of my projects these days
# This file meant for globally available, utility methods.
###############################
## Ruby-level Extensions ##
###############################
class Object
def as(&block)
yield self
end
@wtaysom
wtaysom / where_is.rb
Created September 23, 2011 08:57
A little Ruby module for finding the source location where class and methods are defined.
module Where
class <<self
attr_accessor :editor
def is_proc(proc)
source_location(proc)
end
def is_method(klass, method_name)
source_location(klass.method(method_name))