1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

stop using class_attribute where methods/inheritance will suffice.

This commit is contained in:
Jon Leighton 2012-07-13 18:43:55 +01:00
parent c733b8e985
commit 09d2f168e6
9 changed files with 57 additions and 55 deletions

View file

@ -1,10 +1,10 @@
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 @@ module ActiveRecord::Associations::Builder
@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

View file

@ -2,9 +2,13 @@ require 'active_support/core_ext/object/inclusion'
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]

View file

@ -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

View file

@ -1,8 +1,12 @@
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

View file

@ -2,9 +2,13 @@ require 'active_support/core_ext/object/inclusion'
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

View file

@ -2,12 +2,15 @@ require 'active_support/core_ext/object/inclusion'
module ActiveRecord::Associations::Builder
class HasOne < SingularAssociation #:nodoc:
self.macro = :has_one
def macro
:has_one
end
self.valid_options += [:order, :as]
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 @@ module ActiveRecord::Associations::Builder
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])

View file

@ -1,6 +1,8 @@
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

View file

@ -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

View file

@ -20,22 +20,6 @@ require 'models/company'
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