bigamy 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
data/bigamy.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bigamy}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Angilly"]
@@ -30,7 +30,8 @@ Gem::Specification.new do |s|
30
30
  "test/test_helper.rb",
31
31
  "test/unit/test_ar_side.rb",
32
32
  "test/unit/test_helper.rb",
33
- "test/unit/test_mongo_side.rb"
33
+ "test/unit/test_mongo_side.rb",
34
+ "test/unit/test_proxy.rb"
34
35
  ]
35
36
  s.homepage = %q{http://github.com/ryana/bigamy}
36
37
  s.rdoc_options = ["--charset=UTF-8"]
@@ -42,7 +43,8 @@ Gem::Specification.new do |s|
42
43
  "test/test_helper.rb",
43
44
  "test/unit/test_ar_side.rb",
44
45
  "test/unit/test_helper.rb",
45
- "test/unit/test_mongo_side.rb"
46
+ "test/unit/test_mongo_side.rb",
47
+ "test/unit/test_proxy.rb"
46
48
  ]
47
49
 
48
50
  if s.respond_to? :specification_version then
data/lib/bigamy/proxy.rb CHANGED
@@ -1,14 +1,12 @@
1
1
  module Bigamy
2
2
 
3
3
  class Proxy
4
- attr_accessor :name, :me, :primary_key, :foreign_key, :klass, :methods_added,
5
- :options
4
+ attr_accessor :name, :me, :primary_key, :methods_added, :options
6
5
 
7
6
  def initialize parent, name, options
8
7
  self.name = name
9
8
  self.me = parent
10
9
  self.primary_key = options.delete(:primary_key) || :id
11
- self.klass = options.delete(:class) || target_klass
12
10
  self.methods_added = Set.new
13
11
  self.options = options
14
12
 
@@ -18,7 +16,7 @@ module Bigamy
18
16
  end
19
17
 
20
18
  def foreign_key
21
- options[:foreign_key] || :"#{name.to_s.singularize}_id"
19
+ raise
22
20
  end
23
21
 
24
22
  def create_accessors
@@ -51,8 +49,9 @@ module Bigamy
51
49
  end
52
50
 
53
51
  def target_klass
54
- name.to_s.camelcase.singularize.constantize
52
+ options[:class] || name.to_s.camelcase.singularize.constantize
55
53
  end
54
+ alias klass target_klass
56
55
 
57
56
  def target_klass_name
58
57
  name.to_s.underscore.singularize.gsub('/', '_')
data/test/test_helper.rb CHANGED
@@ -22,6 +22,8 @@ end
22
22
  class User < ActiveRecord::Base
23
23
  end
24
24
 
25
+ Other = User
26
+
25
27
  $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
26
28
 
27
29
  require 'bigamy'
@@ -37,12 +37,36 @@ class TestMongoSide < Test::Unit::TestCase
37
37
  assert User.included_modules.include?(Bigamy::ActiveRecord::InstanceMethods)
38
38
  end
39
39
 
40
+ should "use class option" do
41
+ Doc.has_one_ar :user, :class => Other
42
+ assert_equal Other, Doc.bigamy_associations[:user].target_klass
43
+ end
44
+
45
+ should "use foreign_key option" do
46
+ Doc.has_one_ar :user, :foreign_key => :random_thing
47
+ assert_equal :random_thing, Doc.bigamy_associations[:user].foreign_key
48
+ end
49
+
50
+ should "user primary_key option" do
51
+ Doc.has_one_ar :user, :primary_key => :random_thing
52
+ assert_equal :random_thing, Doc.bigamy_associations[:user].primary_key
53
+ end
54
+
40
55
  context "that has_one_ar :user" do
41
56
  setup do
42
57
  Doc.has_one_ar :user
43
58
  @user = User.create!
44
59
  end
45
60
 
61
+ should "have correct target and root classes" do
62
+ assert_equal Doc, Doc.bigamy_associations[:user].root_klass
63
+ assert_equal User, Doc.bigamy_associations[:user].target_klass
64
+ end
65
+
66
+ should "have correct foreign_key" do
67
+ assert_equal :doc_id, Doc.bigamy_associations[:user].foreign_key
68
+ end
69
+
46
70
  should "create accessors" do
47
71
  assert Doc.new.respond_to?(:user)
48
72
  assert Doc.new.respond_to?(:user=)
@@ -79,6 +103,15 @@ class TestMongoSide < Test::Unit::TestCase
79
103
  Doc.has_many_ar :users
80
104
  end
81
105
 
