|
| 1 | +module ActiveModel |
| 2 | + class Serializer |
| 3 | + class Adapter |
| 4 | + class JsonSideload < Json |
| 5 | + |
| 6 | + # the list of what has already been serialized will be kept here to |
| 7 | + # help avoid infinite recursion |
| 8 | + # |
| 9 | + # this should be a list of association_name to a list of objects |
| 10 | + attr_accessor :serialized |
| 11 | + |
| 12 | + # When we are sideloading associations, we can more easily track what |
| 13 | + # has been serialized, so that we avoid infinite |
| 14 | + # recursion / serialization. |
| 15 | + def initialize(*args) |
| 16 | + @serialized = {} |
| 17 | + |
| 18 | + super(*args) |
| 19 | + end |
| 20 | + |
| 21 | + def serializable_hash(options = nil) |
| 22 | + options ||= {} |
| 23 | + |
| 24 | + serialize_hash(options) |
| 25 | + |
| 26 | + singularize_lone_objects |
| 27 | + |
| 28 | + @serialized |
| 29 | + end |
| 30 | + |
| 31 | + |
| 32 | + def serialize_hash(options) |
| 33 | + result = nil |
| 34 | + |
| 35 | + if serializer.respond_to?(:each) |
| 36 | + #TODO: Is this ever hit? |
| 37 | + # binding.pry |
| 38 | + ap "wwwwwwwwwwwwwwwwwwwwww" |
| 39 | + result = serialize_array(serializer, options) |
| 40 | + else |
| 41 | + # skip if we are already serialized |
| 42 | + key_name = serializer.object.class.name.tableize |
| 43 | + existing_of_kind = @serialized[key_name] |
| 44 | + exists = existing_of_kind ? existing_of_kind.select{|a| a[:id] == s.object.id } : false |
| 45 | + return if exists |
| 46 | + |
| 47 | + # we aren't an array! woo! |
| 48 | + |
| 49 | + result = serialized_attributes_of(serializer, options) |
| 50 | + # add to the list of the serialized |
| 51 | + |
| 52 | + |
| 53 | + # now, go over our associations, and add them to the master |
| 54 | + # serialized hash |
| 55 | + serializer.associations.each do |association| |
| 56 | + serializer = association.serializer |
| 57 | + opts = association.options |
| 58 | + |
| 59 | + # make sure the association key exists in the master |
| 60 | + # serialized hash |
| 61 | + @serialized[association.key] ||= [] |
| 62 | + |
| 63 | + if serializer.respond_to?(:each) |
| 64 | + array = serialize_array(serializer, opts) |
| 65 | + association_list = @serialized[association.key] |
| 66 | + |
| 67 | + |
| 68 | + array.each do |item| |
| 69 | + if not association_list.include?(item) |
| 70 | + association_list << item |
| 71 | + end |
| 72 | + end |
| 73 | + @serialized[association.key] = association_list |
| 74 | + |
| 75 | + # add the ids to the result |
| 76 | + result[ids_name_for(association.key)] = array.map{|a| a[:id] } |
| 77 | + else |
| 78 | + hash = ( |
| 79 | + if serializer && serializer.object |
| 80 | + serialized_attributes_of(serializer, options) |
| 81 | + elsif opts[:virtual_value] |
| 82 | + opts[:virtual_value] |
| 83 | + end |
| 84 | + ) |
| 85 | + |
| 86 | + add(association.key, hash) |
| 87 | + |
| 88 | + # add the id to the result |
| 89 | + result[id_name_for(association.key)] = hash[:id] |
| 90 | + end |
| 91 | + |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + @serialized[key_name] ||= [] |
| 96 | + add(key_name, result) |
| 97 | + |
| 98 | + result |
| 99 | + end |
| 100 | + |
| 101 | + def add(key, data) |
| 102 | + unless associations_contain?(data, key) |
| 103 | + if @serialized[key].is_a?(Hash) |
| 104 | + # make array |
| 105 | + value = @serialized[key] |
| 106 | + @serialized[key] = [value, data] |
| 107 | + else |
| 108 | + # already is array |
| 109 | + @serialized[key] << data |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + def serialize_array(serializer, options) |
| 115 | + array = serializer.map { |s| |
| 116 | + js = JsonSideload.new(s) |
| 117 | + serialized = js.serialize_hash(options) |
| 118 | + |
| 119 | + # keep the associations up to date |
| 120 | + append_to_serialized(js.serialized) |
| 121 | + |
| 122 | + serialized |
| 123 | + } |
| 124 | + |
| 125 | + # remove nils |
| 126 | + array.compact |
| 127 | + end |
| 128 | + |
| 129 | + def ids_name_for(name) |
| 130 | + id_name_for(name).to_s.pluralize.to_sym |
| 131 | + end |
| 132 | + |
| 133 | + def id_name_for(name) |
| 134 | + name.to_s.singularize.foreign_key.to_sym |
| 135 | + end |
| 136 | + |
| 137 | + |
| 138 | + def serialized_attributes_of(item, options) |
| 139 | + cache_check(item) do |
| 140 | + item.attributes(options) |
| 141 | + end |
| 142 | + end |
| 143 | + |
| 144 | + def singularize_lone_objects |
| 145 | + temp = {} |
| 146 | + |
| 147 | + @serialized.each do |key, data| |
| 148 | + if data.length > 1 |
| 149 | + temp[key.to_s.pluralize.to_sym] = data |
| 150 | + else |
| 151 | + temp[key.to_s.singularize.to_sym] = data.first |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + @serialized = temp |
| 156 | + end |
| 157 | + |
| 158 | + def append_to_serialized(serialized_objects) |
| 159 | + serialized_objects ||= {} |
| 160 | + |
| 161 | + serialized_objects.each do |association_name, data| |
| 162 | + @serialized[association_name] ||= [] |
| 163 | + |
| 164 | + if data.is_a?(Array) |
| 165 | + data.each do |sub_data| |
| 166 | + append_to_serialized(association_name => sub_data) |
| 167 | + end |
| 168 | + else |
| 169 | + unless associations_contain?(data, association_name) |
| 170 | + add(association_name, data) |
| 171 | + end |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + @serialized |
| 176 | + end |
| 177 | + |
| 178 | + def associations_contain?(item, key) |
| 179 | + return false if @serialized[key].nil? |
| 180 | + |
| 181 | + @serialized[key] == item || @serialized[key].include?(item) |
| 182 | + end |
| 183 | + |
| 184 | + end |
| 185 | + end |
| 186 | + end |
| 187 | +end |
0 commit comments