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: e095e90e8a67093d377cd4ba64763adead3eaf7f100e7e27a3a6ab60c2851763
4
- data.tar.gz: ee22043cb52eea432818add1114b5e08f8b91deb450845c480ee289794f72809
3
+ metadata.gz: '019f2b81ed1552f13f57b73141860957f4869b10de1f018a289aa8724cc23ec1'
4
+ data.tar.gz: 4ed1bb60f5b1ddef1a3d870296d8548faa18cc36db5adadb8271f0c655c769f6
5
5
  SHA512:
6
- metadata.gz: 116395a999d86f0f976a04cd7be8ba1ee994ce6bcb71c21d735e621ed037881aa24b73a57fa75e7be8062acfb6ea178e935da0e03b9d0c82f29ec8f73b7afff5
7
- data.tar.gz: 6d7659ba3297a2f218829678bd24ea10a62703ed294822dc94ac5591671f92073d496e9d7432b6c7b80da25bd70238490e4285ef31e42c21e5a60d991c5a8448
6
+ metadata.gz: b07cc4e73b801ef79ee52b52a6c82117d7e966a2f1eadb0e415e00c608456c02f2c14694c2fc41037ad381c9c0c3bfbf1c0ed96c0c6d13bd0ce3335c38dc1e11
7
+ data.tar.gz: c0aa33df12ef6ed16927a49bb9b93d0fcffeffc6f20b661f11edb350544f12e550f90fd5fa5870a43154c0097995bbb02f2f80f24e1ff821eddc2d0fa60e613d
@@ -52,58 +52,70 @@
52
52
 
53
53
  class Field {
54
54
  constructor(el) {
55
- const action = el.data('then') || el.data('action') || ''
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
- this.condition = CONDITIONS[el.data('if')]
68
- if (!this.condition && el.data('eq')) {
69
- [this.condition, this.condition_arg] = [CONDITIONS['eq'], el.data('eq')]
70
- }
71
- if (!this.condition && el.data('not')) {
72
- [this.condition, this.condition_arg] = [CONDITIONS['not'], el.data('not')]
73
- }
74
- if (!this.condition && el.data('match')) {
75
- [this.condition, this.condition_arg] = [CONDITIONS['match'], new RegExp(el.data('match'))]
76
- }
77
- if (!this.condition && el.data('mismatch')) {
78
- [this.condition, this.condition_arg] = [CONDITIONS['mismatch'], new RegExp(el.data('mismatch'))]
79
- }
80
- this.custom_function = el.data('function')
81
- if (!this.condition && this.custom_function) {
82
- this.condition = window[this.custom_function]
83
- if (!this.condition) {
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
- // closest find for has many associations
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
- apply(el) {
96
- if (this.condition(el, this.condition_arg)) {
97
- if (this.else_reverse_action) this.else_reverse_action(this.target, this.else_action_arg)
98
- this.action(this.target, this.action_arg)
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
- is_valid() {
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.is_valid()) return
115
- if (this.el.data('if') != 'changed') this.apply(this.el)
116
- this.el.on('change', () => this.apply(this.el))
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
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveAdmin
4
4
  module DynamicFields
5
- VERSION = '0.4.4'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
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.4
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-09-26 00:00:00.000000000 Z
11
+ date: 2020-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeadmin