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

calling parent on localized entry does not return parent in same locale #145

Open inveterateliterate opened 3 years ago

inveterateliterate commented 3 years ago

Expectation When calling a localized entry through the gem, I expect that traversing the hierarchy of the model will also return entries with localized fields for that locale. That is, when calling child.parent, or child.children in a locale other than my default, I expect both the parent and the children to be returned in that locale.

I hope I'm just missing a parameter or option or configuration or something!

Observation When we call parent records for a localized entry through the gem, the parent's fields are not localized with locale of the originally fetched entry.

It seems that child records are getting returned in the same locale as the parent though, as expected.

For example A content model called Publication, which has a has-many references with a model called Chapter. In our Chapter model class, we have belongs_to_many :publications, and in our Publication model class we have has_many :chapters.

When we fetch a Chapter entry in French for example (chapter = Chapter.locale('fr').load.first), I see the chapter's fields in French.

When I call chapter.publication, the resulting publication's fields are in English. When I inspect the publication's locale, I see it comes back as 'en-US' (my default locale).

I do notice that calling chapter.pages (a child of chapter) provides me with page entries with fields in French.

However I also noticed that calling my original chapter in French back from its publication: chapter.publication.chapters.first yields a chapter instance in the default locale, rather than my original entry, as described here: https://github.com/contentful/contentful_model/blob/master/lib/contentful_model/associations/belongs_to_many.rb#L38

Things I tried Calling child.parent.locale(locale) > does not seem to be an option On the Chapter class tried to pass an option to the belongs_to_many method as described here: https://github.com/contentful/contentful_model/blob/master/lib/contentful_model/associations/belongs_to_many.rb

tried passing an option of locale but got a wrong arguments error. In any case I do not see where other option keys outside the defaults are being considered in the fetch for the parent.