106
+ should "have correct target and root classes" do
107
+ assert_equal Doc, Doc.bigamy_associations[:users].root_klass
108
+ assert_equal User, Doc.bigamy_associations[:users].target_klass
109
+ end
110
+
111
+ should "have correct foreign_key" do
112
+ assert_equal :doc_id, Doc.bigamy_associations[:users].foreign_key
113
+ end
114
+
82
115
  should "create accessors" do
83
116
  assert Doc.new.respond_to?(:users)
84
117
  assert Doc.new.respond_to?(:users=)
@@ -125,6 +158,14 @@ class TestMongoSide < Test::Unit::TestCase
125
158
  Doc.belongs_to_ar :user
126
159
  end
127
160
 
161
+ should "have correct target and root classes" do
162
+ assert_equal Doc, Doc.bigamy_associations[:user].root_klass
163
+ assert_equal User, Doc.bigamy_associations[:user].target_klass
164
+ end
165
+
166
+ should "have correct foreign_key" do
167
+ assert_equal :user_id, Doc.bigamy_associations[:user].foreign_key
168
+ end
128
169
  should "create accessors" do
129
170
  assert Doc.new.respond_to?(:user)
130
171
  assert Doc.new.respond_to?(:user=)
@@ -0,0 +1,101 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ module A
4
+ class B
5
+ end
6
+ end
7
+
8
+ class C
9
+ end
10
+
11
+ class DumbBelongsToProxy < Bigamy::BelongsTo
12
+ def add_getter
13
+ end
14
+
15
+ def add_setter
16
+ end
17
+ end
18
+
19
+ class DumbHasProxy < Bigamy::HasOne
20
+ def add_getter
21
+ end
22
+
23
+ def add_setter
24
+ end
25
+ end
26
+
27
+ class TestProxy < Test::Unit::TestCase
28
+
29
+ context "A referenced association proxy" do
30
+ setup do
31
+ @proxy = DumbHasProxy.new(C, :user, {})
32
+ end
33
+
34
+ should "have proper foreign_key" do
35
+ assert_equal :c_id, @proxy.foreign_key
36
+ end
37
+ end
38
+
39
+ context "An referenced association proxy with a namespaced model parent" do
40
+ setup do
41
+ @proxy = DumbHasProxy.new(A::B, :user, {})
42
+ end
43
+
44
+ should "have proper foreign_key" do
45
+ assert_equal :a_b_id, @proxy.foreign_key
46
+ end
47
+ end
48
+
49
+ context "An referenced association proxy with a namespaced model parent and a custom foreign_key" do
50
+ setup do
51
+ @proxy = DumbHasProxy.new(A::B, :user, {:foreign_key => :drats})
52
+ end
53
+
54
+ should "have proper foreign_key" do
55
+ assert_equal :drats, @proxy.foreign_key
56
+ end
57
+ end
58
+
59
+ context "An referenced association proxy with a namespaced target" do
60
+ setup do
61
+ @proxy = DumbHasProxy.new(A::B, :user, {})
62
+ end
63
+
64
+ should "have proper foreign_key" do
65
+ assert_equal :a_b_id, @proxy.foreign_key
66
+ end
67
+
68
+ end
69
+
70
+ context "An association proxy" do
71
+ setup do
72
+ @proxy = DumbBelongsToProxy.new(C, :user, {})
73
+ end
74
+
75
+ should "have proper foreign_key" do
76
+ assert_equal :user_id, @proxy.foreign_key
77
+ end
78
+ end
79
+
80
+ context "An association proxy with a namespaced model parent" do
81
+ setup do
82
+ @proxy = DumbBelongsToProxy.new(A::B, :user, {})
83
+ end
84
+
85
+ should "have proper foreign_key" do
86
+ assert_equal :user_id, @proxy.foreign_key
87
+ end
88
+ end
89
+
90
+ context "An association proxy with a namespaced target" do
91
+ setup do
92
+ @proxy = DumbBelongsToProxy.new(C, :user, {:class => A::B})
93
+ end
94
+
95
+ should "have proper foreign_key" do
96
+ assert_equal :user_id, @proxy.foreign_key
97
+ end
98
+
99
+ end
100
+
101
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ryan Angilly
@@ -125,6 +125,7 @@ files:
125
125
  - test/unit/test_ar_side.rb
126
126
  - test/unit/test_helper.rb
127
127
  - test/unit/test_mongo_side.rb
128
+ - test/unit/test_proxy.rb
128
129
  has_rdoc: true
129
130
  homepage: http://github.com/ryana/bigamy
130
131
  licenses: []
@@ -161,3 +162,4 @@ test_files:
161
162
  - test/unit/test_ar_side.rb
162
163
  - test/unit/test_helper.rb
163
164
  - test/unit/test_mongo_side.rb
165
+ - test/unit/test_proxy.rb