Last active
December 17, 2015 05:19
-
-
Save jdewind/5557301 to your computer and use it in GitHub Desktop.
The curious task of supporting multiple devise authenticatables by ensuring they don't have a instance method fight.
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 Devise | |
module Models | |
module AggregateAuthenticatable | |
extend ActiveSupport::Concern | |
STRATEGIES = {:basic => Devise::Models::DatabaseAuthenticatable, :ldap => Devise::Models::LdapAuthenticatable} | |
MODULES = [Devise::Models::DatabaseAuthenticatable, Devise::Models::LdapAuthenticatable] | |
module ClassMethods | |
def strategy_instance_methods | |
MODULES.map(&:instance_methods).flatten | |
end | |
end | |
included do | |
MODULES.each { |mod| include mod } | |
self.strategy_instance_methods.each do |method| | |
define_method method do |*args| | |
dispatch_method method, *args | |
end | |
end | |
end | |
def respond_to?(method, include_all=false) | |
if self.class.strategy_instance_methods.include? method | |
strategy = STRATEGIES[current_strategy] | |
return strategy.instance_methods.include?(method) | |
end | |
super | |
end | |
def dispatch_method(method, *args) | |
strategy = STRATEGIES[current_strategy] | |
strategy.instance_method(method).bind(self)[*args] if respond_to?(method) | |
end | |
def current_strategy | |
... | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment