Skip to content

Instantly share code, notes, and snippets.

@joeljunstrom
Created February 5, 2010 21:26
Show Gist options
  • Save joeljunstrom/296263 to your computer and use it in GitHub Desktop.
Save joeljunstrom/296263 to your computer and use it in GitHub Desktop.
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