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
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 |
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
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 |
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 ActiveRecord::Base | |
named_scope :conditions, lambda { |*args| {:conditions => args} } | |
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
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 |
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 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 |
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
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 |
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
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') |
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 Post < ActiveRecord::Base | |
def title=(value) | |
self[:title] = value.present? ? value : 'Untitled' | |
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
class Post < ActiveRecord::Base | |
def title=(value) | |
self[:title] = value.presence || 'Untitled' | |
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
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}" |
OlderNewer