This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ever needed to dynamically rewrite the conditions on a has_many association? | |
# This is definitely a hack, but useful if parameterizing or adding scopes are | |
# not practical for whatever reason. | |
class Group < ActiveRecord::Base | |
# Any association will do | |
has_many :people | |
def filter_people(conditions) | |
# All calls to this method after the association is traversed and before a reload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
def self.foo | |
'foo' | |
end | |
private | |
def self.bar | |
'bar' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: http://jkfill.com/2011/05/13/log-which-line-called-redirect_to/ | |
# | |
# Toss this in ApplicationController and all redirects will generate a log statement | |
unless Rails.env.production? | |
def redirect_to(options = {}, response_status = {}) | |
::Rails.logger.error("Redirected by #{caller(1).first rescue "unknown"}") | |
super(options, response_status) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# incorrect | |
describe 'foo' do | |
it 'should bar' do | |
@foo = Factory(:foo) | |
@foo.bar | |
@foo.should_receive :bar # wrong | |
end | |
end | |
# correct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- create_table "book_authors", :force => true do |t| | |
+ create_table "book_authors", :id => false, :force => true do |t| | |
+ t.integer "id", :null => false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assuming you have high-voltage installed | |
# routes.rb | |
match 'long-blitz-string' => 'pages#forty_two' | |
# app/controllers/pages_controller.rb | |
def forty_two | |
'42' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/squelch_asset_pipeline.rb | |
if Rails.env.development? | |
Rails::Rack::Logger.class_eval do | |
def call_with_quiet_assets(env) | |
previous_level = Rails.logger.level | |
Rails.logger.level = Logger::ERROR if env['PATH_INFO'].index("/assets/") == 0 | |
call_without_quiet_assets(env).tap do | |
Rails.logger.level = previous_level | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
def is_user_good_enough(username): | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ApplicationController | |
# Source: http://stackoverflow.com/questions/31320/how-to-profile-a-rails-controller-action | |
around_filter :profile if Rails.env.development? | |
def profile | |
if params[:profile] && result = RubyProf.profile { yield } | |
out = StringIO.new | |
RubyProf::GraphHtmlPrinter.new(result).print out, :min_percent => 0 | |
self.response_body = out.string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# With before filter | |
class FoosController < ApplicationController | |
before_filter :find_foo, :only => [:edit, :show, :update] | |
def create | |
end | |
def edit | |
end |
OlderNewer