Skip to content

Commit

Permalink
Fix Link resolution on localized content
Browse files Browse the repository at this point in the history
  • Loading branch information
dlitvakb committed Sep 29, 2016
1 parent 2a99ea2 commit 58eb162
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 13 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
* Fix Link resolution on localized content

## 1.0.0

Expand Down
10 changes: 1 addition & 9 deletions lib/contentful/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ def array?
false
end

# Returns true if resource is localized
#
# @return [Boolean]
def localized?(value)
return false unless value.is_a? ::Hash
value.keys.any? { |possible_locale| Contentful::Constants::KNOWN_LOCALES.include?(possible_locale) }
end

# Resources that don't include SystemProperties return nil for #sys
def sys
nil
Expand Down Expand Up @@ -146,7 +138,7 @@ def initialize_fields_for_localized_resource(object)
@fields = {}

object['fields'].each do |field_name, nested_child_object|
if localized?(nested_child_object)
if Support.localized?(nested_child_object)
nested_child_object.each do |object_locale, real_child_object|
@fields[object_locale] ||= {}
@fields[object_locale].merge! extract_from_object(
Expand Down
4 changes: 2 additions & 2 deletions lib/contentful/resource/fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ def inspect(info = nil)
end
end

private

def locales
@fields.keys
end

private

def extract_fields_from_object!(object)
initialize_fields_for_localized_resource(object)
end
Expand Down
25 changes: 23 additions & 2 deletions lib/contentful/resource_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ def add_to_known_resources(res)

def replace_children(res, object)
object.each do |name, potential_objects|
if object['sys']['type'] == 'Entry' &&
name == 'fields' &&
potential_objects.is_a?(::Hash) &&
potential_objects.any? { |_, p| Support.localized?(p) }
localized_objects = potential_objects.select { |_, p| Support.localized?(p) }
localized_objects.each do |field_name, localized_object|
detect_child_objects(localized_object).each do |locale, child_object|
res.public_send(name, locale)[field_name.to_sym] = create_resource(child_object)
end
end
end
detect_child_objects(potential_objects).each do |child_name, child_object|
res.public_send(name)[child_name.to_sym] = create_resource(child_object)
end
Expand Down Expand Up @@ -214,9 +225,19 @@ def create_included_resources!(included_objects)
def replace_links_with_known_resources(res, seen_resource_ids = [])
seen_resource_ids << res.id

property_containers = [:properties, :sys, :fields].map do |property_container_name|
property_containers = [:properties, :sys].map do |property_container_name|
res.public_send(property_container_name)
end.compact
end

if res.is_a?(Entry)
res.locales.each do |locale|
property_containers << res.fields(locale)
end
else
property_containers << res.public_send(:fields)
end

property_containers.compact!

property_containers.each do |property_container|
replace_links_in_properties(property_container, seen_resource_ids)
Expand Down
8 changes: 8 additions & 0 deletions lib/contentful/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def snakify(object)
snake = String(object).gsub(/(?<!^)[A-Z]/) { "_#{$&}" }
snake.downcase
end

# Returns true if resource is localized
#
# @return [Boolean]
def localized?(value)
return false unless value.is_a? ::Hash
value.keys.any? { |possible_locale| Contentful::Constants::KNOWN_LOCALES.include?(possible_locale) }
end
end
end
end
18 changes: 18 additions & 0 deletions spec/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,24 @@
expect(nyancat.fields_with_locales[:name][:es]).to eq("Gato Nyan")
}
end

it 'can have references in multiple locales and they are properly solved' do
vcr('multi_locale_reference') {
client = create_client(
space: '1sjfpsn7l90g',
access_token: 'e451a3cdfced9000220be41ed9c899866e8d52aa430eaf7c35b09df8fc6326f9',
dynamic_entries: :auto
)

entry = client.entries(locale: '*').first

expect(entry.image).to be_a ::Contentful::Asset
expect(entry.fields('zh')[:image]).to be_a ::Contentful::Asset
expect(entry.fields('es')[:image]).to be_a ::Contentful::Asset

expect(entry.image.id).not_to eq entry.fields('zh')[:image].id
}
end
end
end

Expand Down
214 changes: 214 additions & 0 deletions spec/fixtures/vcr_cassettes/multi_locale_reference.yml

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

0 comments on commit 58eb162

Please sign in to comment.