Skip to content

Commit

Permalink
Fix Marshalling for Arrays containing custom resource classes
Browse files Browse the repository at this point in the history
  • Loading branch information
dlitvakb committed Aug 17, 2017
1 parent 03f36ee commit 24312a1
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change Log

## Unreleased
### Fixed
* Fixed Marshalling for custom resources in Arrays. [#138](https://github.com/contentful/contentful.rb/issues/138)

## 2.1.2
### Fixed
Expand Down
6 changes: 4 additions & 2 deletions lib/contentful/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class Array < BaseResource
attr_reader :total, :limit, :skip, :items, :endpoint

def initialize(item = nil,
default_locale = Contentful::Client::DEFAULT_CONFIGURATION[:default_locale],
configuration = {
default_locale: Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]
},
endpoint = '', *)
super(item, { default_locale: default_locale })
super(item, configuration)

@endpoint = endpoint
@total = item.fetch('total', nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/contentful/resource_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def build_array
build_item(item, includes)
end
array_class = fetch_array_class
array_class.new(json.dup.merge('items' => result), default_locale, endpoint)
array_class.new(json.dup.merge('items' => result), @configuration, endpoint)
end

def build_single
Expand Down
7 changes: 5 additions & 2 deletions lib/contentful/sync_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ class SyncPage < BaseResource

attr_reader :sync, :items, :next_sync_url, :next_page_url

def initialize(item, default_locale, *)
super(item, { default_locale: default_locale }, true)
def initialize(item,
configuration = {
default_locale: Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]
}, *)
super(item, configuration, true)

@items = item.fetch('items', [])
@next_sync_url = item.fetch('nextSyncUrl', nil)
Expand Down
24 changes: 24 additions & 0 deletions spec/array_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'spec_helper'

class CustomClass < Contentful::Entry; end

describe Contentful::Array do
let(:client) { create_client }
let(:array) { vcr('array') { client.content_types } }
Expand Down Expand Up @@ -94,5 +96,27 @@
expect(entries.last.image2.url).to eq rehydrated.last.image2.url
}
end

it 'marshals custom resources properly - #138' do
vcr('array/marshal_custom_classes') {
client = create_client(
space: 'j8tb59fszch7',
access_token: '5f711401f965951eb724ac72ac905e13d892294ba209268f13a9b32e896c8694',
dynamic_entries: :auto,
max_include_resolution_depth: 5,
entry_mapping: {
'parent' => CustomClass
}
)

entries = client.entries(content_type: 'parent')

expect(entries.first.inspect).to eq "<CustomClass[parent] id='5aV3O0l5jU0cwQ2OkyYsyU'>"

dehydrated = Marshal.load(Marshal.dump(entries))

expect(dehydrated.first.inspect).to eq "<CustomClass[parent] id='5aV3O0l5jU0cwQ2OkyYsyU'>"
}
end
end
end
159 changes: 159 additions & 0 deletions spec/fixtures/vcr_cassettes/array/marshal_custom_classes.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 24312a1

Please sign in to comment.