|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
3 | 3 | describe PolymorphicIntegerType do |
| 4 | + let(:owner) { Person.create(name: "Kyle") } |
| 5 | + let(:dog) { Animal.create(name: "Bela", kind: "Dog", owner: owner) } |
| 6 | + let(:cat) { Animal.create(name: "Alexi", kind: "Cat") } |
4 | 7 |
|
5 | | - let(:owner) { Person.create(:name => "Kyle") } |
6 | | - let(:dog) { Animal.create(:name => "Bela", :kind => "Dog", :owner => owner) } |
7 | | - let(:cat) { Animal.create(:name => "Alexi", :kind => "Cat") } |
| 8 | + let(:kibble) { Food.create(name: "Kibble") } |
| 9 | + let(:chocolate) { Food.create(name: "Choclate") } |
8 | 10 |
|
| 11 | + let(:milk) { Drink.create(name: "milk") } |
| 12 | + let(:water) { Drink.create(name: "Water") } |
| 13 | + let(:whiskey) { Drink.create(name: "Whiskey") } |
9 | 14 |
|
10 | | - let(:kibble) { Food.create(:name => "Kibble") } |
11 | | - let(:chocolate) { Food.create(:name => "Choclate") } |
12 | | - |
13 | | - |
14 | | - let(:milk) { Drink.create(:name => "milk") } |
15 | | - let(:water) { Drink.create(:name => "Water") } |
16 | | - let(:whiskey) { Drink.create(:name => "Whiskey") } |
17 | | - |
18 | | - let(:link) { Link.create(:source => source, :target => target) } |
| 15 | + let(:link) { Link.create(source: source, target: target) } |
19 | 16 |
|
20 | 17 | context "when the source is nil" do |
21 | 18 | let(:source) { nil } |
|
58 | 55 |
|
59 | 56 | end |
60 | 57 | context "When a link is given polymorphic record" do |
61 | | - let(:link) { Link.create(:source => source) } |
| 58 | + let(:link) { Link.create(source: source) } |
62 | 59 | let(:source) { cat } |
63 | 60 | include_examples "proper source" |
64 | 61 |
|
65 | 62 | context "and when it already has a polymorphic record" do |
66 | 63 | let(:target) { kibble } |
67 | | - before { link.update_attributes(:target => target) } |
| 64 | + before { link.update_attributes(target: target) } |
68 | 65 |
|
69 | 66 | include_examples "proper source" |
70 | 67 | include_examples "proper target" |
|
74 | 71 | end |
75 | 72 |
|
76 | 73 | context "When a link is given polymorphic id and type" do |
77 | | - let(:link) { Link.create(:source_id => source.id, :source_type => source.class.to_s) } |
| 74 | + let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) } |
78 | 75 | let(:source) { cat } |
79 | 76 | include_examples "proper source" |
80 | 77 |
|
81 | 78 | context "and when it already has a polymorphic id and type" do |
82 | 79 | let(:target) { kibble } |
83 | | - before { link.update_attributes(:target_id => target.id, :target_type => target.class.to_s) } |
| 80 | + before { link.update_attributes(target_id: target.id, target_type: target.class.to_s) } |
84 | 81 | include_examples "proper source" |
85 | 82 | include_examples "proper target" |
86 | 83 |
|
|
90 | 87 |
|
91 | 88 | context "When using a relation to the links with eagar loading" do |
92 | 89 | let!(:links){ |
93 | | - [Link.create(:source => source, :target => kibble), |
94 | | - Link.create(:source => source, :target => water)] |
| 90 | + [Link.create(source: source, target: kibble), |
| 91 | + Link.create(source: source, target: water)] |
95 | 92 | } |
96 | 93 | let(:source) { cat } |
97 | 94 |
|
|
105 | 102 |
|
106 | 103 | context "When using a through relation to the links with eagar loading" do |
107 | 104 | let!(:links){ |
108 | | - [Link.create(:source => source, :target => kibble), |
109 | | - Link.create(:source => source, :target => water)] |
| 105 | + [Link.create(source: source, target: kibble), |
| 106 | + Link.create(source: source, target: water)] |
110 | 107 | } |
111 | 108 | let(:source) { dog } |
112 | 109 |
|
|
119 | 116 | end |
120 | 117 |
|
121 | 118 | context "When eagar loading the polymorphic association" do |
122 | | - let(:link) { Link.create(:source_id => source.id, :source_type => source.class.to_s) } |
| 119 | + let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) } |
123 | 120 | let(:source) { cat } |
124 | 121 |
|
125 | 122 | context "and when there are multiples sources" do |
126 | | - let(:link_2) { Link.create(:source_id => source_2.id, :source_type => source_2.class.to_s) } |
| 123 | + let(:link_2) { Link.create(source_id: source_2.id, source_type: source_2.class.to_s) } |
127 | 124 | let(:source_2) { dog } |
128 | 125 | it "should be able to preload both associations" do |
129 | | - links = Link.includes(:source).where(:id => [link.id, link_2.id]).order(:id) |
| 126 | + links = Link.includes(:source).where(id: [link.id, link_2.id]).order(:id) |
130 | 127 | expect(links.first.source).to eql cat |
131 | 128 | expect(links.last.source).to eql dog |
132 | 129 | end |
133 | 130 |
|
134 | 131 | end |
135 | 132 |
|
136 | 133 | it "should be able to preload the association" do |
137 | | - l = Link.includes(:source).where(:id => link.id).first |
| 134 | + l = Link.includes(:source).where(id: link.id).first |
138 | 135 | expect(l.source).to eql cat |
139 | 136 | end |
140 | 137 |
|
141 | 138 |
|
142 | 139 | end |
143 | 140 |
|
144 | 141 | context "when the association is an STI table" do |
145 | | - let(:link) { Link.create(:source => source, :target => whiskey) } |
146 | | - let(:source) { Dog.create(:name => "Bela", :kind => "Dog", :owner => owner) } |
| 142 | + let(:link) { Link.create(source: source, target: whiskey) } |
| 143 | + let(:source) { Dog.create(name: "Bela", kind: "Dog", owner: owner) } |
147 | 144 | it "should have the proper id, type and object for the source" do |
148 | 145 | expect(link.source_id).to eql source.id |
149 | 146 | expect(link.source_type).to eql "Animal" |
150 | 147 | expect(link.source).to eql source |
151 | 148 | end |
152 | 149 | end |
153 | 150 |
|
154 | | - |
| 151 | + context "when " |
155 | 152 | end |
0 commit comments