From 86a28a98bbc0e16acd21ab4aa57266b777e91af1 Mon Sep 17 00:00:00 2001 From: reemplazable Date: Thu, 1 Jun 2023 23:51:01 +0200 Subject: [PATCH 1/8] Add optional block to associations find method - Call class find if param is provided - Call find on find_some enumerable if block given - Else return find_some enumerable (same happens in ruby by default in an array) --- lib/spyke/relation.rb | 6 ++++-- lib/spyke/version.rb | 2 +- test/associations_test.rb | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/spyke/relation.rb b/lib/spyke/relation.rb index 8e3b340..91e1d38 100644 --- a/lib/spyke/relation.rb +++ b/lib/spyke/relation.rb @@ -41,8 +41,10 @@ def with_fallback(fallback = nil) end # Overrides Enumerable find - def find(id) - scoping { klass.find(id) } + def find(id = nil, &block) + return scoping { klass.find(id) } if id + + block_given? ? find_some.find(&block) : find_some end def find_one diff --git a/lib/spyke/version.rb b/lib/spyke/version.rb index 419bede..4adab6d 100644 --- a/lib/spyke/version.rb +++ b/lib/spyke/version.rb @@ -1,3 +1,3 @@ module Spyke - VERSION = '7.0.0' + VERSION = '7.1.0' end diff --git a/test/associations_test.rb b/test/associations_test.rb index dde2436..db5b990 100644 --- a/test/associations_test.rb +++ b/test/associations_test.rb @@ -90,6 +90,8 @@ def test_array_like_behavior assert_equal %w{ Fish Fruit }, recipe.groups[0..1].map(&:name) assert_equal 'Bread', recipe.groups.last.name assert_equal 'Fish', recipe.groups.first.name + assert_equal 'Fruit', recipe.groups.find { |g| g.name == 'Fruit' }.name + assert_kind_of Enumerable, recipe.groups.find end def test_nil_has_one_association From 69a586800b5d6ed5f1529bc43fd3e588e5d6efa2 Mon Sep 17 00:00:00 2001 From: Jens Balvig Date: Mon, 5 Jun 2023 11:24:33 +0900 Subject: [PATCH 2/8] add test for User.find with block --- test/orm_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/orm_test.rb b/test/orm_test.rb index 290d9b3..5941f6e 100644 --- a/test/orm_test.rb +++ b/test/orm_test.rb @@ -14,6 +14,16 @@ def test_find assert_equal 'Bob', user.name end + def test_find_with_block + stub_request(:get, 'http://sushi.com/users').to_return_json(result: { id: 1, name: 'Bob' }) + + user = User.find do |u| + u.name == 'Bob' + end + + assert_equal 'Bob', user.name + end + def test_reload stub_request(:get, 'http://sushi.com/recipes/1').to_return_json(result: { id: 1, title: 'Sushi' }) From 5c056f41440e41cc98ab39f481a550dd1258e9a8 Mon Sep 17 00:00:00 2001 From: reemplazable Date: Tue, 6 Jun 2023 22:27:20 +0200 Subject: [PATCH 3/8] Implement find with block argument for class ORM --- lib/spyke/orm.rb | 5 ++++- test/orm_test.rb | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/spyke/orm.rb b/lib/spyke/orm.rb index c367ea7..5fa84e9 100644 --- a/lib/spyke/orm.rb +++ b/lib/spyke/orm.rb @@ -25,8 +25,11 @@ def method_for(callback, value = nil) callback_methods[callback] end - def find(id) + def find(id = nil, &block) + return all.find_some.find(&block) if block_given? + raise ResourceNotFound if id.blank? + where(primary_key => id).find_one || raise(ResourceNotFound) end diff --git a/test/orm_test.rb b/test/orm_test.rb index 5941f6e..f2b1692 100644 --- a/test/orm_test.rb +++ b/test/orm_test.rb @@ -15,7 +15,7 @@ def test_find end def test_find_with_block - stub_request(:get, 'http://sushi.com/users').to_return_json(result: { id: 1, name: 'Bob' }) + stub_request(:get, 'http://sushi.com/users').to_return_json(result: [{ id: 1, name: 'Bob' }]) user = User.find do |u| u.name == 'Bob' From 2f7df35bd562d8bf183b9a62785d2aa1ea588233 Mon Sep 17 00:00:00 2001 From: Jens Balvig Date: Wed, 7 Jun 2023 11:38:02 +0900 Subject: [PATCH 4/8] clear whitespace --- lib/spyke/orm.rb | 2 +- lib/spyke/relation.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/spyke/orm.rb b/lib/spyke/orm.rb index 5fa84e9..49dd503 100644 --- a/lib/spyke/orm.rb +++ b/lib/spyke/orm.rb @@ -29,7 +29,7 @@ def find(id = nil, &block) return all.find_some.find(&block) if block_given? raise ResourceNotFound if id.blank? - + where(primary_key => id).find_one || raise(ResourceNotFound) end diff --git a/lib/spyke/relation.rb b/lib/spyke/relation.rb index 91e1d38..6adb5ef 100644 --- a/lib/spyke/relation.rb +++ b/lib/spyke/relation.rb @@ -43,7 +43,7 @@ def with_fallback(fallback = nil) # Overrides Enumerable find def find(id = nil, &block) return scoping { klass.find(id) } if id - + block_given? ? find_some.find(&block) : find_some end From efe280d6f2fa04df0d9843f4a3826031d5bf7a0c Mon Sep 17 00:00:00 2001 From: Jens Balvig Date: Wed, 7 Jun 2023 11:46:15 +0900 Subject: [PATCH 5/8] split tests --- test/associations_test.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/associations_test.rb b/test/associations_test.rb index db5b990..9bd6195 100644 --- a/test/associations_test.rb +++ b/test/associations_test.rb @@ -90,8 +90,14 @@ def test_array_like_behavior assert_equal %w{ Fish Fruit }, recipe.groups[0..1].map(&:name) assert_equal 'Bread', recipe.groups.last.name assert_equal 'Fish', recipe.groups.first.name + end + + def test_find_with_block + stub_request(:get, 'http://sushi.com/recipes/1/groups').to_return_json(result: [{ name: 'Fish' }, { name: 'Fruit' }, { name: 'Bread' }]) + + recipe = Recipe.new(id: 1) + assert_equal 'Fruit', recipe.groups.find { |g| g.name == 'Fruit' }.name - assert_kind_of Enumerable, recipe.groups.find end def test_nil_has_one_association From bb78165d056bf03cec11e6996237e9e19f2c8da7 Mon Sep 17 00:00:00 2001 From: Jens Balvig Date: Wed, 7 Jun 2023 11:48:23 +0900 Subject: [PATCH 6/8] can rely on code in orm --- lib/spyke/relation.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/spyke/relation.rb b/lib/spyke/relation.rb index 6adb5ef..2e7f59c 100644 --- a/lib/spyke/relation.rb +++ b/lib/spyke/relation.rb @@ -42,9 +42,7 @@ def with_fallback(fallback = nil) # Overrides Enumerable find def find(id = nil, &block) - return scoping { klass.find(id) } if id - - block_given? ? find_some.find(&block) : find_some + scoping { klass.find(id, &block) } end def find_one From 0a3226625cdd785cd4ce81b3c388d3b687d7cc79 Mon Sep 17 00:00:00 2001 From: Jens Balvig Date: Wed, 7 Jun 2023 11:48:31 +0900 Subject: [PATCH 7/8] do a regular branch --- lib/spyke/orm.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/spyke/orm.rb b/lib/spyke/orm.rb index 49dd503..d7ecad6 100644 --- a/lib/spyke/orm.rb +++ b/lib/spyke/orm.rb @@ -26,11 +26,13 @@ def method_for(callback, value = nil) end def find(id = nil, &block) - return all.find_some.find(&block) if block_given? + if block_given? + all.find_some.find(&block) + else + raise ResourceNotFound if id.blank? - raise ResourceNotFound if id.blank? - - where(primary_key => id).find_one || raise(ResourceNotFound) + where(primary_key => id).find_one || raise(ResourceNotFound) + end end def fetch From 2ab06de0f31b8ca379f0ee6b6b1e8e3f4cf52cd3 Mon Sep 17 00:00:00 2001 From: Jens Balvig Date: Wed, 7 Jun 2023 11:49:16 +0900 Subject: [PATCH 8/8] flesh out test a bit --- test/orm_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/orm_test.rb b/test/orm_test.rb index f2b1692..199a47d 100644 --- a/test/orm_test.rb +++ b/test/orm_test.rb @@ -15,7 +15,7 @@ def test_find end def test_find_with_block - stub_request(:get, 'http://sushi.com/users').to_return_json(result: [{ id: 1, name: 'Bob' }]) + stub_request(:get, 'http://sushi.com/users').to_return_json(result: [{ id: 1, name: 'Bob' }, id: 2, name: 'Alice']) user = User.find do |u| u.name == 'Bob'