From d0163e99d9affeccb341e12cc7c50b599f129b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 9 Oct 2013 20:56:05 -0300 Subject: [PATCH] Remove builder instances All the job can be done at class level so we can avoid some object allocation --- .../associations/builder/association.rb | 22 +++++-------------- .../builder/has_and_belongs_to_many.rb | 10 ++++----- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 4efd5aa8ba..023afef3b9 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -18,18 +18,15 @@ class << self VALID_OPTIONS = [:class_name, :class, :foreign_key, :validate] - attr_reader :name, :scope, :options - def self.build(model, name, scope, options, &block) extension = define_extensions model, name, &block - builder = create_builder model, name, scope, options, extension - reflection = builder.build(model) + reflection = create_reflection model, name, scope, options, extension define_accessors model, reflection define_callbacks model, reflection reflection end - def self.create_builder(model, name, scope, options, extension = nil) + def self.create_reflection(model, name, scope, options, extension = nil) raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol) if scope.is_a?(Hash) @@ -37,16 +34,11 @@ def self.create_builder(model, name, scope, options, extension = nil) scope = nil end - new(name, scope, options, extension) - end - - def initialize(name, scope, options, extension) - @name = name - @options = options + validate_options(options) - self.class.validate_options(options) + scope = build_scope(scope, extension) - @scope = self.class.build_scope(scope, extension) + ActiveRecord::Reflection.create(macro, name, scope, options, model) end def self.build_scope(scope, extension) @@ -67,10 +59,6 @@ def self.wrap_scope(scope, extension) scope end - def build(model) - ActiveRecord::Reflection.create(self.class.macro, name, scope, options, model) - end - def self.macro raise NotImplementedError end diff --git a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb index af596a3a64..1c9c04b044 100644 --- a/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb +++ b/activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb @@ -84,11 +84,11 @@ def middle_reflection(join_model) middle_name = [lhs_model.name.downcase.pluralize, association_name].join('_').gsub(/::/, '_').to_sym middle_options = middle_options join_model - hm_builder = HasMany.create_builder(lhs_model, - middle_name, - nil, - middle_options) - hm_builder.build lhs_model + + HasMany.create_reflection(lhs_model, + middle_name, + nil, + middle_options) end private -- GitLab