Sha256: c13e669064aecac23393bbfa1101ca423b72994a98193d486647533cbc02ea24

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'active_model/serializer/adapter/json/fragment_cache'

module ActiveModel
  class Serializer
    class Adapter
      class Json < Adapter
        def serializable_hash(options = {})
          if serializer.respond_to?(:each)
            @result = serializer.map{|s| FlattenJson.new(s).serializable_hash }
          else
            @hash = {}

            @core = cache_check(serializer) do
              serializer.attributes(options)
            end

            serializer.each_association do |name, association, opts|
              if association.respond_to?(:each)
                array_serializer = association
                @hash[name] = array_serializer.map do |item|
                  cache_check(item) do
                    item.attributes(opts)
                  end
                end
              else
                if association && association.object
                  @hash[name] = cache_check(association) do
                    association.attributes(options)
                  end
                elsif opts[:virtual_value]
                  @hash[name] = opts[:virtual_value]
                else
                  @hash[name] = nil
                end
              end
            end
            @result = @core.merge @hash
          end

          { root => @result }
        end

        def fragment_cache(cached_hash, non_cached_hash)
          Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_model_serializers-0.10.0.rc2 lib/active_model/serializer/adapter/json.rb