Skip to content

Instantly share code, notes, and snippets.

@tobi
Created June 30, 2010 22:46
Show Gist options
  • Save tobi/459315 to your computer and use it in GitHub Desktop.
Save tobi/459315 to your computer and use it in GitHub Desktop.
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