簡體   English   中英

Blueprinter gem - 渲染 STI 集合

[英]Blueprinter gem - render STI collection

我正在嘗試使用新的序列化替代方案: Blueprinter

在每個 model 都是某種視圖的情況下,您如何處理 STI 集合?

我想出了這樣的事情(在 AMS github 問題上看到了類似的代碼):

def serialize_cards(cards)
  cards.group_by(&:type).map do |type, cards|
    case type
    when 'TypeOneCard' then Api::V1::CardBlueprint.render(cards, view: :one)
    when 'TypeTwoCard' then Api::V1::CardBlueprint.render(cards, view: :two)
    else  raise ArgumentError, "Unhandled event type '#{type}'"
    end
  end
end

為什么 output 在帶有反斜杠的字符串中:

[
    "[{\"id\":27566,\"attachment\":null,\"avatar\":null,\"card_comments_count\":0,\"card_likes_count\":0,\"card_publisher\":\"group\",\"created_at\":\"2020-11-18T10:24:42.195+01:00\",\"description\":\"Ted, how many times have I told you to put the lid back on the peanut butter jar?! It’s this inconsiderate, immature jackassery that makes me feel like I’m living in The Real World House! And not the early days when they all had jobs and social consciences, I’m talking about Hawaii, and after!\",\"ends_at\":\"2021-02-06T23:55:00.000+01:00\",\"event_attendees_count\":0,\"group_name\":\"Cheeseburger - Herbes de Provence\",\"origin\":\"http://localhost:3000/dorfplatz/3\",\"origin_name\":\"Dardagny - Athenaz (Avusy)\",\"picture\":null,\"starts_at\":\"2020-11-21T00:00:00.718+01:00\",\"title\":\"The Doors\",\"type\":\"EventCard\",\"updated_at\":\"2020-11-19T10:25:49.467+01:00\",\"user_full_name\":\"Carolann Ritchie\"},{\"id\":28163,\"attachment\":null,\"avatar\":null,\"card_comments_count\":0,\"card_likes_count\":0,\"card_publisher\":\"group\",\"created_at\":\"2020-11-14T23:22:42.059+01:00\",\"description\":\"That’s life, you know, we never end up where you thought you wanted to be.\",\"ends_at\":\"2020-12-05T23:55:00.000+01:00\",\"event_attendees_count\":0,\"group_name\":\"Cheeseburger - Herbes de Provence\",\"origin\":\"http://localhost:3000/dorfplatz/3\",\"origin_name\":\"Dardagny - Athenaz (Avusy)\",\"picture\":null,\"starts_at\":\"2020-11-14T00:00:00.942+01:00\",\"title\":\"Iron Maiden\",\"type\":\"EventCard\",\"updated_at\":\"2020-11-19T10:25:59.622+01:00\",\"user_full_name\":\"Reuben Kertzmann\"},{\"id\":24658,\"attachment\":null,\"avatar\":null,\"card_comments_count\":0,\"card_likes_count\":0,\"card_publisher\":\"group\",\"created_at\":\"2020-11-14T14:40:45.442+01:00\",\"description\":\"You can’t just skip ahead to where you think your life should be.\",\"ends_at\":\"2021-02-04T23:55:00.000+01:00\",\"event_attendees_count\":0,\"group_name\":\"Cheeseburger - Herbes de Provence\",\"origin\":\"http://localhost:3000/dorfplatz/3\",\"origin_name\":\"Dardagny - Athenaz (Avusy)\",\"picture\":null,\"starts_at\":\"2020-11-19T00:00:00.811+01:00\",\"title\":\"Steely Dan\",\"type\":\"EventCard\",\"updated_at\":\"2020-11-19T10:24:58.452+01:00\",\"user_full_name\":\"Reuben Kertzmann\"}]",

使用render_to_hash而不是將每個序列化器輸出為 JSON

def serialize_cards(cards)
  cards.group_by(&:type).map do |type, cards|
    case type
    when 'TypeOneCard' 
      Api::V1::CardBlueprint.render_to_hash(cards, view: :one)
    when 'TypeTwoCard' 
      Api::V1::CardBlueprint.render_to_hash(cards, view: :two)
    else  
      raise ArgumentError, "Unhandled event type '#{type}'"
    end
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM