令人困惑的ActiveModelSerializer行为。发生了什么,为什么不正确序列化?

时间:2016-03-21 21:12:03

标签: active-model-serializers

我的序列化程序只是访问ActiveModel对象中的一个键,在序列化程序中,它似乎返回嵌套的哈希。这是我的序列化器:

module Api
  module V20150315
    class RecipeToolSerializer < ActiveModel::Serializer
      cached
      delegate :cache_key, to: :object

      attributes :id,
                 :display_name,
                 :images,
                 :display_price,
                 :description,
                 :main_imagez,
                 :subtitle

      def display_name
        object.display_name
      end

      def images
        object.story.get_spree_product.master.images
      end

      def display_price
        object.story.get_spree_product.master.display_price
      end

      def description
        object.story.description
      end

      def main_imagez
        binding.pry
        object.story.main_image
      end

      def subtitle
        object.story.get_spree_product.subtitle
      end
    end
  end
end

有问题的方法是main_imagez方法。当我点击那个pry时,object.story.main_image返回:

pry(#<Api::V20150315::RecipeToolSerializer>)> object.story.main_image
{
                  :key => "/spec/support/uploads/story/main_image/1/20160321-1706-2988-3566/someimage",
           :bucket_url => "https://s3.amazonaws.com/test_bucket",
               :policy => "somethingsomething",
            :signature => "someencryptedcode",
                  :acl => "public-read",
    :aws_access_key_id => "somekey",
          :process_url => nil
}

但是当我检查响应时,在我的测试中......发生了陌生感:

这是我的测试:

it "should have a main image attribute for each recipe tool" do
        response = JSON.parse(subject.body)
        binding.pry
        response['recipe_tools'].each do |recipe_tool_response|
          binding.pry
          expect(recipe_tool_response["main_image"]).to match /(.jpg|.jpeg|.gif|.png)/
        end
      end

和绑定:

[3] pry(#<RSpec::ExampleGroups::ApiRecipesRecipeToolsController::GETIndex::WhenGivenRecipeParams>)> JSON.parse(subject.body)
{
    "recipe_tools" => [
        [0] {
                       "id" => 1,
             "display_name" => "Testing Tool",
                   ...
              "main_imagez" => {
                "main_image" => {
                       "url" => "//d2v868ucs1c5n9.cloudfront.net/spec/support/uploads/story/main_image/1/20160321-1706-2988-3566/blueapronimage.jpg",
                    "medium" => {
                        "url" => "//someurl/spec/support/uploads/story/main_image/1/20160321-1706-2988-3566/some_image.jpg"
                    },
                     "thumb" => {
                        "url" => "//someurl/spec/support/uploads/story/main_image/1/20160321-1706-2988-3566/some_image_thumb.jpg"
                    }
                }
            },
                 "subtitle" => ""
        },

任何想法为什么main_imagez失去了它的结构并成为这个奇怪的嵌套哈希?

我认为这是最重要的部分:

我在main_image方法的载波上传器中有一个名为to_hash的方法:

def to_hash
    {
      key: key,
      bucket_url: bucket_url,
      policy: policy,
      signature: signature,
      acl: acl,
      aws_access_key_id: aws_access_key_id,
      process_url: process_url
    }
  end

基本上,我认为在应用序列化程序的方法(包括to_hash)之后会调用一个方法来更改main_image对象,该对象看起来像一个哈希但实际上并不是一个哈希... main_image实际上返回一个main_image_uploader对象。为了将其转换为哈希,我打赌应用了to_hash方法但不幸的是我们修补了该方法。我如何找出所谓的内容以及猴子修补to_hash是否危险?

0 个答案:

没有答案