Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Make localize_entry localize nested entries and maintain hash base class. #131

Merged
merged 9 commits into from
Aug 17, 2017
Merged

Make localize_entry localize nested entries and maintain hash base class. #131

merged 9 commits into from
Aug 17, 2017

Conversation

kylorhall
Copy link
Contributor

@kylorhall kylorhall commented Aug 17, 2017

Aims to fix my issue #130. Seems to work fine for my use case.

Ruby isn't really my language, so something like localized_entry = entry.class.new (to create a new instance of the same type of the entry) may be a horrible idea..

Kylor Hall added 5 commits August 17, 2017 10:49

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…alue.
Entry, if coming from the contentful datastore, should be Thor::CoreExt::HashWithIndifferentAccess which has access via both `entry[:symbol]` and `entry['string']`.
Not sure what nested hashes would be coming from contentful – are there 1:1 relationships?
Not sure what Ruby's typical syntax is for this, didn't fail for me though.
Copy link
Contributor

@dlitvakb dlitvakb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great addition.

Thanks for the contribution, please make the small changes I requested above, and let's get it approved.

cheers

return value.fetch(locale) if value.key? locale
return value.fetch(fallback_locale) if value.key? fallback_locale
end
value = value.fetch(locale) if value.respond_to?(:fetch) && value.respond_to?(:key) && value.key?(locale)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key checks are unnecessary and should be key? if they were.

The call to localize_array and localize_entry are totally spot-on. Hadn't tested nested localized items, and it's a great addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to value.respond_to?(:key?).

The issue I was trying to avoid with this is if value is passed an Array (shouldn't happen aside from user error?) or value becomes an Array via value = value.fetch(locale), Arrays do have fetch on them, but not key?, so it would throw a NoMethodError if it matched on the locale key.

Not sure which syntax is more ruby-like – I'd write this differently in Javascript:

  if value.respond_to?(:fetch) && value.respond_to?(:key?)
    if value.key?(locale)
      value = value.fetch(locale)
    elsif value.key?(fallback_locale)
      value = value.fetch(fallback_locale)
    end
  end

or

  value = value.fetch(locale) if value.respond_to?(:fetch) && value.respond_to?(:key?) && value.key?(locale)
  value = value.fetch(fallback_locale) if value.respond_to?(:fetch) && value.respond_to?(:key?) && value.key?(fallback_locale)

I re-committed the more succinct syntax.

@@ -7,7 +7,8 @@ def contentful_instances
end

def localize_entry(entry, locale, fallback_locale='en-US')
localized_entry = {}
localized_entry = entry.class.new
localized_entry = {} unless localized_entry.is_a? ::Hash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be changed to

localized_entry = ::Thor::CoreExt::HashWithIndifferentAccess.new

As it is a superclass of ::Hash, there should be absolutely no difference with the current implementation.

Just make sure to add the explicit require at the top of the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, wasn't sure on that.

Did you want Thor in the gemfile, or happy with it being a very integral part of middleman-core? If Middleman dropped Thor, plenty of this might break anyways..

Kylor Hall added 4 commits August 17, 2017 14:03
We're using the `key?`, not the `key` method following this.
…ia symbol.

As a result of the changes from adding Thor, we want to make sure symbols still work.
@kylorhall
Copy link
Contributor Author

kylorhall commented Aug 17, 2017

Forgot to re-run tests after review changes. Fixed test and added a test to ensure both symbols and strings work as accessors on localize_entry.

@dlitvakb
Copy link
Contributor

Looks good.

Merging, thanks for the contribution.

@dlitvakb dlitvakb merged commit 9679216 into contentful:master Aug 17, 2017
@kylorhall kylorhall deleted the allow-localize_entries-to-localize-nested-entries branch August 17, 2017 03:03
dlitvakb pushed a commit that referenced this pull request Aug 17, 2017
…ass. (#131)

* Continue to localize further arrays and hashes after localizing the value.

* Adds spec for nested_field.

* Use the same class as the entry, with a fallback to hash.

Entry, if coming from the contentful datastore, should be Thor::CoreExt::HashWithIndifferentAccess which has access via both `entry[:symbol]` and `entry['string']`.

* Adds a test for a nested hash as well.

Not sure what nested hashes would be coming from contentful – are there 1:1 relationships?

* Cleanup trailing commas.

Not sure what Ruby's typical syntax is for this, didn't fail for me though.

* Change `respond_to?(:key)` to `respond_to?(:key?)`

We're using the `key?`, not the `key` method following this.

* Use Thor's hash directly here.

* Thor seems to have changed this comparison from symbols to strings keys.

* Adds tests to validate that the localized_entry is still accessible via symbol.

As a result of the changes from adding Thor, we want to make sure symbols still work.
@dlitvakb
Copy link
Contributor

This has been released for version 2.1.2 of the integration.

Cheers

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants