Skip to content

Commit 96c7d92

Browse files
committed
Add 'data-then'; set 'data-action' as alias of 'data-then'
1 parent 762c37a commit 96c7d92

File tree

4 files changed

+13
-22
lines changed

4 files changed

+13
-22
lines changed

README.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
An Active Admin plugin to add dynamic behaviors to fields.
44

55
Features:
6-
76
- set conditional checks on fields
87
- trigger some actions on other fields
98
- inline field editing
@@ -12,16 +11,13 @@ Features:
1211
The easiest way to show how this plugin works is looking the examples [below](#examples).
1312

1413
## Install
15-
1614
- Add to your Gemfile: `gem 'activeadmin_dynamic_fields'`
1715
- Execute bundle
1816
- Add at the end of your ActiveAdmin javascripts (_app/assets/javascripts/active_admin.js_):
1917
`//= require activeadmin/dynamic_fields`
2018

2119
## Options
22-
2320
Options are passed to fields using *input_html* parameter as *data* attributes:
24-
2521
- **data-if**: check a condition, values:
2622
+ **checked**: check if a checkbox is checked
2723
+ **not_checked**: check if a checkbox is not checked
@@ -32,7 +28,7 @@ Options are passed to fields using *input_html* parameter as *data* attributes:
3228
- **data-not**: check if a field hasn't a specific value
3329
- **data-target**: target css selector (from parent fieldset, look for the closest match)
3430
- **data-gtarget**: target css selector globally
35-
- **data-action**: the action to trigger, values:
31+
- **data-then**: the action to trigger (alias **data-action**), values:
3632
+ **hide**: hides elements
3733
+ **slide**: hides elements (using sliding)
3834
+ **fade**: hides elements (using fading)
@@ -45,13 +41,12 @@ Options are passed to fields using *input_html* parameter as *data* attributes:
4541
## Examples
4642

4743
### Dynamic fields examples
48-
4944
- A checkbox that hides other fields if is checked (ex. model *Article*):
5045

5146
```rb
5247
form do |f|
5348
f.inputs 'Article' do
54-
f.input :published, input_html: { data: { if: 'checked', action: 'hide', target: '.grp1' } }
49+
f.input :published, input_html: { data: { if: 'checked', then: 'hide', target: '.grp1' } }
5550
f.input :online_date, wrapper_html: { class: 'grp1' }
5651
f.input :draft_notes, wrapper_html: { class: 'grp1' }
5752
end
@@ -61,15 +56,15 @@ end
6156

6257
- Add 3 classes (*first*, *second*, *third*) if a checkbox is not checked:
6358

64-
`f.input :published, input_html: { data: { if: 'not_checked', action: 'addClass first second third', target: '.grp1' } }`
59+
`f.input :published, input_html: { data: { if: 'not_checked', then: 'addClass first second third', target: '.grp1' } }`
6560

6661
- Set another field value if a string field is blank:
6762

68-
`f.input :title, input_html: { data: { if: 'blank', action: 'setValue 10', target: '#article_position' } }`
63+
`f.input :title, input_html: { data: { if: 'blank', then: 'setValue 10', target: '#article_position' } }`
6964

7065
- Use a custom function for conditional check (*title_not_empty()* must be available on global scope) (with alternative syntax for data attributes):
7166

72-
`f.input :title, input_html: { 'data-function': 'title_empty', 'data-action': 'slide', 'data-target': '#article_description_input' }`
67+
`f.input :title, input_html: { 'data-function': 'title_empty', 'data-then': 'slide', 'data-target': '#article_description_input' }`
7368

7469
```js
7570
function title_empty( el ) {
@@ -79,7 +74,7 @@ function title_empty( el ) {
7974

8075
- Call a callback function as action:
8176

82-
`f.input :published, input_html: { data: { if: 'checked', action: 'callback set_title', args: '["Unpublished !"]' } }`
77+
`f.input :published, input_html: { data: { if: 'checked', then: 'callback set_title', args: '["Unpublished !"]' } }`
8378

8479
```js
8580
function set_title( args ) {
@@ -103,7 +98,6 @@ function on_change_category( el ) {
10398
```
10499

105100
### Inline editing examples
106-
107101
- Prepare a custom member action to save data, an *update* helper function is available (third parameter is optional, allow to filter using strong parameters):
108102

109103
```rb
@@ -141,7 +135,6 @@ end
141135
```
142136

143137
### Dialog example
144-
145138
Example with 2 models: *Author* and *Article*
146139

147140
Prepare the content dialog - in Active Admin Author config:
@@ -185,15 +178,13 @@ end
185178
The link url is loaded via AJAX before opening the dialog.
186179

187180
## Do you like it? Star it!
188-
189181
If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
190182

191183
Take a look at [other ActiveAdmin components](https://github.com/blocknotes?utf8=✓&tab=repositories&q=activeadmin&type=source) that I made if you are curious.
192184

193185
## Contributors
194-
195186
- [Mattia Roccoberton](http://blocknot.es): author
187+
- The good guys that opened issues and pull requests from time to time
196188

197189
## License
198-
199-
[MIT](LICENSE.txt)
190+
The gem is available as open-source under the terms of the [MIT](LICENSE.txt).

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
// Prepare a field
3636
function dfSetupField(el) {
37-
let action = el.data('action');
37+
let action = el.data('then') || el.data('action');
3838
let target, args = {};
3939

4040
args.if = el.data('if');

spec/dummy/app/admin/posts.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
f.inputs 'Post' do
4242
f.input :author
4343
f.input :title
44-
f.input :description, input_html: { data: { if: 'blank', action: 'setValue no title', target: '#post_category' } }
44+
f.input :description, input_html: { data: { if: 'blank', then: 'setValue no title', target: '#post_category' } }
4545
f.input :category
46-
f.input :published, input_html: { data: { if: 'not_checked', action: 'hide', target: '.group1' } }
46+
f.input :published, input_html: { data: { if: 'not_checked', then: 'hide', target: '.group1' } }
4747
f.input :dt, wrapper_html: { class: 'group1' }
4848
f.input :position, wrapper_html: { class: 'group1' }
4949
end

spec/system/dynamic_fields_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
it 'toggles the .group1 elements when clicking on the checkbox' do
1818
visit "/admin/posts/#{post.id}/edit"
1919

20-
expect(page).to have_css('#post_published[data-if="not_checked"][data-action="hide"][data-target=".group1"]')
20+
expect(page).to have_css('#post_published[data-if="not_checked"][data-then="hide"][data-target=".group1"]')
2121

2222
expect(find('#post_published')).not_to be_checked
2323
expect(page).to have_css('#post_dt_input', visible: :hidden)
@@ -36,7 +36,7 @@
3636
visit "/admin/posts/#{post.id}/edit"
3737

3838
expect(page).to have_css(
39-
'#post_description[data-if="blank"][data-action="setValue no title"][data-target="#post_category"]'
39+
'#post_description[data-if="blank"][data-then="setValue no title"][data-target="#post_category"]'
4040
)
4141
expect(find('#post_category').value).to eq 'no title'
4242
find('#post_category').set('...')

0 commit comments

Comments
 (0)