Skip to content

Commit 4bc3e5e

Browse files
blocknotesMattia Roccoberton
authored andcommitted
Not operator
1 parent f65ff86 commit 4bc3e5e

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ Conditions:
4747

4848
- **data-if**: check a condition, values:
4949
+ **checked**: check if a checkbox is checked (ex. `"data-if": "checked"`)
50-
+ **not_checked**: check if a checkbox is not checked
50+
+ **not_checked**: check if a checkbox is not checked (equivalent to `"data-if": "!checked"`)
5151
+ **blank**: check if a field is blank
5252
+ **not_blank**: check if a field is not blank
5353
+ **changed**: check if the value of an input is changed (dirty)
54-
- **data-eq**: check if a field has a specific value (ex. `"data-eq": "42"`)
55-
- **data-not**: check if a field has not a specific value
54+
- **data-eq**: check if a field has a specific value (ex. `"data-eq": "42"` or `"data-eq": "!5"`)
55+
- **data-not**: check if a field has not a specific value (equivalent to `"data-eq": "!something"`)
5656
- **data-match**: check if a field match a regexp
5757
- **data-mismatch**: check if a field doesn't match a regexp (ex. `"data-mismatch": "^\d+$"`)
5858
- **data-function**: check the return value of a custom function (ex. `"data-function": "my_check"`)

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
match: (el, regexp) => regexp.test(el.val()),
4040
mismatch: (el, regexp) => !regexp.test(el.val()),
4141
not: (el, value) => el.val() != value,
42-
not_blank: el => el.val().trim(),
43-
not_checked: el => !el.is(':checked')
42+
not_blank: el => !CONDITIONS.blank(el),
43+
not_checked: el => !CONDITIONS.checked(el)
4444
}
4545

4646
const REVERSE_ACTIONS = {
@@ -53,6 +53,8 @@
5353
slide: el => el.slideDown()
5454
}
5555

56+
const REGEXP_NOT = /^!\s*/
57+
5658
class Field {
5759
constructor(el) {
5860
this.el = el
@@ -91,21 +93,29 @@
9193
}
9294

9395
evaluateCondition() {
94-
let data_if = this.el.data('if')
95-
let value = data_if ? CONDITIONS[data_if.trim()] : null
96-
if (value) return { condition: value }
97-
98-
value = this.el.data('eq')
99-
if (value) return { condition: CONDITIONS['eq'], condition_arg: value }
100-
101-
value = this.el.data('not')
102-
if (value) return { condition: CONDITIONS['not'], condition_arg: value }
103-
104-
value = this.el.data('match')
105-
if (value) return { condition: CONDITIONS['match'], condition_arg: new RegExp(value) }
106-
107-
value = this.el.data('mismatch')
108-
if (value) return { condition: CONDITIONS['mismatch'], condition_arg: new RegExp(value) }
96+
let value
97+
if (value = this.el.data('if')) {
98+
if (REGEXP_NOT.test(value)) value = 'not_' + value.replace(REGEXP_NOT, '')
99+
return { condition: CONDITIONS[value] }
100+
}
101+
if (value = this.el.data('eq')) {
102+
if (REGEXP_NOT.test(value)) {
103+
return { condition: CONDITIONS['not'], condition_arg: value.replace(REGEXP_NOT, '') }
104+
}
105+
return { condition: CONDITIONS['eq'], condition_arg: value }
106+
}
107+
if (value = this.el.data('not')) {
108+
if (REGEXP_NOT.test(value)) {
109+
return { condition: CONDITIONS['eq'], condition_arg: value.replace(REGEXP_NOT, '') }
110+
}
111+
return { condition: CONDITIONS['not'], condition_arg: value }
112+
}
113+
if (value = this.el.data('match')) {
114+
return { condition: CONDITIONS['match'], condition_arg: new RegExp(value) }
115+
}
116+
if (value = this.el.data('mismatch')) {
117+
return { condition: CONDITIONS['mismatch'], condition_arg: new RegExp(value) }
118+
}
109119

110120
this.custom_function = this.el.data('function')
111121
if (this.custom_function) {

spec/dummy/app/admin/posts.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
ActiveAdmin.register Post do # rubocop:disable Metrics/BlockLength
3+
ActiveAdmin.register Post do
44
permit_params :author_id, :title, :description, :category, :dt, :position, :published, tag_ids: []
55

66
member_action :save, method: [:post] do
@@ -54,6 +54,9 @@ def add_field(form, name, type, data, override_options = {}, extra_attrs = {})
5454
df111 = { if: 'checked', then: 'addClass red', target: '#post_data_field_111_input label' }
5555
add_field(f, :data_field_111, :boolean, df111)
5656

57+
df112 = { if: '!checked', then: 'addClass red', target: '#post_data_field_112_input label' }
58+
add_field(f, :data_field_112, :boolean, df112)
59+
5760
df121 = { if: 'not_checked', then: 'addClass red', target: '#post_data_field_121_input label' }
5861
add_field(f, :data_field_121, :boolean, df121)
5962

@@ -88,6 +91,9 @@ def add_field(form, name, type, data, override_options = {}, extra_attrs = {})
8891
df163 = { eq: '163', then: 'addClass red', target: '#post_data_field_163_input label' }
8992
add_field(f, :data_field_163, :text, df163)
9093

94+
df164 = { eq: '!164', then: 'addClass red', target: '#post_data_field_164_input label' }
95+
add_field(f, :data_field_164, :string, df164)
96+
9197
# --- not
9298
df171 = { not: '171', then: 'addClass red', target: '#post_data_field_171_input label' }
9399
add_field(f, :data_field_171, :string, df171)

spec/system/dynamic_fields_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_change_css(target, attrs1, attrs2, options = {})
7474
# --- if
7575
spec_message('check data-if condition')
7676
test_set_css('#post_data_field_111_input label.red', action: [:click, '#post_data_field_111'])
77-
# test_unset_css('#post_data_field_112_input label.red', action: [:click, '#post_data_field_112'])
77+
test_unset_css('#post_data_field_112_input label.red', action: [:click, '#post_data_field_112'])
7878
test_unset_css('#post_data_field_121_input label.red', action: [:click, '#post_data_field_121'])
7979
test_unset_css('#post_data_field_131_input label.red', action: [:fill, 'post_data_field_131', 'something'])
8080
test_unset_css('#post_data_field_132_input label.red', action: [:fill, 'post_data_field_132', 'something'])
@@ -91,7 +91,7 @@ def test_change_css(target, attrs1, attrs2, options = {})
9191
test_set_css('#post_data_field_161_input label.red', action: [:fill, 'post_data_field_161', '161'])
9292
test_set_css('#post_data_field_162_input label.red', action: [:select, 'post_data_field_162', '162'])
9393
test_set_css('#post_data_field_163_input label.red', action: [:fill, 'post_data_field_163', '163'])
94-
# test_unset_css('#post_data_field_164_input label.red', action: [:fill, 'post_data_field_164', '164'])
94+
test_unset_css('#post_data_field_164_input label.red', action: [:fill, 'post_data_field_164', '164'])
9595

9696
# --- not
9797
spec_message('check data-not condition')

0 commit comments

Comments
 (0)