提交 09d2f168 编写于 作者: J Jon Leighton

stop using class_attribute where methods/inheritance will suffice.

上级 c733b8e9
module ActiveRecord::Associations::Builder
class Association #:nodoc:
class_attribute :valid_options
self.valid_options = [:class_name, :foreign_key, :select, :conditions, :include, :extend, :readonly, :validate, :references]
class << self
attr_accessor :valid_options
end
# Set by subclasses
class_attribute :macro
self.valid_options = [:class_name, :foreign_key, :select, :conditions, :include, :extend, :readonly, :validate, :references]
attr_reader :model, :name, :scope, :options, :reflection
......@@ -29,17 +29,28 @@ def mixin
@model.generated_feature_methods
end
include Module.new { def build; end }
def build
validate_options
reflection = model.create_reflection(self.class.macro, name, scope, options, model)
define_accessors
reflection
@reflection = model.create_reflection(macro, name, scope, options, model)
super # provides an extension point
@reflection
end
def macro
raise NotImplementedError
end
def valid_options
Association.valid_options
end
private
def validate_options
options.assert_valid_keys(self.class.valid_options)
options.assert_valid_keys(valid_options)
end
def define_accessors
......
......@@ -2,9 +2,13 @@
module ActiveRecord::Associations::Builder
class BelongsTo < SingularAssociation #:nodoc:
self.macro = :belongs_to
def macro
:belongs_to
end
self.valid_options += [:foreign_type, :polymorphic, :touch]
def valid_options
super + [:foreign_type, :polymorphic, :touch]
end
def constructable?
!options[:polymorphic]
......
......@@ -2,10 +2,12 @@ module ActiveRecord::Associations::Builder
class CollectionAssociation < Association #:nodoc:
CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
self.valid_options += [
:table_name, :order, :group, :having, :limit, :offset, :uniq, :finder_sql,
:counter_sql, :before_add, :after_add, :before_remove, :after_remove
]
def valid_options
super + [
:table_name, :order, :group, :having, :limit, :offset, :uniq, :finder_sql,
:counter_sql, :before_add, :after_add, :before_remove, :after_remove
]
end
attr_reader :block_extension
......
module ActiveRecord::Associations::Builder
class HasAndBelongsToMany < CollectionAssociation #:nodoc:
self.macro = :has_and_belongs_to_many
def macro
:has_and_belongs_to_many
end
self.valid_options += [:join_table, :association_foreign_key, :delete_sql, :insert_sql]
def valid_options
super + [:join_table, :association_foreign_key, :delete_sql, :insert_sql]
end
def build
reflection = super
......
......@@ -2,9 +2,13 @@
module ActiveRecord::Associations::Builder
class HasMany < CollectionAssociation #:nodoc:
self.macro = :has_many
def macro
:has_many
end
self.valid_options += [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of]
def valid_options
super + [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of]
end
def build
reflection = super
......
......@@ -2,12 +2,15 @@
module ActiveRecord::Associations::Builder
class HasOne < SingularAssociation #:nodoc:
self.macro = :has_one
self.valid_options += [:order, :as]
def macro
:has_one
end
class_attribute :through_options
self.through_options = [:through, :source, :source_type]
def valid_options
valid = super + [:order, :as]
valid += [:through, :source, :source_type] if options[:through]
valid
end
def constructable?
!options[:through]
......@@ -21,12 +24,6 @@ def build
private
def validate_options
valid_options = self.class.valid_options
valid_options += self.class.through_options if options[:through]
options.assert_valid_keys(valid_options)
end
def configure_dependency
if options[:dependent]
unless options[:dependent].in?([:destroy, :delete, :nullify, :restrict])
......
module ActiveRecord::Associations::Builder
class SingularAssociation < Association #:nodoc:
self.valid_options += [:remote, :dependent, :counter_cache, :primary_key, :inverse_of]
def valid_options
super + [:remote, :dependent, :counter_cache, :primary_key, :inverse_of]
end
def constructable?
true
......
......@@ -127,23 +127,17 @@ module ActiveRecord
module AutosaveAssociation
extend ActiveSupport::Concern
ASSOCIATION_TYPES = %w{ HasOne HasMany BelongsTo HasAndBelongsToMany }
module AssociationBuilderExtension #:nodoc:
def self.included(base)
base.valid_options << :autosave
end
def build
reflection = super
model.send(:add_autosave_association_callbacks, reflection)
reflection
super
end
end
included do
ASSOCIATION_TYPES.each do |type|
Associations::Builder.const_get(type).send(:include, AssociationBuilderExtension)
Associations::Builder::Association.class_eval do
self.valid_options << :autosave
include AssociationBuilderExtension
end
end
......
......@@ -20,22 +20,6 @@
require 'models/eye'
class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
def test_autosave_should_be_a_valid_option_for_has_one
assert ActiveRecord::Associations::Builder::HasOne.valid_options.include?(:autosave)
end
def test_autosave_should_be_a_valid_option_for_belongs_to
assert ActiveRecord::Associations::Builder::BelongsTo.valid_options.include?(:autosave)
end
def test_autosave_should_be_a_valid_option_for_has_many
assert ActiveRecord::Associations::Builder::HasMany.valid_options.include?(:autosave)
end
def test_autosave_should_be_a_valid_option_for_has_and_belongs_to_many
assert ActiveRecord::Associations::Builder::HasAndBelongsToMany.valid_options.include?(:autosave)
end
def test_should_not_add_the_same_callbacks_multiple_times_for_has_one
assert_no_difference_when_adding_callbacks_twice_for Pirate, :ship
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册