somehow_has_relation 0.0.4 → 0.0.6

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.
data/Gemfile CHANGED
@@ -3,7 +3,7 @@ source "http://rubygems.org"
3
3
  if ENV["RAILS_VER"] == "3"
4
4
  gem "rails", "~> 3.0"
5
5
  else
6
- gem "rails", "~> 2.3"
6
+ gem "rails", "~> 2.3.11"
7
7
  end
8
8
 
9
9
  group :test do
@@ -1,3 +1,3 @@
1
1
  module SomehowHasRelation
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -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
- opts ||= self.class.send(:class_variable_get, :@@somehow_has_relation_options)
46
- opts = [opts] unless opts.is_a? Array
47
-
48
- found = []
49
-
50
- opts.each do |opt|
51
- begin
52
- if opt.has_key?(:through)
53
- found << somehow_recur(relation, opt[:through], opt[:if])
54
- else
55
- return send_and_filter(relation, condition)
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
- found.all?{|elem| elem.nil? || (elem.is_a?(Array) && elem.empty?)} ? nil : found.compact.flatten
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
- @post = Post.create(:author => Author.create, :comments => [Comment.create, Comment.create])
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
- assert_equal @post.author, @post.related_author
32
- assert_equal @post.comments, @post.related_comments
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: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matteo Latini