-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CustomResource module and Fix Marshalling #81
Conversation
super | ||
|
||
unless client.nil? | ||
if client.dynamic_entry_cache.key?(content_type['sys']['id'].to_sym) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we warn/fail if client == nil
or the content type isn't cached, yet? Might be better than silently not having any fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@neonichu The entry will still have fields, just not dynamically assigned.
And can be read via entry.fields[:field_name]
Also, the only case when client == nil
is when you create the entry manually like MyCustomEntry.new
, in which case you can provide a hash as a parameter with the values for the fields, which will be available even if the CT is not cached.
Only possible exception is: Client exists, CT not cached, that case can happen under 2 conditions:
- Client created before CT was and no client reload happened
- More than 1000 CTs in the Space and entry was requested by ID and is of one of the CTs left out
Second case for content types can't happen because there's a limit of 50 Considering we think both cases are impossible, making it straight up throw seems better? |
I don't think raising an exception is a good idea, as user might not have If that's the case, then having to operate over |
I would suggest documenting this behaviour then. |
890ec98
to
e522b76
Compare
e522b76
to
9f9f77e
Compare
The CT cache behaviour is now removed and only relies on defined Custom Resource schema |
So, I tried it out. The property-thing seems to work now:
Where:
The sad thing is that properties have no setters (e.g.
Any chance that you guys would add that "officially" or that I might monkey-patch it in? I tried something like this:
in |
Sadly the
Perhaps there is still something lingering in Thanks for your support. Would be really cool if we get this to work. |
Hey @toadle, Thanks for jumping in and looking into this. Regarding Regarding the setter methods, as the Delivery API is read-only, it's not contemplated to use setters externally. I can talk with our product manager and decide if it's a good idea to add that. You can either overwrite manually the Hope you find this answer useful, And if you can, please let me know if there is anything that you would find important to mention in the Readme or the Class Documentation regarding these use cases that might be missing. |
@dlitvakb I'll figure something out for that setter-methods stuff, thanks! Regarding the marshalling. In my case, it is exactly like you said: I have a custom So
I saw that you guys recently moved to defining an
Don't know if that is important, but all my classes are correctly mapped to something that is a So I'd still say that there is a BTW: Just a thought about that mashalling. If I understand the implementation correctly, then this could potentially lead the a huge load of unwilling API-requests which don't result in the same outcome, when Let's say I requested Is it too difficult to strip out all |
Hey @toadle, I'd have to test how this works with As for Raw calls not hydrating the amount of includes... I should probably add a way of telling the object how many includes to marshal. Any hydration should retrieve the object as expected (though currently, minus the includes). Thanks for pointing me into a better direction for improving this library 👍 |
Hey @toadle, I've been working on improving marshalling, and added handling for included objects and added specs for both ways of resource mapping + marshalling. Please let me know if this solved your problem PS: Here is a little script that checks for both of this methods (this is what the specs added were based from): https://gist.github.com/dlitvakb/ca5ad8737f1d4bc5f90e |
Hey @dlitvakb, cool stuff. Sorry for taking some time to test this, but I had to finish a story here ;-)! |
@dlitvakb So first I checked the marshalling with my example. Good news: My scenario is this: I load a small category-tree (categories have subcategories), for being able to look up the breadcrumb for certain articles. Therefore I load the categories with
I put those into the
Works the first time around. Second time not so much. The
So I suppose the |
Hey @toadle Thanks for the quick answer and the interest in this PR. Would you be willing to contribute a fix? Your case is more complex than I expected and I need more information to continue moving on with this. Can you provide me with the following:
You can fetch the data required in JSON format using contentful-bootstrap.rb with the following command: $ contentful_bootstrap generate_json $SPACE_ID $ACCESS_TOKEN --output-file dump.json |
Hey @dlitvakb, sure I'd like to provide a fix. So far I haven't totally understood how the parsing-process works for the returned JSON. Therefore I haven't contributed. From what I understood you are using basic jsonapi.org -style JSONs, but the GEM already hands out neatly build object-trees which were already looked up in the links. I'll look into it closer and see if I can provide code myself. As for the infos you requested. JSON for the Content-Type JSON for one entry Since I request my entries with (I dumped this by hacking I think it is pretty straight forward. Should be the same for every content-type that uses references/links. |
Hey @toadle, Thanks for the follow up, I'll be trying to reproduce this scenario and get back to you as soon as possible. In the meantime, if you want to dig into the codebase I'm more than willing to give you a hand to get you familiarised. Any kind of help is always welcome! |
@dlitvakb Just to let you know: I got the scenario reproduced as a spec in my fork. |
Thanks @toadle, I'll finish testing this. I'll reword your commits to follow the Style Guide we use and submit for review afterwards. Thank you very much for the contribution and hope to collaborate more in the future. |
@dlitvakb Could you help me with one question so that I can put this thing in production-use? As you know I want to be able to use
Sadly this doesn't work - can't tell why. It always gives me that Where would you monkey-patch this "setter" in? |
Hey @toadle, It's not working because of the You can accomplish what you want by doing the following: module SetterResource
def update_mappings!
properties.keys.each do |name|
define_singleton_method Contentful::Support.snakify(name).to_sym do |wanted_locale = default_locale|
properties[name] = fields(wanted_locale)[name]
end
define_singleton_method "#{Contentful::Support.snakify(name).to_sym}=" do |value, wanted_locale = default_locale|
fields(wanted_locale)[name] = value
end
end
end
end
class Cat < Contentful::Entry
include Contentful::Resource::CustomResource
include SetterResource
property :name
property :lives
property :bestFriend, Cat
end Hope that helps |
@dlitvakb Sadly no. When I do it this way then I don't really get this: Is there a difference between I also though about hacking |
Hey @toadle Properties define the schema of the object, whereas fields include the API response. Have you tried using the exact code I sent you? Instead of monkey patching Here is the test I used to check it and works perfectly: class OtherCat < Contentful::Entry
include Contentful::Resource::CustomResource
include SetterResource
property :name
property :lives
property :bestFriend, OtherCat
end
def test_setters
nyancat = client(entry_mapping: { 'cat' => OtherCat }).entries(include: 1, 'sys.id' => 'nyancat').first
nyancat.name = 'blah'
assert(nyancat.name, 'blah')
end Please let me know if that worked |
Yes I did. I got the slight feeling that this I did it like this:
and
Then tried it out:
Tried to debug it afterwards:
Result:
So in the end the goal is that I couldn't find the place where the attributes in |
Hey @toadle, I found what's causing the I'll update that later today and get you up and running |
@dlitvakb Any update on this? Otherwise I have this PR already in use ;-) |
4c32d2b
to
6c60bec
Compare
Hey @toadle Sorry for the delay, I was working on an integration project. The fix has now been pushed Please let me know if that helps |
@dlitvakb OK, got it working. Some little changes still needed, but this works:
and having everything be a
Thanks for the help up to here! |
No problem @toadle Glad to help |
Add CustomResource module and Fix Marshalling
Fixes #79
Fixes #80