activeadmin_dynamic_fields 0.4.4 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '019f2b81ed1552f13f57b73141860957f4869b10de1f018a289aa8724cc23ec1'
|
4
|
+
data.tar.gz: 4ed1bb60f5b1ddef1a3d870296d8548faa18cc36db5adadb8271f0c655c769f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b07cc4e73b801ef79ee52b52a6c82117d7e966a2f1eadb0e415e00c608456c02f2c14694c2fc41037ad381c9c0c3bfbf1c0ed96c0c6d13bd0ce3335c38dc1e11
|
7
|
+
data.tar.gz: c0aa33df12ef6ed16927a49bb9b93d0fcffeffc6f20b661f11edb350544f12e550f90fd5fa5870a43154c0097995bbb02f2f80f24e1ff821eddc2d0fa60e613d
|
@@ -52,58 +52,70 @@
|
|
52
52
|
|
53
53
|
class Field {
|
54
54
|
constructor(el) {
|
55
|
-
|
55
|
+
this.el = el
|
56
|
+
const action_name = this.evaluateAction()
|
57
|
+
const result = this.evaluateCondition()
|
58
|
+
this.condition = result.condition
|
59
|
+
this.condition_arg = result.condition_arg
|
60
|
+
this.evaluateTarget(action_name)
|
61
|
+
}
|
62
|
+
|
63
|
+
apply() {
|
64
|
+
if (this.condition(this.el, this.condition_arg)) {
|
65
|
+
if (this.else_reverse_action) this.else_reverse_action(this.target, this.else_action_arg)
|
66
|
+
this.action(this.target, this.action_arg)
|
67
|
+
}
|
68
|
+
else {
|
69
|
+
if (this.reverse_action) this.reverse_action(this.target, this.action_arg)
|
70
|
+
if (this.else_action) this.else_action(this.target, this.else_action_arg)
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
evaluateAction() {
|
75
|
+
const action = this.el.data('then') || this.el.data('action') || ''
|
56
76
|
const action_name = action.split(' ', 1)[0]
|
57
|
-
const else_action = el.data('else') || ''
|
77
|
+
const else_action = this.el.data('else') || ''
|
58
78
|
const else_action_name = else_action.split(' ', 1)[0]
|
59
79
|
|
60
|
-
this.el = el
|
61
80
|
this.action = ACTIONS[action_name]
|
62
81
|
this.action_arg = action.substring(action.indexOf(' ') + 1)
|
63
82
|
this.reverse_action = REVERSE_ACTIONS[action_name]
|
64
83
|
this.else_action = ACTIONS[else_action_name]
|
65
84
|
this.else_action_arg = else_action.substring(else_action.indexOf(' ') + 1)
|
66
85
|
this.else_reverse_action = REVERSE_ACTIONS[else_action_name]
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
}
|
74
|
-
if (
|
75
|
-
|
76
|
-
}
|
77
|
-
if (
|
78
|
-
|
79
|
-
|
80
|
-
this.custom_function
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
el.attr('data-df-errors', 'custom function not found')
|
86
|
+
|
87
|
+
return action_name
|
88
|
+
}
|
89
|
+
|
90
|
+
evaluateCondition() {
|
91
|
+
let value = CONDITIONS[this.el.data('if')?.trim()]
|
92
|
+
if (value) return { condition: value }
|
93
|
+
if (value = this.el.data('eq')) return { condition: CONDITIONS['eq'], condition_arg: value }
|
94
|
+
if (value = this.el.data('not')) return { condition: CONDITIONS['not'], condition_arg: value }
|
95
|
+
if (value = this.el.data('match')) return { condition: CONDITIONS['match'], condition_arg: new RegExp(value) }
|
96
|
+
if (value = this.el.data('mismatch')) return { condition: CONDITIONS['mismatch'], condition_arg: new RegExp(value) }
|
97
|
+
|
98
|
+
this.custom_function = this.el.data('function')
|
99
|
+
if (this.custom_function) {
|
100
|
+
value = window[this.custom_function]
|
101
|
+
if (value) return { condition: value }
|
102
|
+
else {
|
103
|
+
this.el.attr('data-df-errors', 'custom function not found')
|
85
104
|
console.warn(`activeadmin_dynamic_fields custom function not found: ${this.custom_function}`)
|
86
105
|
}
|
87
106
|
}
|
88
107
|
|
89
|
-
|
90
|
-
if (el.data('target')) this.target = el.closest('fieldset').find(el.data('target'))
|
91
|
-
else if (el.data('gtarget')) this.target = $(el.data('gtarget'))
|
92
|
-
if (action_name == 'callback') this.target = el
|
108
|
+
return {}
|
93
109
|
}
|
94
110
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
else {
|
101
|
-
if (this.reverse_action) this.reverse_action(this.target, this.action_arg)
|
102
|
-
if (this.else_action) this.else_action(this.target, this.else_action_arg)
|
103
|
-
}
|
111
|
+
evaluateTarget(action_name) {
|
112
|
+
// closest find for has many associations
|
113
|
+
if (this.el.data('target')) this.target = this.el.closest('fieldset').find(this.el.data('target'))
|
114
|
+
else if (this.el.data('gtarget')) this.target = $(this.el.data('gtarget'))
|
115
|
+
if (action_name == 'callback') this.target = this.el
|
104
116
|
}
|
105
117
|
|
106
|
-
|
118
|
+
isValid() {
|
107
119
|
if (!this.condition) return false
|
108
120
|
if (!this.action && !this.custom_function) return false
|
109
121
|
|
@@ -111,9 +123,9 @@
|
|
111
123
|
}
|
112
124
|
|
113
125
|
setup() {
|
114
|
-
if (!this.
|
115
|
-
if (this.el.data('if') != 'changed') this.apply(
|
116
|
-
this.el.on('change', () => this.apply(
|
126
|
+
if (!this.isValid()) return
|
127
|
+
if (this.el.data('if') != 'changed') this.apply()
|
128
|
+
this.el.on('change', () => this.apply())
|
117
129
|
}
|
118
130
|
}
|
119
131
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeadmin_dynamic_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattia Roccoberton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activeadmin
|