somehow_has_relation 0.0.4 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/lib/somehow_has_relation/version.rb +1 -1
- data/lib/somehow_has_relation.rb +25 -16
- data/test/somehow_has_relation_test.rb +7 -4
- metadata +3 -3
data/Gemfile
CHANGED
data/lib/somehow_has_relation.rb
CHANGED
@@ -25,7 +25,12 @@ module SomehowHasRelation
|
|
25
25
|
|
26
26
|
# Dynamic Instance Method related_%{relation_name}
|
27
27
|
define_method(related) do
|
28
|
-
somehow_found_or_recur relation, current_options[:if], current_options
|
28
|
+
found = somehow_found_or_recur relation, current_options[:if], current_options
|
29
|
+
if current_options.has_key?(:one)
|
30
|
+
found.flatten.compact.first rescue found
|
31
|
+
elsif current_options.has_key?(:many)
|
32
|
+
found.flatten.compact rescue []
|
33
|
+
end
|
29
34
|
end
|
30
35
|
end
|
31
36
|
end
|
@@ -42,24 +47,28 @@ module SomehowHasRelation
|
|
42
47
|
end
|
43
48
|
|
44
49
|
def somehow_found_or_recur(relation, condition=nil, opts=nil)
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
50
|
+
if self.class.send(:class_variable_defined?, :@@somehow_has_relation_options)
|
51
|
+
opts ||= self.class.send(:class_variable_get, :@@somehow_has_relation_options)
|
52
|
+
opts = [opts] unless opts.is_a? Array
|
53
|
+
|
54
|
+
found = []
|
55
|
+
|
56
|
+
opts.each do |opt|
|
57
|
+
begin
|
58
|
+
if opt.has_key?(:through)
|
59
|
+
found << somehow_recur(relation, opt[:through], opt[:if])
|
60
|
+
else
|
61
|
+
return send_and_filter(relation, condition)
|
62
|
+
end
|
63
|
+
rescue
|
64
|
+
found << []
|
56
65
|
end
|
57
|
-
rescue
|
58
|
-
found << (opt.has_key?(:many) ? [] : nil)
|
59
66
|
end
|
60
|
-
end
|
61
67
|
|
62
|
-
|
68
|
+
found
|
69
|
+
else
|
70
|
+
send_and_filter(relation, condition)
|
71
|
+
end
|
63
72
|
end
|
64
73
|
|
65
74
|
private
|
@@ -26,10 +26,13 @@ class SomehowHasRelationTest < ActiveSupport::TestCase
|
|
26
26
|
end
|
27
27
|
|
28
28
|
test "relations should work even when somehow_has has not been defined for first step" do
|
29
|
-
|
29
|
+
destroy_somehow_has
|
30
|
+
comments = [Comment.create, Comment.create]
|
31
|
+
post = Post.create(:comments => comments)
|
32
|
+
@author = Author.create(:posts => [post])
|
30
33
|
|
31
|
-
|
32
|
-
assert_equal
|
34
|
+
Author.somehow_has :many => :comments, :through => :posts
|
35
|
+
assert_equal post.comments, @author.related_comments
|
33
36
|
end
|
34
37
|
|
35
38
|
test "1-step relation defines a method prefixed with related_ for standard associations methods" do
|
@@ -151,7 +154,7 @@ class SomehowHasRelationTest < ActiveSupport::TestCase
|
|
151
154
|
|
152
155
|
def destroy_somehow_has
|
153
156
|
models.each do |model|
|
154
|
-
model.send(:remove_class_variable, :@@somehow_has_relation_options)
|
157
|
+
model.send(:remove_class_variable, :@@somehow_has_relation_options) rescue next
|
155
158
|
end
|
156
159
|
end
|
157
160
|
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:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Matteo Latini
|