Permalink
Browse files

Merge pull request #153 from om-nishu-trantor/feature/where-for-activ…

…eresource-collection

`where` for `ActiveResource::Collection`
  • Loading branch information...
2 parents 5bb25e9 + d5cd1cc commit e28f907145c34bcad1d354fa9b25fbd4264e52e9 @rafaelfranca rafaelfranca committed on GitHub Feb 13, 2017
Showing with 19 additions and 0 deletions.
  1. +6 −0 lib/active_resource/collection.rb
  2. +13 −0 test/cases/collection_test.rb
@@ -82,5 +82,11 @@ def first_or_initialize(attributes = {})
rescue NoMethodError
raise "Cannot build resource from resource type: #{resource_class.inspect}"
end
+
+ def where(clauses = {})
+ raise ArgumentError, "expected a clauses Hash, got #{clauses.inspect}" unless clauses.is_a? Hash
+ new_clauses = original_params.merge(clauses)
+ resource_class.where(new_clauses)
+ end
end
end
@@ -41,6 +41,11 @@ def test_collect_bang_returns_collection
results = @collection.collect! { |i| i + "!" }
assert_kind_of ActiveResource::Collection, results
end
+
+ def respond_to_where
+ assert @collection.respond_to?(:where)
+ end
+
end
class PaginatedCollection < ActiveResource::Collection
@@ -76,6 +81,7 @@ def setup
mock.get '/paginated_posts/new.json', {}, @new_post
mock.get '/paginated_posts.json?page=2', {}, @posts
mock.get '/paginated_posts.json?title=test', {}, @empty_posts
+ mock.get '/paginated_posts.json?page=2&title=Awesome', {}, @posts
mock.post '/paginated_posts.json', {}, nil
end
end
@@ -105,4 +111,11 @@ def test_first_or_initialize
post = PaginatedPost.where(:title => 'test').first_or_initialize
assert post.valid?
end
+
+ def test_where
+ posts = PaginatedPost.where(:page => 2)
+ next_posts = posts.where(:title => 'Awesome')
+ assert_kind_of PaginatedCollection, next_posts
+ end
+
end

0 comments on commit e28f907

Please sign in to comment.