Completed
Push — master ( 3433cb...e6f031 )
by Mark
20s queued 11s
created

MorphOne.getValueByParentKey   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.95
c 0
b 0
f 0
cc 1
1
import Relation from "../Relation";
2
3
/**
4
 *
5
 */
6
export default class MorphOne extends Relation {
7
8
    /**
9
     *
10
     * @param {Model} model
11
     */
12
    constructor(model) {
13
        super(model);
14
    }
15
16
    /**
17
     *
18
     * @return {Model|null}
19
     */
20
    get value() {
21
        return this.getValueByParentKey('primaryKey');
22
    }
23
24
    /**
25
     *
26
     * @return {Model|null}
27
     */
28
    get originalValue() {
29
        return this.getValueByParentKey('originalPrimaryKey');
30
    }
31
32
    /**
33
     *
34
     * @param {string} parentProperty
35
     * @return {Model|null}
36
     */
37
    getValueByParentKey(parentProperty) {
38
        const type = this.$parent.className;
39
        const id = this.$parent[parentProperty];
40
        const idKeyName = `${this.$name}_id`;
41
        const idTypeName = `${this.$name}_type`;
42
43
        const lookUpKey = {};
44
        lookUpKey[idKeyName] = id;
45
        lookUpKey[idTypeName] = type;
46
47
        return this.model.find(lookUpKey);
48
    }
49
50
    /**
51
     *
52
     * @return {*[]}
53
     */
54
    getRelationalFields() {
55
        return [];
56
    }
57
}