whichdigital / active-rest-client

ActiveRestClient API Client
https://rubygems.org/gems/active_rest_client
MIT License
386 stars 44 forks source link

unable to access object.errors #93

Closed vanso-hubsi closed 9 years ago

vanso-hubsi commented 9 years ago

Hi! Thanks for this helpful gem which helped me a lot to quickly create a REST-based model.

With basic requests working I now wanted to carry on with error handling. I wanted to use active-rest-client's validation capability, when I got stuck with not being able to access the errors hashmap.

In short, this didn't work:

class MyRestClass < ActiveRestClient::Base
  validates :myAttribute, presence: true
[...]
end

> restobject = MyRestClass.new()
> restobject.valid?
> restobject.errors
 => nil

I wondered why, because both valid? and errors are defined in validation.rb, but somehow I wasn't able to use the errors method. It wasn't in the list of restobject.methods either, so I started digging and found that the errors method seems to get undefined in base.rb:L14. When I removed errors from the array, all started working as expected.

I'm no ruby/rails expert, but to me it looks like a bug. Or is it me doing something wrong?

andyjeffries commented 9 years ago

The only reason may be it's quite common for REST responses to contain an error key. Maybe the method should be _errors so it doesn't conflict with an errors key from the response? Or maybe have #errors also work, if there was no such key. I'll have a look in to this.

andyjeffries commented 9 years ago

If you can test the latest github version, see if that works for you? If so, I'll release it as a new version as it's interface compatible.

vanso-hubsi commented 9 years ago

Thanks for the ultra quick response!

Maybe the method should be _errors so it doesn't conflict with an errors key from the response?

I don't think so, since "errors" is the error hash name commonly used in ActiveRecord. If the response should contain an errors key, I think you could rewrite it with a proxy.

The latest github version works for me, it's exactly the same change as what I did locally.

But I'm wondering if I'm really the only one trying to read the errors from validations?! Cause from the commit history it looks like this hasn't been working for a longer time... (And perhaps the validation spec tests should be modified to not use a struct but a real ActiveRestClient::Base object?)

Anyways, it's working now, so I'm going to close this issue.

vanso-hubsi commented 9 years ago

Oops, I didn't see that you renamed the errors method to _errors. This basically makes the errors thing work, but I can't use the ActiveRestClient objects in my views the same way as ActiveRecord objects. I have a helper function that checks for object.errors.any? which now doesn't work for ActiveRestClient objects anymore. Don't you think that your model should be as close as possible to ActiveRecord?

vanso-hubsi commented 9 years ago

Well, don't bother. I just found that I can't use my helper function anyways, as I'm using ActiveRecord::Errors specific calls, which is the errors type for ActiveRecord objects.

andyjeffries commented 9 years ago

We aim for it to be as close as possible, but there are some differences as we're potentially accessing a wider variety of "databases" ;-)

No to worry, sorry the quick response didn't help you in the end.

vanso-hubsi commented 9 years ago

No to worry, sorry the quick response didn't help you in the end.

It did help me: By calling _errors I'm now able to access the validation errors at all, which I wasn't before. Or was I?

andyjeffries commented 9 years ago

No, you're right, I think it had been broken a while. I guess no-one was using the validations :-)