From 16c5106cd3a7777664cc40b883d85c6232defd95 Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Mon, 11 May 2015 17:14:24 +0200 Subject: [PATCH] v3: reduce object allocations from polyorphic relations patch --- lib/travis/api/v3/extensions/belongs_to.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/travis/api/v3/extensions/belongs_to.rb b/lib/travis/api/v3/extensions/belongs_to.rb index 94b00052..cafa86e2 100644 --- a/lib/travis/api/v3/extensions/belongs_to.rb +++ b/lib/travis/api/v3/extensions/belongs_to.rb @@ -18,6 +18,13 @@ module Travis::API::V3 # in ActiveRecord::Base#[]=, so we don't break other code by accidentially writing the prefixed version to the # database. module BelongsTo + class BaseClass + attr_reader :name + def initialize(klass) + @name = klass.polymorphic_name + end + end + module ClassMethods def polymorfic_foreign_types @polymorfic_foreign_types ||= [] @@ -28,8 +35,9 @@ module Travis::API::V3 super end - def name - caller_locations.first.base_label == 'add_constraints'.freeze ? polymorphic_name : super + def base_class + return super unless caller_locations.first.base_label == 'add_constraints'.freeze + @base_class ||= BaseClass.new(super) end def polymorphic_name