Skip to content

Instantly share code, notes, and snippets.

View matthuhiggins's full-sized avatar

Matthew Higgins matthuhiggins

View GitHub Profile
PROPER_NOUN_INDEX = {
"analysis" => {
"char_filter" => {
"proper_noun_char_filter" => {
"type" => "mapping",
"mappings" => ["'\''=>", "&=>_", "-=>_"]
}
},
"analyzer" => {
"proper_noun" => {
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