active-hash / active_hash

A readonly ActiveRecord-esque base class that lets you use a hash, a Yaml file or a custom file as the datasource
MIT License
1.2k stars 179 forks source link

Add #to_ary to ActiveHash::Relation #182

Closed nsommer closed 4 years ago

nsommer commented 4 years ago

This PR adds #to_ary to ActiveHash::Relation, which returns a #duped array of the items of the relation. When upgrading to active_hash 3.0.0, the following line in a controller that used active_model_serializers to return a collection of items of an ActiveHash data store did not work anymore, because active_model_serializers uses #respond_to?(:to_ary) to determine if an object is a collection or not (ActiveRecord::Relation does respond to this method). After this PR, this should work again.

render json: MyActiveHashClass.all, each_serializer: MyActiveHashClassSerializer

I put the spec for this new method into a new file spec/active_hash/relation_spec.rb, because it would not really belong to the base_spec.rb.

nsommer commented 4 years ago

@kbrock As you mentioned in my previous PR (#178), it seems like #to_ary also removes the necessity to call #to_a in base_spec.rb. :-)

syguer commented 4 years ago

👍