Created
June 30, 2010 22:46
-
-
Save tobi/459315 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
def self.accelerated_belongs_to(record) | |
klass = record.to_s.camelize.constantize | |
if !klass.respond_to?(:current) | |
raise ArgumentError, "#{record} does not implement the current class method that is needed for accelerated_belongs_to." | |
end | |
self.belongs_to record | |
alias_method :"#{record}_without_acceleration", record | |
define_method(record) do |*params| | |
(klass.current && send(:"#{record}_id") == klass.current.id) ? klass.current : send(:"#{record}_without_acceleration", *params) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment