active_repository 0.5.0 → 0.5.1
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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3682a06da966f35d1d173a4bfd20bf284fd6618
|
4
|
+
data.tar.gz: 842a9c142db9cbb0c31efcf5ac5cefaf6d21f79c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a27dda12a0ce272d9a187ed45c9b9c795e954156c41d366461b80729deeeef8a4318932699b05cf32358966b632800e31e0ec73bc933a62b62a9f75dd0ce963
|
7
|
+
data.tar.gz: 6879de80c9ce5aa1de0cc2094b5e6b506758db4f672198581a801de6afaba9b57cca0c086dc193aa54bc770bf3563f3923eecf403a8c2e8a0b81b87a61f6fdfc
|
@@ -15,10 +15,11 @@ module ActiveRepository
|
|
15
15
|
def has_many(association_id, options = {})
|
16
16
|
define_method(association_id) do
|
17
17
|
options = {
|
18
|
-
class_name: association_id.to_s.classify
|
18
|
+
class_name: association_id.to_s.classify,
|
19
|
+
foreign_key: self.class.to_s.foreign_key
|
19
20
|
}.merge(options)
|
20
21
|
|
21
|
-
foreign_key =
|
22
|
+
foreign_key = options[:foreign_key]
|
22
23
|
klass = options[:class_name].constantize
|
23
24
|
|
24
25
|
klass.where(foreign_key => id)
|
@@ -28,7 +29,8 @@ module ActiveRepository
|
|
28
29
|
# Defines "has one" type relation between ActiveRepository objects
|
29
30
|
def has_one(association_id, options = {})
|
30
31
|
options = {
|
31
|
-
class_name: association_id.to_s.classify
|
32
|
+
class_name: association_id.to_s.classify,
|
33
|
+
foreign_key: self.to_s.foreign_key
|
32
34
|
}.merge(options)
|
33
35
|
|
34
36
|
has_one_methods(association_id, options)
|
@@ -61,7 +63,7 @@ module ActiveRepository
|
|
61
63
|
|
62
64
|
def define_has_one_method(association_id, options)
|
63
65
|
define_method(association_id) do
|
64
|
-
foreign_key =
|
66
|
+
foreign_key = options[:foreign_key]
|
65
67
|
klass = options[:class_name].constantize
|
66
68
|
|
67
69
|
klass.where(foreign_key => self.id).first
|
@@ -10,6 +10,25 @@ describe ActiveRepository::Base, "associations" do
|
|
10
10
|
connection.create_table(:countries, :force => true) {}
|
11
11
|
end
|
12
12
|
|
13
|
+
class CountryRepository < ActiveRepository::Base
|
14
|
+
self.persistence_class = Country
|
15
|
+
self.save_in_memory = false
|
16
|
+
end
|
17
|
+
|
18
|
+
class Mountain < ActiveRecord::Base
|
19
|
+
establish_connection :adapter => "sqlite3", :database => ":memory:"
|
20
|
+
connection.create_table(:mountains, :force => true) do |t|
|
21
|
+
t.integer :country_id
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class President < ActiveRecord::Base
|
26
|
+
establish_connection :adapter => "sqlite3", :database => ":memory:"
|
27
|
+
connection.create_table(:presidents, :force => true) do |t|
|
28
|
+
t.integer :country_id
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
13
32
|
class School < ActiveRecord::Base
|
14
33
|
establish_connection :adapter => "sqlite3", :database => ":memory:"
|
15
34
|
connection.create_table(:schools, :force => true) do |t|
|
@@ -43,6 +62,9 @@ describe ActiveRepository::Base, "associations" do
|
|
43
62
|
Object.send :remove_const, :City
|
44
63
|
Object.send :remove_const, :Author
|
45
64
|
Object.send :remove_const, :Country
|
65
|
+
Object.send :remove_const, :CountryRepository
|
66
|
+
Object.send :remove_const, :Mountain
|
67
|
+
Object.send :remove_const, :President
|
46
68
|
Object.send :remove_const, :School
|
47
69
|
Object.send :remove_const, :Book
|
48
70
|
end
|
@@ -90,6 +112,22 @@ describe ActiveRepository::Base, "associations" do
|
|
90
112
|
end
|
91
113
|
end
|
92
114
|
|
115
|
+
context "with ActiveRecord children but different naming scheme requiring foreign_key option" do
|
116
|
+
it "honors foreign_key option to properly fetch child records from has_many" do
|
117
|
+
CountryRepository.has_many :mountains, :foreign_key => "country_id"
|
118
|
+
country = Country.create :id => 1
|
119
|
+
mountain_1 = Mountain.create :country_id => 1, :id => 1
|
120
|
+
expect(CountryRepository.first.mountains).to eq([mountain_1])
|
121
|
+
end
|
122
|
+
|
123
|
+
it "honors foreign_key option to properly fetch child record from has_one" do
|
124
|
+
CountryRepository.has_one :president, :foreign_key => "country_id"
|
125
|
+
country = Country.create :id => 1
|
126
|
+
president_1 = President.create :country_id => 1, :id => 1
|
127
|
+
expect(CountryRepository.first.president).to eq(president_1)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
93
131
|
end
|
94
132
|
|
95
133
|
describe "#belongs_to" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_repository
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Caio Torres
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_hash
|