Class: ActiveRecord::Associations::Builder::Association
Do not use. This class is for internal use only.
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | activerecord/lib/active_record/associations/builder/association.rb |
Constant Summary
-
VALID_OPTIONS =
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 21[ :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :query_constraints ].freeze
Class Attribute Summary
- .extensions rw
Class Method Summary
- .build(model, name, scope, options, &block)
- .create_reflection(model, name, scope, options, &block)
- .add_after_commit_jobs_callback(model, dependent) private
- .add_destroy_callbacks(model, reflection) private
- .build_scope(scope) private
- .check_dependent_options(dependent, model) private
-
.define_accessors(model, reflection)
private
Defines the setter and getter methods for the association class Post <
::ActiveRecord::Base
- .define_callbacks(model, reflection) private
- .define_change_tracking_methods(model, reflection) private
- .define_extensions(model, name) private
- .define_readers(mixin, name) private
- .define_validations(model, reflection) private
- .define_writers(mixin, name) private
- .macro private
- .valid_dependent_options private
- .valid_options(options) private
- .validate_options(options) private
Class Attribute Details
.extensions (rw)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 17
attr_accessor :extensions
Class Method Details
.add_after_commit_jobs_callback(model, dependent) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 145
def self.add_after_commit_jobs_callback(model, dependent) if dependent == :destroy_async mixin = model.generated_association_methods unless mixin.method_defined?(:_after_commit_jobs) model.after_commit(-> do _after_commit_jobs.each do |job_class, job_arguments| job_class.perform_later(**job_arguments) end end) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def _after_commit_jobs @_after_commit_jobs ||= [] end CODE end end end
.add_destroy_callbacks(model, reflection) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 140
def self.add_destroy_callbacks(model, reflection) name = reflection.name model.before_destroy(->(o) { o.association(name).handle_dependency }) end
.build(model, name, scope, options, &block)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 25
def self.build(model, name, scope, , &block) if model.dangerous_attribute_method?(name) raise ArgumentError, "You tried to define an association named #{name} on the model #{model.name}, but " \ "this will conflict with a method #{name} already defined by Active Record. " \ "Please choose a different association name." end reflection = create_reflection(model, name, scope, , &block) define_accessors(model, reflection) define_callbacks(model, reflection) define_validations(model, reflection) define_change_tracking_methods(model, reflection) reflection end
.build_scope(scope) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 53
def self.build_scope(scope) if scope && scope.arity == 0 proc { instance_exec(&scope) } else scope end end
.check_dependent_options(dependent, model) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 130
def self. (dependent, model) if dependent == :destroy_async && !model.destroy_association_async_job = "A valid destroy_association_async_job is required to use `dependent: :destroy_async` on associations" raise ActiveRecord::ConfigurationError, end unless .include?(dependent) raise ArgumentError, "The :dependent option must be one of #{}, but is :#{dependent}" end end
.create_reflection(model, name, scope, options, &block)
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 40
def self.create_reflection(model, name, scope, , &block) raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol) ( ) extension = define_extensions(model, name, &block) [:extend] = [* [:extend], extension] if extension scope = build_scope(scope) ActiveRecord::Reflection.create(macro, name, scope, , model) end
.define_accessors(model, reflection) (private)
Defines the setter and getter methods for the association class Post < ::ActiveRecord::Base
has_many :comments
end
Post.first
.comments and Post.first
.comments= methods are defined by this method…
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 95
def self.define_accessors(model, reflection) mixin = model.generated_association_methods name = reflection.name define_readers(mixin, name) define_writers(mixin, name) end
.define_callbacks(model, reflection) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 77
def self.define_callbacks(model, reflection) if dependent = reflection. [:dependent] (dependent, model) add_destroy_callbacks(model, reflection) add_after_commit_jobs_callback(model, dependent) end Association.extensions.each do |extension| extension.build(model, reflection) end end
.define_change_tracking_methods(model, reflection) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 122
def self.define_change_tracking_methods(model, reflection) # noop end
.define_extensions(model, name) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 73
def self.define_extensions(model, name) # noop end
.define_readers(mixin, name) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 102
def self.define_readers(mixin, name) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name} association(:#{name}).reader end CODE end
.define_validations(model, reflection) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 118
def self.define_validations(model, reflection) # noop end
.define_writers(mixin, name) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 110
def self.define_writers(mixin, name) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}=(value) association(:#{name}).writer(value) end CODE end
.macro (private)
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 61
def self.macro raise NotImplementedError end
.valid_dependent_options (private)
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 126
def self. raise NotImplementedError end
.valid_options(options) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 65
def self. ( ) VALID_OPTIONS + Association.extensions.flat_map(&: ) end
.validate_options(options) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/builder/association.rb', line 69
def self. ( ) .assert_valid_keys( ( )) end