somehow_has_relation 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  module SomehowHasRelation
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -19,8 +19,12 @@ module SomehowHasRelation
19
19
 
20
20
  # Dynamic Instance Method related_%{relation_name}
21
21
  define_method(related) do
22
- somehow_got = params[:through] ? somehow_look_for(relation, params[:through]) : send_and_filter(relation, filter)
23
- to_flatten ? somehow_got.flatten : somehow_got
22
+ begin
23
+ somehow_got = params[:through] ? somehow_look_for(relation, params[:through]) : send_and_filter(relation, filter)
24
+ to_flatten ? somehow_got.flatten : somehow_got
25
+ rescue
26
+ [] if params[:many]
27
+ end
24
28
  end
25
29
  end
26
30
 
@@ -124,4 +124,18 @@ class SomehowHasRelationTest < ActiveSupport::TestCase
124
124
  assert_equal comments1, @author.related_bio_comments
125
125
  assert_equal comments2, @author.related_posts_comments
126
126
  end
127
+
128
+ test "when there is no :one related, return nil" do
129
+ @post = Post.create
130
+ Post.somehow_has :one => :author
131
+
132
+ assert_nil @post.related_author
133
+ end
134
+
135
+ test "when there is no :many related, return []" do
136
+ @post = Post.create
137
+ Post.somehow_has :many => :comments
138
+
139
+ assert_equal [], @post.related_comments
140
+ end
127
141
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: somehow_has_relation
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matteo Latini