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_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.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_record/associations/builder/association.rb', line 49 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 |
# 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 builder.define_extensions model reflection end |
.create_builder(model, name, scope, options, &block) ⇒ Object
43 44 45 46 47 |
# File 'lib/active_record/associations/builder/association.rb', line 43 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…
100 101 102 103 104 105 |
# File 'lib/active_record/associations/builder/association.rb', line 100 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
87 88 89 90 91 92 |
# File 'lib/active_record/associations/builder/association.rb', line 87 def self.define_callbacks(model, reflection) add_before_destroy_callbacks(model, reflection) if reflection.[:dependent] Association.extensions.each do |extension| extension.build model, reflection end end |
.define_readers(mixin, name) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/active_record/associations/builder/association.rb', line 107 def self.define_readers(mixin, name) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}(*args) association(:#{name}).reader(*args) end CODE end |
.define_writers(mixin, name) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/active_record/associations/builder/association.rb', line 115 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
123 124 125 |
# File 'lib/active_record/associations/builder/association.rb', line 123 def self. raise NotImplementedError end |
Instance Method Details
#build(model) ⇒ Object
68 69 70 |
# File 'lib/active_record/associations/builder/association.rb', line 68 def build(model) ActiveRecord::Reflection.create(macro, name, scope, , model) end |
#define_extensions(model) ⇒ Object
84 85 |
# File 'lib/active_record/associations/builder/association.rb', line 84 def define_extensions(model) end |
#macro ⇒ Object
72 73 74 |
# File 'lib/active_record/associations/builder/association.rb', line 72 def macro raise NotImplementedError end |
#valid_options ⇒ Object
76 77 78 |
# File 'lib/active_record/associations/builder/association.rb', line 76 def Association. + Association.extensions.flat_map(&:valid_options) end |
#validate_options ⇒ Object
80 81 82 |
# File 'lib/active_record/associations/builder/association.rb', line 80 def .assert_valid_keys() end |