mhgbrown / cached_resource

Caching for ActiveResource
MIT License
80 stars 28 forks source link

Doesn't return an ActiveResource::Collection #18

Closed rzane closed 10 years ago

rzane commented 10 years ago

Really useful and easy to use gem. The only issue I'm experiencing is that it makes the resource return an Array rather than an ActiveResource::Collection. This is especially an issue when using a custom collection parser that extends the functionality of the list of elements.

class PostCollection < ActiveResource::Collection
  attr_accessor :next_page
  def initialize(parsed = {})
    @elements = parsed['posts']
    @next_page = parsed['next_page']
  end
end

class Post < ActiveResource::Base
  self.site = "http://example.com"
  self.collection_parser = PostCollection
end

Example taken from: http://www.rubydoc.info/github/rails/activeresource/ActiveResource/Collection

mhgbrown commented 10 years ago

Awesome thanks. Is there some way we could get a test for this?

mhgbrown commented 10 years ago

Also, how does this affect compatibility?

rzane commented 10 years ago

I can definitely add a test tomorrow. Probably should have done that in the first place.

Regarding compatibility, ActiveResource::Collection was added with this PR: https://github.com/rails/activeresource/pull/22 for 4.0.0.beta1. Anything before won't respond_to?(:collection_parser), so they'll just return an array. For anyone using ActiveResource less than 4.0 there will be no change at all. ActiveResource::Collection accepts all array instance methods, so this change probably won't hurt anyone w/ ActiveResource greater than 4.0 either.

rzane commented 10 years ago

I've added specs, but the conditional tests feel a bit filthy. Let me know if you would prefer to do that a different way.

mhgbrown commented 10 years ago

Thanks for adding the tests. The conditional is a little filthy, but not something that's making me crazy. I think my defining and undefining of constants was also pretty filthy :) I'll get this merged hopefully later today and then release a new version!

rzane commented 10 years ago

Awesome. Thanks!

mhgbrown commented 10 years ago

Published to RubyGems! Thanks for contributing @rzane!

rzane commented 10 years ago

:+1: