Class: DuckRecord::Associations::Builder::Association
- Inherits:
-
Object
- Object
- DuckRecord::Associations::Builder::Association
- Defined in:
- lib/duck_record/associations/builder/association.rb
Overview
:nodoc:
Direct Known Subclasses
Constant Summary collapse
- VALID_OPTIONS =
:nodoc:
[:class_name, :anonymous_class, :foreign_key, :validate]
Class Attribute Summary collapse
-
.extensions ⇒ Object
Returns the value of attribute extensions.
Class Method Summary collapse
- .build(model, name, scope, options, &block) ⇒ Object
- .build_scope(scope, extension) ⇒ Object
- .create_reflection(model, name, scope, options, extension = nil) ⇒ 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_extensions(model, name) ⇒ Object
- .define_readers(mixin, name) ⇒ Object
- .define_validations(model, reflection) ⇒ Object
- .define_writers(mixin, name) ⇒ Object
- .macro ⇒ Object
- .valid_options(options) ⇒ Object
- .validate_options(options) ⇒ Object
- .wrap_scope(scope, _extension) ⇒ Object
Class Attribute Details
.extensions ⇒ Object
Returns the value of attribute extensions.
15 16 17 |
# File 'lib/duck_record/associations/builder/association.rb', line 15 def extensions @extensions end |
Class Method Details
.build(model, name, scope, options, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/duck_record/associations/builder/association.rb', line 21 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 extension = define_extensions model, name, &block reflection = create_reflection model, name, scope, , extension define_accessors model, reflection define_callbacks model, reflection define_validations model, reflection reflection end |
.build_scope(scope, extension) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/duck_record/associations/builder/association.rb', line 51 def self.build_scope(scope, extension) new_scope = scope if scope && scope.arity == 0 new_scope = proc { instance_exec(&scope) } end if extension new_scope = wrap_scope new_scope, extension end new_scope end |
.create_reflection(model, name, scope, options, extension = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/duck_record/associations/builder/association.rb', line 36 def self.create_reflection(model, name, scope, , extension = nil) raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol) if scope.is_a?(Hash) = scope scope = nil end () scope = build_scope(scope, extension) DuckRecord::Reflection.create(macro, name, scope, , model) 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…
96 97 98 99 100 101 |
# File 'lib/duck_record/associations/builder/association.rb', line 96 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
84 85 86 87 88 |
# File 'lib/duck_record/associations/builder/association.rb', line 84 def self.define_callbacks(model, reflection) Association.extensions.each do |extension| extension.build model, reflection end end |
.define_extensions(model, name) ⇒ Object
81 82 |
# File 'lib/duck_record/associations/builder/association.rb', line 81 def self.define_extensions(model, name) end |
.define_readers(mixin, name) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/duck_record/associations/builder/association.rb', line 103 def self.define_readers(mixin, name) mixin.class_eval " def \#{name}(*args)\n association(:\#{name}).reader(*args)\n end\n CODE\nend\n", __FILE__, __LINE__ + 1 |
.define_validations(model, reflection) ⇒ Object
123 124 125 |
# File 'lib/duck_record/associations/builder/association.rb', line 123 def self.define_validations(model, reflection) # noop end |
.define_writers(mixin, name) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/duck_record/associations/builder/association.rb', line 111 def self.define_writers(mixin, name) mixin.class_eval " def \#{name}=(value)\n if self.class.readonly_attributes.include?(\"\#{name}\") && attr_readonly_enabled?\n return\n end\n\n association(:\#{name}).writer(value)\n end\n CODE\nend\n", __FILE__, __LINE__ + 1 |
.macro ⇒ Object
69 70 71 |
# File 'lib/duck_record/associations/builder/association.rb', line 69 def self.macro raise NotImplementedError end |
.valid_options(options) ⇒ Object
73 74 75 |
# File 'lib/duck_record/associations/builder/association.rb', line 73 def self.() VALID_OPTIONS + Association.extensions.flat_map(&:valid_options) end |
.validate_options(options) ⇒ Object
77 78 79 |
# File 'lib/duck_record/associations/builder/association.rb', line 77 def self.() .assert_valid_keys(()) end |
.wrap_scope(scope, _extension) ⇒ Object
65 66 67 |
# File 'lib/duck_record/associations/builder/association.rb', line 65 def self.wrap_scope(scope, _extension) scope end |