|
73 | 73 | it "properly finds the object with a find_by" do |
74 | 74 | expect(Link.find_by(source: source, id: link.id)).to eql link |
75 | 75 | end |
| 76 | + |
| 77 | + context "when source and target are namedpaced without modifying polymorphic_name" do |
| 78 | + it "properly finds the object" do |
| 79 | + plant = Namespaced::Plant.create(name: "Mighty", kind: "Oak", owner: owner) |
| 80 | + activity = Namespaced::Activity.create(name: "swaying") |
| 81 | + link = Link.create(source: plant, target: activity) |
| 82 | + expect(Link.where(source: plant, id: link.id).first).to eql link |
| 83 | + end |
| 84 | + end |
76 | 85 | end |
77 | 86 |
|
78 | 87 | shared_examples "proper source" do |
|
103 | 112 | expect(source.source_links[0].source).to eql source |
104 | 113 | end |
105 | 114 | end |
106 | | - |
107 | 115 | end |
108 | 116 | context "When a link is given polymorphic record" do |
109 | 117 | let(:link) { Link.create(source: source) } |
|
116 | 124 |
|
117 | 125 | include_examples "proper source" |
118 | 126 | include_examples "proper target" |
119 | | - |
120 | 127 | end |
121 | | - |
122 | 128 | end |
123 | 129 |
|
124 | 130 | context "When a link is given polymorphic id and type" do |
|
131 | 137 | before { link.update_attributes(target_id: target.id, target_type: target.class.to_s) } |
132 | 138 | include_examples "proper source" |
133 | 139 | include_examples "proper target" |
134 | | - |
135 | 140 | end |
136 | | - |
137 | 141 | end |
138 | 142 |
|
139 | 143 | context "When using a relation to the links with eager loading" do |
|
146 | 150 | it "should be able to return the links and the targets" do |
147 | 151 | expect(cat.source_links).to match_array links |
148 | 152 | expect(cat.source_links.includes(:target).collect(&:target)).to match_array [water, kibble] |
149 | | - |
150 | 153 | end |
151 | | - |
152 | 154 | end |
153 | 155 |
|
154 | 156 | context "When using a through relation to the links with eager loading" do |
|
161 | 163 | it "should be able to return the links and the targets" do |
162 | 164 | expect(owner.pet_source_links).to match_array links |
163 | 165 | expect(owner.pet_source_links.includes(:target).collect(&:target)).to match_array [water, kibble] |
164 | | - |
165 | 166 | end |
166 | | - |
167 | 167 | end |
168 | 168 |
|
169 | 169 | context "When eager loading the polymorphic association" do |
|
178 | 178 | expect(links.first.source).to eql cat |
179 | 179 | expect(links.last.source).to eql dog |
180 | 180 | end |
181 | | - |
182 | 181 | end |
183 | 182 |
|
184 | 183 | it "should be able to preload the association" do |
185 | 184 | l = Link.includes(:source).where(id: link.id).first |
186 | 185 | expect(l.source).to eql cat |
187 | 186 | end |
188 | | - |
189 | | - |
190 | | - |
191 | 187 | end |
192 | 188 |
|
193 | 189 | context "when the association is an STI table" do |
@@ -239,7 +235,7 @@ class InlineDrink < ActiveRecord::Base |
239 | 235 | include_examples "proper target" |
240 | 236 |
|
241 | 237 | it "creates foreign_type mapping method" do |
242 | | - expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal"}) |
| 238 | + expect(Link.source_type_mapping).to eq({1 => "Person", 2 => "Animal", 3 => "Plant"}) |
243 | 239 | expect(InlineLink.source_type_mapping).to eq({10 => "Person", 11 => "InlineAnimal"}) |
244 | 240 | end |
245 | 241 |
|
|
0 commit comments