Module: ActiveRecord::VirtualAttributes::VirtualFields::ClassMethods
- Defined in:
- lib/active_record/virtual_attributes/virtual_fields.rb
Instance Method Summary collapse
- #include_to_hash(value) ⇒ Hash
- #merge_includes(hash1, hash2) ⇒ Object
- #replace_virtual_field_hash(associations) ⇒ Object
- #replace_virtual_fields(associations) ⇒ Object
- #simplify_includes(ret) ⇒ Object
- #virtual_field?(name) ⇒ Boolean
- #virtual_fields_base? ⇒ Boolean
Instance Method Details
#include_to_hash(value) ⇒ Hash
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/active_record/virtual_attributes/virtual_fields.rb', line 71 def include_to_hash(value) case value when String, Symbol {value.to_sym => {}} when Array value.flatten.each_with_object({}) do |k, h| merge_includes(h, k) end when nil {} else value end end |
#merge_includes(hash1, hash2) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/active_record/virtual_attributes/virtual_fields.rb', line 88 def merge_includes(hash1, hash2) return hash1 if hash2.blank? # very common case. # optimization to skip deep_merge and hash creation if hash2.kind_of?(Symbol) hash1[hash2] ||= {} return hash1 end hash1.deep_merge!(include_to_hash(hash2)) do |_k, v1, v2| # this block is conflict resolution when a key has 2 values merge_includes(include_to_hash(v1), v2) end end |
#replace_virtual_field_hash(associations) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_record/virtual_attributes/virtual_fields.rb', line 52 def replace_virtual_field_hash(associations) associations.each_with_object({}) do |(parent, child), h| if virtual_field?(parent) # form virtual_attribute => {} merge_includes(h, replace_virtual_fields(virtual_includes(parent))) else reflection = reflect_on_association(parent.to_sym) if reflection.nil? merge_includes(h, parent) elsif reflection.[:polymorphic] merge_includes(h, parent => child) else merge_includes(h, parent => reflection.klass.replace_virtual_fields(child) || {}) end end end end |
#replace_virtual_fields(associations) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_record/virtual_attributes/virtual_fields.rb', line 35 def replace_virtual_fields(associations) return nil if associations.blank? ret = case associations when String, Symbol virtual_field?(associations) ? replace_virtual_fields(virtual_includes(associations)) : associations.to_sym when Array associations.filter_map { |association| replace_virtual_fields(association) } when Hash replace_virtual_field_hash(associations) else associations end simplify_includes(ret) end |
#simplify_includes(ret) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/active_record/virtual_attributes/virtual_fields.rb', line 105 def simplify_includes(ret) case ret when Hash ret.size <= 1 && ret.values.first.blank? ? ret.keys.first : ret when Array ret.size <= 1 ? ret.first : ret else ret end end |
#virtual_field?(name) ⇒ Boolean
31 32 33 |
# File 'lib/active_record/virtual_attributes/virtual_fields.rb', line 31 def virtual_field?(name) virtual_attribute?(name) || virtual_reflection?(name) end |
#virtual_fields_base? ⇒ Boolean
27 28 29 |
# File 'lib/active_record/virtual_attributes/virtual_fields.rb', line 27 def virtual_fields_base? !(superclass < VirtualFields) end |