Skip to content

Instantly share code, notes, and snippets.

@andersondias
Created February 22, 2010 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersondias/311138 to your computer and use it in GitHub Desktop.
Save andersondias/311138 to your computer and use it in GitHub Desktop.
Basics of scope_by_attribute plugin
module ScopeByAttribute
def self.included(base)
base.send(:extend, ScopeByAttribute::ClassMethods)
end
module ClassMethods
def scope_by_attribute(attribute)
@scoped_attributes ||= []
@scoped_attributes << attribute.to_sym
end
def method_missing(method, *args, &block)
obj = nil
if @scoped_attributes.present?
@scoped_attributes.each do |attribute|
obj = self.first(:conditions => {attribute => method.to_s})
break if obj
end
end
obj || super(method, args, &block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment