|
14 | 14 |
|
15 | 15 | let(:link) { Link.create(source: source, target: target) } |
16 | 16 |
|
| 17 | + |
| 18 | + context "when creating associations" do |
| 19 | + it "sets the source_type" do |
| 20 | + link = dog.source_links.new |
| 21 | + expect(link.source_type).to eq("Animal") |
| 22 | + end |
| 23 | + |
| 24 | + it "sets the target_type" do |
| 25 | + link = kibble.target_links.new |
| 26 | + expect(link.target_type).to eq("Food") |
| 27 | + end |
| 28 | + |
| 29 | + context "when models are namespaced" do |
| 30 | + context "and mappings include namespaces" do |
| 31 | + it "sets the source_type" do |
| 32 | + allow(Link).to receive(:source_type_mapping).and_return({3 => "Namespaced::Plant"}) |
| 33 | + allow(Link).to receive(:source_type_mapping2).and_return({3 => "Namespaced::Plant"}) |
| 34 | + |
| 35 | + link = Namespaced::Plant.create(name: "Oak").source_links.new |
| 36 | + expect(link.source_type).to eq("Namespaced::Plant") |
| 37 | + allow(Link).to receive(:source_type_mapping).and_return({1 => "Person", 2 => "Animal", 3 => "Plant"}) |
| 38 | + end |
| 39 | + |
| 40 | + it "sets the target_type" do |
| 41 | + allow(Link).to receive(:target_type_mapping).and_return({3 => "Namespaced::Activity"}) |
| 42 | + link = Namespaced::Activity.create(name: "swaying").target_links.new |
| 43 | + expect(link.target_type).to eq("Namespaced::Activity") |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + context "and mappings don't include namespaces" do |
| 48 | + it "sets the source type" do |
| 49 | + Link.source_type_mapping |
| 50 | + link = Namespaced::Plant.create(name: "Oak").source_links.new |
| 51 | + expect(link.source_type).to eq("Plant") |
| 52 | + end |
| 53 | + |
| 54 | + it "sets the target type" do |
| 55 | + link = Namespaced::Activity.create(name:"swaying").target_links.new |
| 56 | + expect(link.target_type).to eq("Activity") |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + |
17 | 62 | context "when the source is nil" do |
18 | 63 | let(:source) { nil } |
19 | 64 | let(:target) { nil } |
|
73 | 118 | it "properly finds the object with a find_by" do |
74 | 119 | expect(Link.find_by(source: source, id: link.id)).to eql link |
75 | 120 | 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 |
85 | 121 | end |
86 | 122 |
|
87 | 123 | shared_examples "proper source" do |
|
0 commit comments