Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove builder instances

All the job can be done at class level so we can avoid some object
allocation
  • Loading branch information...
commit d0163e99d9affeccb341e12cc7c50b599f129b5c 1 parent c1cd342
@rafaelfranca rafaelfranca authored
View
22 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
View
10 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
Please sign in to comment.
Something went wrong with that request. Please try again.