Class: ActiveRecord::Associations::Builder::Association
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::Builder::Association
- Defined in:
- lib/active_record/associations/builder/association.rb
Overview
:nodoc:
Direct Known Subclasses
Class Attribute Summary collapse
-
.extensions ⇒ Object
Returns the value of attribute extensions.
-
.valid_options ⇒ Object
TODO: This class accessor is needed to make activerecord-deprecated_finders work.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
- .build(model, name, scope, options, &block) ⇒ Object
- .create_builder(model, name, scope, options, &block) ⇒ Object
-
.define_accessors(model, reflection) ⇒ Object
Defines the setter and getter methods for the association class Post < ActiveRecord::Base has_many :comments end.
- .define_callbacks(model, reflection) ⇒ Object
- .define_readers(mixin, name) ⇒ Object
- .define_validations(model, reflection) ⇒ Object
- .define_writers(mixin, name) ⇒ Object
- .valid_dependent_options ⇒ Object
Instance Method Summary collapse
- #build(model) ⇒ Object
- #define_extensions(model) ⇒ Object
-
#initialize(model, name, scope, options) ⇒ Association
constructor
A new instance of Association.
- #macro ⇒ Object
- #valid_options ⇒ Object
- #validate_options ⇒ Object
Constructor Details
#initialize(model, name, scope, options) ⇒ Association
Returns a new instance of Association.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_record/associations/builder/association.rb', line 50 def initialize(model, name, scope, ) # TODO: Move this to create_builder as soon we drop support to activerecord-deprecated_finders. if scope.is_a?(Hash) = scope scope = nil end # TODO: Remove this model argument as soon we drop support to activerecord-deprecated_finders. @name = name @scope = scope @options = if scope && scope.arity == 0 @scope = proc { instance_exec(&scope) } end end |
Class Attribute Details
.extensions ⇒ Object
Returns the value of attribute extensions.
17 18 19 |
# File 'lib/active_record/associations/builder/association.rb', line 17 def extensions @extensions end |
.valid_options ⇒ Object
TODO: This class accessor is needed to make activerecord-deprecated_finders work. We can move it to a constant in 5.0.
20 21 22 |
# File 'lib/active_record/associations/builder/association.rb', line 20 def @valid_options end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/active_record/associations/builder/association.rb', line 26 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
26 27 28 |
# File 'lib/active_record/associations/builder/association.rb', line 26 def @options end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
26 27 28 |
# File 'lib/active_record/associations/builder/association.rb', line 26 def scope @scope end |
Class Method Details
.build(model, name, scope, options, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/active_record/associations/builder/association.rb', line 28 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 builder = create_builder model, name, scope, , &block reflection = builder.build(model) define_accessors model, reflection define_callbacks model, reflection define_validations model, reflection builder.define_extensions model reflection end |
.create_builder(model, name, scope, options, &block) ⇒ Object
44 45 46 47 48 |
# File 'lib/active_record/associations/builder/association.rb', line 44 def self.create_builder(model, name, scope, , &block) raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol) new(model, name, scope, , &block) end |
.define_accessors(model, reflection) ⇒ Object
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…
105 106 107 108 109 110 |
# File 'lib/active_record/associations/builder/association.rb', line 105 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) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/active_record/associations/builder/association.rb', line 88 def self.define_callbacks(model, reflection) if dependent = reflection.[:dependent] (dependent) add_destroy_callbacks(model, reflection) end Association.extensions.each do |extension| extension.build model, reflection end end |
.define_readers(mixin, name) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/active_record/associations/builder/association.rb', line 112 def self.define_readers(mixin, name) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}(*args) association(:#{name}).reader(*args) end CODE end |
.define_validations(model, reflection) ⇒ Object
128 129 130 |
# File 'lib/active_record/associations/builder/association.rb', line 128 def self.define_validations(model, reflection) # noop end |
.define_writers(mixin, name) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/active_record/associations/builder/association.rb', line 120 def self.define_writers(mixin, name) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}=(value) association(:#{name}).writer(value) end CODE end |
.valid_dependent_options ⇒ Object
132 133 134 |
# File 'lib/active_record/associations/builder/association.rb', line 132 def self. raise NotImplementedError end |
Instance Method Details
#build(model) ⇒ Object
69 70 71 |
# File 'lib/active_record/associations/builder/association.rb', line 69 def build(model) ActiveRecord::Reflection.create(macro, name, scope, , model) end |
#define_extensions(model) ⇒ Object
85 86 |
# File 'lib/active_record/associations/builder/association.rb', line 85 def define_extensions(model) end |
#macro ⇒ Object
73 74 75 |
# File 'lib/active_record/associations/builder/association.rb', line 73 def macro raise NotImplementedError end |
#valid_options ⇒ Object
77 78 79 |
# File 'lib/active_record/associations/builder/association.rb', line 77 def Association. + Association.extensions.flat_map(&:valid_options) end |
#validate_options ⇒ Object
81 82 83 |
# File 'lib/active_record/associations/builder/association.rb', line 81 def .assert_valid_keys() end |