Last active
December 11, 2015 21:08
-
-
Save afurmanov/4659655 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
module NamedScopeArity | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def scope(name, scope_options = {}) | |
super | |
redefined = false | |
eigenclass = instance_eval do | |
class << self | |
self | |
end | |
end | |
if scope_options.class == Proc | |
redefined = true | |
case scope_options.arity | |
when 0 | |
eigenclass.instance_eval do | |
define_method "#{name}_with_arity_0".to_sym do | |
send("#{name}_without_arity_0".to_sym) | |
end | |
alias_method_chain name, :arity_0 | |
end | |
redefined = true | |
when 1 | |
eigenclass.instance_eval do | |
define_method "#{name}_with_arity_1".to_sym do |arg1| | |
send("#{name}_without_arity_1".to_sym, arg1) | |
end | |
alias_method_chain name, :arity_1 | |
end | |
else | |
redefined = false | |
end | |
end | |
if !redefined | |
eigenclass.instance_eval do | |
define_method("#{name}_with_any_arity".to_sym) do |*args| | |
send("#{name}_without_any_arity".to_sym, *args) | |
end | |
alias_method_chain name, :any_arity | |
end | |
end | |
end | |
end | |
end | |
ActiveRecord::Base.send(:include, NamedScopeArity) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment