Total Complexity | 2 |
Complexity/F | 1 |
Lines of Code | 34 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import Relation from "../Relation"; |
||
2 | import ForeignKey from "../Field/ForeignKey"; |
||
3 | |||
4 | export default class MorphOne extends Relation { |
||
5 | |||
6 | constructor(model) { |
||
7 | super(model); |
||
8 | } |
||
9 | |||
10 | get originalValue() { |
||
11 | return this.getValueByParentKey('originalPrimaryKey'); |
||
12 | } |
||
13 | |||
14 | get value() { |
||
15 | return this.getValueByParentKey('primaryKey'); |
||
16 | } |
||
17 | |||
18 | getRelationalFields(): Array<ForeignKey> { |
||
19 | return []; |
||
20 | } |
||
21 | |||
22 | private getValueByParentKey(parentProperty): unknown { |
||
23 | const type = this.$parent.className; |
||
24 | const id = this.$parent[parentProperty]; |
||
25 | const idKeyName = `${this.name}_id`; |
||
26 | const idTypeName = `${this.name}_type`; |
||
27 | |||
28 | const lookUpKey = {}; |
||
29 | lookUpKey[idKeyName] = id; |
||
30 | lookUpKey[idTypeName] = type; |
||
31 | |||
32 | return this.model.find(lookUpKey); |
||
33 | } |
||
34 | } |