Class: ActiveRecord::Associations::Builder::Association
- Defined in:
- activerecord/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
Returns the value of attribute valid_options.
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#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
Instance Method Summary collapse
- #build ⇒ Object
- #configure_dependency ⇒ Object
-
#define_accessors ⇒ Object
Defines the setter and getter methods for the association class Post < ActiveRecord::Base has_many :comments end.
- #define_readers ⇒ Object
- #define_writers ⇒ Object
-
#initialize(model, name, scope, options) ⇒ Association
constructor
A new instance of Association.
- #macro ⇒ Object
- #mixin ⇒ Object
- #valid_dependent_options ⇒ Object
- #valid_options ⇒ Object
- #validate_options ⇒ Object
Constructor Details
#initialize(model, name, scope, options) ⇒ Association
Returns a new instance of Association.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 29 def initialize(model, name, scope, ) raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol) @model = model @name = name if scope.is_a?(Hash) @scope = nil @options = scope else @scope = scope @options = end if @scope && @scope.arity == 0 prev_scope = @scope @scope = proc { instance_exec(&prev_scope) } end end |
Class Attribute Details
.extensions ⇒ Object
Returns the value of attribute extensions
17 18 19 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 17 def extensions @extensions end |
.valid_options ⇒ Object
Returns the value of attribute valid_options
16 17 18 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 16 def @valid_options end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model
23 24 25 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 23 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name
23 24 25 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 23 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options
23 24 25 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 23 def @options end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope
23 24 25 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 23 def scope @scope end |
Class Method Details
.build(*args, &block) ⇒ Object
25 26 27 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 25 def self.build(*args, &block) new(*args, &block).build end |
Instance Method Details
#build ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 53 def build define_accessors configure_dependency if [:dependent] reflection = ActiveRecord::Reflection.create(macro, name, scope, , model) Association.extensions.each do |extension| extension.build @model, reflection end reflection end |
#configure_dependency ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 104 def configure_dependency unless .include? [:dependent] raise ArgumentError, "The :dependent option must be one of #{}, but is :#{[:dependent]}" end n = name model.before_destroy lambda { |o| o.association(n).handle_dependency } end |
#define_accessors ⇒ 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…
83 84 85 86 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 83 def define_accessors define_readers define_writers end |
#define_readers ⇒ Object
88 89 90 91 92 93 94 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 88 def define_readers mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}(*args) association(:#{name}).reader(*args) end CODE end |
#define_writers ⇒ Object
96 97 98 99 100 101 102 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 96 def define_writers mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}=(value) association(:#{name}).writer(value) end CODE end |
#macro ⇒ Object
64 65 66 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 64 def macro raise NotImplementedError end |
#mixin ⇒ Object
49 50 51 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 49 def mixin @model.generated_feature_methods end |
#valid_dependent_options ⇒ Object
113 114 115 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 113 def raise NotImplementedError end |
#valid_options ⇒ Object
68 69 70 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 68 def Association. + Association.extensions.flat_map(&:valid_options) end |
#validate_options ⇒ Object
72 73 74 |
# File 'activerecord/lib/active_record/associations/builder/association.rb', line 72 def .assert_valid_keys() end |