Created
February 5, 2010 21:26
-
-
Save joeljunstrom/296263 to your computer and use it in GitHub Desktop.
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
ActiveRecord::Base.send :include, Spock::Base::Filterable | |
module Spock | |
module Base | |
module Filterable | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def my_filter(f) | |
f_name, f_arguments = f.to_s.split(':') | |
f_arguments = f_arguments.to_s.split(',') | |
if f_name && my_available_filters.include?(f_name.to_sym) | |
f_arguments.empty? ? send("filter_#{f_name}".to_sym) : self.scopes["filter_#{f_name}".to_sym].call(self, *f_arguments) | |
else | |
self | |
end | |
end | |
def my_available_filters | |
self.scopes.keys.map { |m| m.to_s }.select do |m| | |
m =~ /^filter_/ | |
end.map { |m| m[7..-1].to_sym } | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment