Module: ActiveRecord::VirtualAttributes::VirtualReflections::ClassMethods
- Defined in:
- lib/active_record/virtual_attributes/virtual_reflections.rb
Instance Method Summary collapse
-
#collect_reflections(association_names) ⇒ Object
invalid associations return a nil real reflections are followed a virtual association will stop the traversal.
- #collect_reflections_with_virtual(association_names) ⇒ Object
- #follow_associations(association_names) ⇒ Object
- #follow_associations_with_virtual(association_names) ⇒ Object
- #reflection_with_virtual(association) ⇒ Object
- #reflections_with_virtual ⇒ Object
- #virtual_belongs_to(name, options = {}) ⇒ Object
- #virtual_has_many(name, options = {}) ⇒ Object
-
#virtual_has_one(name, options = {}) ⇒ Object
Definition.
- #virtual_reflection(name) ⇒ Object
- #virtual_reflection?(name) ⇒ Boolean
-
#virtual_reflections ⇒ Object
Introspection.
Instance Method Details
#collect_reflections(association_names) ⇒ Object
invalid associations return a nil real reflections are followed a virtual association will stop the traversal
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 71 def collect_reflections(association_names) klass = self association_names.each_with_object([]) do |name, ret| reflection = klass.reflect_on_association(name) if reflection.nil? if klass.reflection_with_virtual(name) break(ret) else break end end klass = reflection.klass ret << reflection end end |
#collect_reflections_with_virtual(association_names) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 87 def collect_reflections_with_virtual(association_names) klass = self association_names.collect do |name| reflection = klass.reflection_with_virtual(name) || break klass = reflection.klass reflection end end |
#follow_associations(association_names) ⇒ Object
59 60 61 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 59 def follow_associations(association_names) association_names.inject(self) { |klass, name| klass&.reflect_on_association(name)&.klass } end |
#follow_associations_with_virtual(association_names) ⇒ Object
63 64 65 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 63 def follow_associations_with_virtual(association_names) association_names.inject(self) { |klass, name| klass&.reflection_with_virtual(name)&.klass } end |
#reflection_with_virtual(association) ⇒ Object
55 56 57 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 55 def reflection_with_virtual(association) virtual_reflection(association) || reflect_on_association(association) end |
#reflections_with_virtual ⇒ Object
51 52 53 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 51 def reflections_with_virtual reflections.symbolize_keys.merge(virtual_reflections) end |
#virtual_belongs_to(name, options = {}) ⇒ Object
29 30 31 32 33 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 29 def virtual_belongs_to(name, = {}) uses = .delete(:uses) reflection = ActiveRecord::Associations::Builder::BelongsTo.build(self, name, nil, ) add_virtual_reflection(reflection, name, uses, ) end |
#virtual_has_many(name, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 19 def virtual_has_many(name, = {}) define_method(:"#{name.to_s.singularize}_ids") do records = send(name) records.respond_to?(:ids) ? records.ids : records.collect(&:id) end uses = .delete(:uses) reflection = ActiveRecord::Associations::Builder::HasMany.build(self, name, nil, ) add_virtual_reflection(reflection, name, uses, ) end |
#virtual_has_one(name, options = {}) ⇒ Object
Definition
13 14 15 16 17 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 13 def virtual_has_one(name, = {}) uses = .delete(:uses) reflection = ActiveRecord::Associations::Builder::HasOne.build(self, name, nil, ) add_virtual_reflection(reflection, name, uses, ) end |
#virtual_reflection(name) ⇒ Object
39 40 41 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 39 def virtual_reflection(name) virtual_reflections[name.to_sym] end |
#virtual_reflection?(name) ⇒ Boolean
35 36 37 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 35 def virtual_reflection?(name) virtual_reflections.key?(name.to_sym) end |
#virtual_reflections ⇒ Object
Introspection
47 48 49 |
# File 'lib/active_record/virtual_attributes/virtual_reflections.rb', line 47 def virtual_reflections (virtual_fields_base? ? {} : superclass.virtual_reflections).merge(_virtual_reflections) end |