Skip to content

Instantly share code, notes, and snippets.

View matthuhiggins's full-sized avatar

Matthew Higgins matthuhiggins

View GitHub Profile
def named_conditions(name, conditions, &block)
named_scope(name, case conditions
when Hash
{:conditions => conditions}
when Proc
lambda { |*args| {:conditions => conditions.call(*args)} }
end, &block)
end
module StrictlyUntyped
module ConditionsScope
# Makes it easy to add scopes which are only conditions.
#
# For example,
# class Person < ActiveRecord::Base
# named_scope :active, :conditions => {:active => true}
# end
#
# Can be replaced with
class ActiveRecord::Base
named_scope :conditions, lambda { |*args| {:conditions => args} }
end
class Article < ActiveRecord::Base
belongs_to :author
after_create do |article|
if article.title.blank?
article.title = "#{article.author.first_name}'s article about Rails 3"
end
end
end
class Article < ActiveRecord::Base
belongs_to :author
after_create do
if title.blank?
self.title = "#{author.first_name}'s article about Rails 3"
end
end
end
module Awesomeness
def self.include(model_klass)
model_klass.class_eval do
extend ClassMethods
include InstanceMethods
named_scope :awesome, :conditions => {:favorite_language => 'ruby'}
end
end
module ClassMethods
module Awesomeness
extend ActiveSupport::Concern
included do
scope :awesome, where(:favorite_language => 'ruby')
end
module ClassMethods
def make_everyone_awesome
update_all(:favorite_language => 'ruby')
class Post < ActiveRecord::Base
def title=(value)
self[:title] = value.present? ? value : 'Untitled'
end
end
class Post < ActiveRecord::Base
def title=(value)
self[:title] = value.presence || 'Untitled'
end
end
@matthuhiggins
matthuhiggins / asset_host.rb
Created November 9, 2010 05:13
google ajax
google_paths = {
'prototype' => 'http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/',
'controls' => 'http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/',
'dragdrop' => 'http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/',
'effects' => 'http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/'
}
config.action_controller.asset_host = Proc.new do |source, request|
google_asset = google_paths.keys.detect { |asset| source.starts_with?("/javascripts/#{asset}") }
google_asset ? google_paths[google_asset] : "#{request.protocol}#{request.host_with_port}"