Created
February 22, 2010 15:08
-
-
Save andersondias/311138 to your computer and use it in GitHub Desktop.
Basics of scope_by_attribute plugin
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 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