Skip to content

Commit a7ccc6a

Browse files
committed
New feature: add match/mismatch condition checks
1 parent c937c6f commit a7ccc6a

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Conditions:
3131
+ **changed**: check if the value of an input is changed (dirty)
3232
- **data-eq**: check if a field has a specific value (ex. `"data-eq": "42"`)
3333
- **data-not**: check if a field has not a specific value
34+
- **data-match**: check if a field match a regexp
35+
- **data-mismatch**: check if a field doesn't match a regexp (ex. `"data-mismatch": "^\d+$"`)
3436
- **data-function**: check the return value of a custom function (ex. `"data-function": "my_check"`)
3537

3638
Actions:

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
changed: _el => true,
3434
checked: el => el.is(':checked'),
3535
eq: (el, value) => el.val() == value,
36+
match: (el, regexp) => regexp.test(el.val()),
37+
mismatch: (el, regexp) => !regexp.test(el.val()),
3638
not: (el, value) => el.val() != value,
3739
not_blank: el => el.val().trim(),
3840
not_checked: el => !el.is(':checked')
@@ -69,6 +71,12 @@
6971
if (!this.condition && el.data('not')) {
7072
[this.condition, this.condition_arg] = [CONDITIONS['not'], el.data('not')]
7173
}
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+
}
7280
this.custom_function = el.data('function')
7381
if (!this.condition && this.custom_function) {
7482
this.condition = window[this.custom_function]
@@ -164,7 +172,7 @@
164172
// Init
165173
$(document).ready(function () {
166174
// Setup dynamic fields
167-
const selectors = '.active_admin .input [data-if], .active_admin .input [data-eq], .active_admin .input [data-not], .active_admin .input [data-function]'
175+
const selectors = '.active_admin .input [data-if], .active_admin .input [data-eq], .active_admin .input [data-not], .active_admin .input [data-match], .active_admin .input [data-mismatch], .active_admin .input [data-function]'
168176
$(selectors).each(function () {
169177
new Field($(this)).setup()
170178
})

spec/dummy/app/admin/posts.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,22 @@ def add_field(form, name, type, data, override_options = {}, extra_attrs = {})
8989
add_field(f, :data_field_163, :text, df163)
9090

9191
# --- not
92-
df181 = { not: '181', then: 'addClass red', target: '#post_data_field_181_input label' }
93-
add_field(f, :data_field_181, :string, df181)
92+
df171 = { not: '171', then: 'addClass red', target: '#post_data_field_171_input label' }
93+
add_field(f, :data_field_171, :string, df171)
94+
95+
df172 = { not: '172', then: 'addClass red', target: '#post_data_field_172_input label' }
96+
add_field(f, :data_field_172, :select, df172, collection: [171, 172, 173])
9497

95-
df182 = { not: '182', then: 'addClass red', target: '#post_data_field_182_input label' }
96-
add_field(f, :data_field_182, :select, df182, collection: [181, 182, 183])
98+
df173 = { not: '173', then: 'addClass red', target: '#post_data_field_173_input label' }
99+
add_field(f, :data_field_173, :text, df173)
100+
101+
# --- match
102+
df181 = { match: 'Something\s', then: 'addClass red', target: '#post_data_field_181_input label' }
103+
add_field(f, :data_field_181, :string, df181)
97104

98-
df183 = { not: '183', then: 'addClass red', target: '#post_data_field_183_input label' }
99-
add_field(f, :data_field_183, :text, df183)
105+
# --- mismatch
106+
df191 = { mismatch: '^\d+$', then: 'addClass red', target: '#post_data_field_191_input label' }
107+
add_field(f, :data_field_191, :string, df191)
100108

101109
# --- function
102110
df201 = { function: 'test_fun', then: 'addClass red', target: '#post_data_field_201_input label' }

spec/system/dynamic_fields_spec.rb

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,31 @@
7373
expect(page).to have_css('#post_data_field_163_input label.red')
7474

7575
# --- not
76-
expect(page).to have_css('#post_data_field_181_input label.red')
77-
fill_in('post_data_field_181', with: '181')
76+
expect(page).to have_css('#post_data_field_171_input label.red')
77+
fill_in('post_data_field_171', with: '171')
7878
find('body').click
79-
expect(page).not_to have_css('#post_data_field_181_input label.red')
79+
expect(page).not_to have_css('#post_data_field_171_input label.red')
80+
81+
expect(page).to have_css('#post_data_field_172_input label.red')
82+
select('172', from: 'post_data_field_172')
83+
expect(page).not_to have_css('#post_data_field_172_input label.red')
84+
85+
expect(page).to have_css('#post_data_field_173_input label.red')
86+
fill_in('post_data_field_173', with: '173')
87+
find('body').click
88+
expect(page).not_to have_css('#post_data_field_173_input label.red')
8089

81-
expect(page).to have_css('#post_data_field_182_input label.red')
82-
select('182', from: 'post_data_field_182')
83-
expect(page).not_to have_css('#post_data_field_182_input label.red')
90+
# --- match
91+
expect(page).not_to have_css('#post_data_field_181_input label.red')
92+
fill_in('post_data_field_181', with: ' Something new ...')
93+
find('body').click
94+
expect(page).to have_css('#post_data_field_181_input label.red')
8495

85-
expect(page).to have_css('#post_data_field_183_input label.red')
86-
fill_in('post_data_field_183', with: '183')
96+
# --- mismatch
97+
expect(page).to have_css('#post_data_field_191_input label.red')
98+
fill_in('post_data_field_191', with: '1234')
8799
find('body').click
88-
expect(page).not_to have_css('#post_data_field_183_input label.red')
100+
expect(page).not_to have_css('#post_data_field_191_input label.red')
89101

90102
# --- function
91103
expect(page).not_to have_css('#post_data_field_201_input label.red')

0 commit comments

Comments
 (0)