contentful / contentful_model

A lightweight wrapper around the Contentful api gem, to make it behave more like ActiveRecord
MIT License
44 stars 42 forks source link

Marshalling error for key .. can't be referred to #89

Closed Frexuz closed 6 years ago

Frexuz commented 6 years ago

Error

=> CMS::District.cached_post('central')

Cache read: cms/en-GB/districts/central/v1
Cache generate: cms/en-GB/districts/central/v1
Cache write: cms/en-GB/districts/central/v1
Marshalling error for key 'flately:cms/en-GB/districts/central/v1': CMS::District can't be referred to
You are trying to cache a Ruby object which cannot be serialized to memcached.

.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/dalli-2.7.6/lib/dalli/server.rb:414:in `dump'

Code

module CMS

  class District < ContentfulModel::Base

    CACHE_VERSION = :v1
    CONTENT_TYPE = :districts
    self.content_type_id = CONTENT_TYPE.to_s.singularize

    def self.cached_post(slug, locale = I18n.default_locale)
      Rails.cache.fetch([:cms, locale, CONTENT_TYPE, slug, CACHE_VERSION]) do
        find_by(slug: slug).load.first
      end
    end

  end

end

Result

=> #<CMS::District: @fields={:name=>"Central", :slug=>"central", :text=>"The central district consists of Soho, Lan Kwai Fong, mid-levels and the Central Harbour front area. Lan Kwai Fong and Soho are the premier nightlife destinations for many living in Hong Kong. Many expatriates also choose to settle within this area as this is also the big financial hub of Hong Kong with many large banks and financial institutions have set up office. \n\nBeing the financial hub of Hong Kong, you can expect the pricing for rent here to be higher than some of the other locations within Hong Kong. Once again, anything from studio flats closer to Lan Kwai Fong to more family oriented housing options located in the Mid levels area. \n", :backgroundColor=>"c2112e", :textColor=>"000000"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"yWn2tcVSzmcsCWOQ6guWS", :type=>"Entry", :createdAt=>Sat, 18 Nov 2017 06:04:02 +0000, :updatedAt=>Thu, 11 Jan 2018 06:08:01 +0000, :revision=>6, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"district"}>, :locale=>"en-GB"}>

Seems quite strange. This object from contentful looks pretty straight forward to me :)

Gems

contentful (0.12.0)
  http (~> 1.0)
  json (~> 1.8)
  multi_json (~> 1)
contentful-management (1.10.0)
  http (> 1.0, < 3.0)
  json (~> 1.8)
  multi_json (~> 1)
contentful_model (0.2.0)
  activesupport
  contentful (~> 0.9)
  contentful-management (~> 1.0)
  redcarpet
  require_all
Frexuz commented 6 years ago

Quick workaround: format.json { render json: districts.map(&:raw) } but without cache.

with cache: i need to return the #raw data in my cache

      Rails.cache.fetch(some_key) do
        all.load.map(&:raw)
      end

and then i lose the attribute accessors in my rails views, right?

Frexuz commented 6 years ago

.raw also removes all nested DynamicEntry stuff. This is pretty much a show-stopper for me :(

posts[2]
#<CMS::Faq: @fields={:title=>"My listings", :slug=>"my-listings", :questions=>[#<Contentful::DynamicEntry[faq]: @fields={:title=>"Are listing fees renewed automatically?", :answer=>"Flately's listing fees are renewed automatically every 30 days. If the listing expires, than it will be barred from showing up in search results until it is renewed automatically. ", :category=>[#<CMS::Faq: @fields={:title=>"Payments", :slug=>"payments", :questions=>[#<Contentful::DynamicEntry[faq]: @fields={...} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"79AI2UwfZeAsqM4muOc08C", :type=>"Entry", :createdAt=>Tue, 23 Jan 2018 10:35:06 +0000, :updatedAt=>Mon, 05 Feb 2018 04:53:06 +0000, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"faq"}>, :locale=>"en-GB"}>, #<Contentful::DynamicEntry[faq]: @fields={:title=>"How much does it cost to list a property?", :answer=>"Flately charges $100 HKD for each 30 day listing. The listing will expire at the end of 30 days unless it is renewed. \n", :category=>[#<CMS::Faq: @fields={...} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"1Pu4KknEaYwWmmy6IKUKKa", :type=>"Entry", :createdAt=>Fri, 19 Jan 2018 07:53:33 +0000, :updatedAt=>Mon, 05 Feb 2018 05:49:03 +0000, :revision=>3, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"category"}>, :locale=>"en-GB"}>], :slug=>"how-much-does-it-cost-to-list-a-property"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"3yvkkh6eZWM8S8MKOAM6s8", :type=>"Entry", :createdAt=>Mon, 22 Jan 2018 09:03:40 +0000, :updatedAt=>Mon, 05 Feb 2018 05:06:16 +0000, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"faq"}>, :locale=>"en-GB"}>, #<Contentful::DynamicEntry[faq]: @fields={:title=>"Is bulk pricing available?", :answer=>"Bulk pricing will be made available at a later time. for the time being, all listings are set at $100 HKD for every 30 day listing. \n", :category=>[#<CMS::Faq: @fields={...} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"1Pu4KknEaYwWmmy6IKUKKa", :type=>"Entry", :createdAt=>Fri, 19 Jan 2018 07:53:33 +0000, :updatedAt=>Mon, 05 Feb 2018 05:49:03 +0000, :revision=>3, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"category"}>, :locale=>"en-GB"}>], :slug=>"is-bulk-pricing-available"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"4xSdvixaZ2mW0S2GkaKwkW", :type=>"Entry", :createdAt=>Mon, 22 Jan 2018 09:11:19 +0000, :updatedAt=>Mon, 05 Feb 2018 05:18:04 +0000, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"faq"}>, :locale=>"en-GB"}>]} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"1Pu4KknEaYwWmmy6IKUKKa", :type=>"Entry", :createdAt=>Fri, 19 Jan 2018 07:53:33 +0000, :updatedAt=>Mon, 05 Feb 2018 05:49:03 +0000, :revision=>3, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"category"}>, :locale=>"en-GB"}>], :slug=>"are-listing-fees-renewed-automatically"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"79AI2UwfZeAsqM4muOc08C", :type=>"Entry", :createdAt=>Tue, 23 Jan 2018 10:35:06 +0000, :updatedAt=>Mon, 05 Feb 2018 04:53:06 +0000, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"faq"}>, :locale=>"en-GB"}>, #<Contentful::DynamicEntry[faq]: @fields={:title=>"How do I create a new listing?", :answer=>"Hit the create listing button on your profile and fill in the appropriate and required fields! After payment is accepted, your listing will appear", :category=>[#<CMS::Faq: @fields={...} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"62bIQZxm0giCyMw2wwUy6E", :type=>"Entry", :createdAt=>Fri, 19 Jan 2018 07:31:55 +0000, :updatedAt=>Mon, 05 Feb 2018 05:35:13 +0000, :revision=>4, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"category"}>, :locale=>"en-GB"}>], :slug=>"How-do-i-create-a-new-listing"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"1ztaqQSjfisGq0EuYSS4GW", :type=>"Entry", :createdAt=>Thu, 25 Jan 2018 11:19:51 +0000, :updatedAt=>Mon, 05 Feb 2018 04:47:41 +0000, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"faq"}>, :locale=>"en-GB"}>, #<Contentful::DynamicEntry[faq]: @fields={:title=>"How do I find my favourite listings?", :answer=>"Flately offers the ability to favourite a listing by pressing the heart on the listings. You will be able to see your favourited properties under the \"favourites\" tab in the bottom bar. \n\n", :category=>[#<CMS::Faq: @fields={...} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"62bIQZxm0giCyMw2wwUy6E", :type=>"Entry", :createdAt=>Fri, 19 Jan 2018 07:31:55 +0000, :updatedAt=>Mon, 05 Feb 2018 05:35:13 +0000, :revision=>4, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"category"}>, :locale=>"en-GB"}>], :slug=>"how-do-i-find-my-favourite-listings"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"3ugy0BnqcEoggGMWOUaEsm", :type=>"Entry", :createdAt=>Mon, 22 Jan 2018 10:48:15 +0000, :updatedAt=>Mon, 05 Feb 2018 05:05:06 +0000, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"faq"}>, :locale=>"en-GB"}>, #<Contentful::DynamicEntry[faq]: @fields={:title=>"What happens to an expired listing?", :answer=>"When a listing is expired, they are automatically removed from the search results until the listing is renewed. Please consult our listing guide for further details ", :category=>[#<CMS::Faq: @fields={...} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"62bIQZxm0giCyMw2wwUy6E", :type=>"Entry", :createdAt=>Fri, 19 Jan 2018 07:31:55 +0000, :updatedAt=>Mon, 05 Feb 2018 05:35:13 +0000, :revision=>4, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"category"}>, :locale=>"en-GB"}>], :slug=>"what-happens-to-an-expired-listing"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"FOeauhtxeKsE6MSe8AYWC", :type=>"Entry", :createdAt=>Tue, 23 Jan 2018 10:39:54 +0000, :updatedAt=>Mon, 05 Feb 2018 04:52:31 +0000, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"faq"}>, :locale=>"en-GB"}>, #<Contentful::DynamicEntry[faq]: @fields={:title=>"Why did my initial submission of a listing get rejected?", :answer=>"Flately aims to provide the most detailed listings for our users. We believe that having all available information at the hands of the property hunter will lead to a higher closing price on your listing as an agent. \n\nWe will notify you to fill in all required information before a listing goes live. \n\nPlease refer to our listing guideline for more information.\n\n", :category=>[#<CMS::Faq: @fields={...} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"62bIQZxm0giCyMw2wwUy6E", :type=>"Entry", :createdAt=>Fri, 19 Jan 2018 07:31:55 +0000, :updatedAt=>Mon, 05 Feb 2018 05:35:13 +0000, :revision=>4, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"category"}>, :locale=>"en-GB"}>], :slug=>"why-did-my-initial-submission-of-a-listing-get-rejected"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"691OHRWen6KgqsQ86A0yKG", :type=>"Entry", :createdAt=>Mon, 22 Jan 2018 10:52:23 +0000, :updatedAt=>Mon, 05 Feb 2018 05:00:25 +0000, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"faq"}>, :locale=>"en-GB"}>, #<Contentful::DynamicEntry[faq]: @fields={:title=>"Why is so much information required for a listing?", :answer=>"Flately aims to provide the most detailed listings for our users. We believe that having all available information at the hands of the property hunter will lead to a higher closing price on your listing as an agent. \n\nPlease refer to our listing guideline for what is required as part of a Flately listing.\n", :category=>[#<CMS::Faq: @fields={...} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"62bIQZxm0giCyMw2wwUy6E", :type=>"Entry", :createdAt=>Fri, 19 Jan 2018 07:31:55 +0000, :updatedAt=>Mon, 05 Feb 2018 05:35:13 +0000, :revision=>4, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"category"}>, :locale=>"en-GB"}>], :slug=>"why-is-so-much-information-required-for-a-listing"} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"lpWDlCLsJwAgiEWaImEYc", :type=>"Entry", :createdAt=>Mon, 22 Jan 2018 09:20:15 +0000, :updatedAt=>Mon, 05 Feb 2018 05:17:30 +0000, :revision=>2, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"faq"}>, :locale=>"en-GB"}>]} @sys={:space=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"Space", :id=>"92z8ivfvo55n"}>, :id=>"62bIQZxm0giCyMw2wwUy6E", :type=>"Entry", :createdAt=>Fri, 19 Jan 2018 07:31:55 +0000, :updatedAt=>Mon, 05 Feb 2018 05:35:13 +0000, :revision=>4, :contentType=>#<Contentful::Link: @sys={:type=>"Link", :linkType=>"ContentType", :id=>"category"}>, :locale=>"en-GB"}>
posts[2].raw
{"sys"=>{"space"=>{"sys"=>{"type"=>"Link", "linkType"=>"Space", "id"=>"92z8ivfvo55n"}}, "id"=>"62bIQZxm0giCyMw2wwUy6E", "type"=>"Entry", "createdAt"=>"2018-01-19T07:31:55.801Z", "updatedAt"=>"2018-02-05T05:35:13.882Z", "revision"=>4, "contentType"=>{"sys"=>{"type"=>"Link", "linkType"=>"ContentType", "id"=>"category"}}, "locale"=>"en-GB"}, "fields"=>{"title"=>"My listings", "slug"=>"my-listings", "questions"=>[{"sys"=>{"type"=>"Link", "linkType"=>"Entry", "id"=>"79AI2UwfZeAsqM4muOc08C"}}, {"sys"=>{"type"=>"Link", "linkType"=>"Entry", "id"=>"1ztaqQSjfisGq0EuYSS4GW"}}, {"sys"=>{"type"=>"Link", "linkType"=>"Entry", "id"=>"3ugy0BnqcEoggGMWOUaEsm"}}, {"sys"=>{"type"=>"Link", "linkType"=>"Entry", "id"=>"FOeauhtxeKsE6MSe8AYWC"}}, {"sys"=>{"type"=>"Link", "linkType"=>"Entry", "id"=>"691OHRWen6KgqsQ86A0yKG"}}, {"sys"=>{"type"=>"Link", "linkType"=>"Entry", "id"=>"lpWDlCLsJwAgiEWaImEYc"}}]}}
dlitvakb commented 6 years ago

Hey @Frexuz,

The 0.x version of the CDA SDK is not serializable.

Please update to a newer release, 1.x (works with contentful_model 0.2.0) and 2.x (works with current master) are properly serializable.

Cheers

Frexuz commented 6 years ago

Not sure what's going on here :)

When im installing or updating contentful_mode, i get these versions


    contentful (0.12.0)
      http (~> 1.0)
      json (~> 1.8)
      multi_json (~> 1)
    contentful-management (1.10.1)
      http (> 1.0, < 3.0)
      json (~> 1.8)
      multi_json (~> 1)
    contentful_model (0.2.0)
      activesupport
      contentful (~> 0.9)
      contentful-management (~> 1.0)
      redcarpet
      require_all

So I can't update to 1.x because of ~> 0.9

So i tried master, but then I get this error on boot.

/Users/kristiangerardsson/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/require_all-2.0.0/lib/require_all.rb:102:in `rescue in block in require_all': Could not require /Users/kristiangerardsson/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/bundler/gems/contentful_model-1955dd5d4388/lib/contentful_model/base.rb (uninitialized constant ContentfulModel::ChainableQueries). Please require the necessary files (RequireAll::LoadError)
dlitvakb commented 6 years ago

Hi @Frexuz,

Please add gem 'require_all', '~> 1.0' to your Gemfile.

This dependency will be removed in the next release. Which I'm expecting to release within April, as I'm starting active development again in this gem next week.

Cheers

dlitvakb commented 6 years ago

Current master has require_all removed.

Release of 1.0.0 version is scheduled for May 2nd.

Cheers