activeadmin_dynamic_fields 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ee6e7b7330564161d89fc27dca1c94c33c17e24
4
+ data.tar.gz: 95dd1da212342bd8531f270426e5666403f691de
5
+ SHA512:
6
+ metadata.gz: b3c5c278abee8e3ce1befd06ddf91236a02467ee3a80e905942718ddd0f58896ad592ba098cf876def36b2f12b529e38fab13ea4e3ccb8b69a3e63644ebf1af3
7
+ data.tar.gz: 6c929053ca401f09a8901f97f81f23b0cfbfe93a8fe74933b5270054de7081e4757fd2d67f63d185de534996018a6cddce3cdad7a4e216bcbc4df8f038ae5be0
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ _misc/
2
+
3
+ *.orig
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2017 Mattia Roccoberton
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,149 @@
1
+ # ActiveAdmin Dynamic Fields [![Gem Version](https://badge.fury.io/rb/activeadmin_dynamic_fields.svg)](https://badge.fury.io/rb/activeadmin_dynamic_fields)
2
+
3
+ An Active Admin plugin to add dynamic behaviors to fields.
4
+
5
+ Features:
6
+
7
+ - set conditional checks on fields
8
+ - trigger some actions on other fields
9
+ - create links to load some content in a dialog
10
+
11
+ The easiest way to show how this plugin works is looking the examples (below).
12
+
13
+ ## Install
14
+
15
+ - Add to your Gemfile: `gem 'activeadmin_dynamic_fields'`
16
+ - Execute bundle
17
+ - Add at the end of your ActiveAdmin styles (_app/assets/stylesheets/active_admin.scss_):
18
+ `@import 'activeadmin/dynamic_fields';`
19
+ - Add at the end of your ActiveAdmin javascripts (_app/assets/javascripts/active_admin.js_):
20
+ `//= require activeadmin/dynamic_fields`
21
+
22
+ ## Options
23
+
24
+ Options are passed to fields using *input_html* parameter:
25
+
26
+ - **data-if**: check a condition, values:
27
+ + **checked**: check if a checkbox is checked
28
+ + **not_checked**: check if a checkbox is not checked
29
+ + **blank**: check if a field is blank
30
+ + **not_blank**: check if a field is not blank
31
+ - **data-eq**: check if a field has a specific value
32
+ - **data-not**: check if a field hasn't a specific value
33
+ - **data-function**: check the return value of a custom function
34
+ - **data-target**: target css selector
35
+ - **data-action**: the action to trigger, values:
36
+ + **hide**: hides elements
37
+ + **slide**: hides elements (using sliding)
38
+ + **fade**: hides elements (using fading)
39
+ + **setValue**: set a value
40
+ + **callback**: call a function
41
+ - **data-value**: value to set
42
+ - **data-callback**: custom function for setting a value
43
+ - **data-arg**: argument passed to the custom set function
44
+
45
+ ## Examples of dynamic fields
46
+
47
+ - A checkbox that hides other fields if false (ex. model *Article*):
48
+
49
+ ```rb
50
+ form do |f|
51
+ f.inputs 'Article' do
52
+ f.input :published, input_html: { 'data-if': 'not_checked', 'data-action': 'hide', 'data-target': '.grp1' }
53
+ f.input :online_date, wrapper_html: { class: 'grp1' }
54
+ f.input :position, wrapper_html: { class: 'grp1' }
55
+ end
56
+ f.actions
57
+ end
58
+ ```
59
+
60
+ - Set another field value if a string field is blank:
61
+
62
+ `f.input :title, input_html: { 'data-if': 'blank', 'data-action': 'setValue', 'data-target': '#article_position', 'data-value': '10' }`
63
+
64
+ - Use a custom function for conditional check (*title_not_empty()* must be available on global scope):
65
+
66
+ `f.input :title, input_html: { 'data-function': 'title_empty', 'data-action': 'slide', 'data-target': '#article_description_input' }`
67
+
68
+ ```js
69
+ function title_empty( el ) {
70
+ return ( $('#article_title').val().trim() === '' );
71
+ }
72
+ ```
73
+
74
+ - Call a callback function as action:
75
+
76
+ `f.input :published, input_html: { 'data-if': 'checked', 'data-action': 'callback', 'data-callback': 'set_title', 'data-args': '["[unpublished]"]' }`
77
+
78
+ ```js
79
+ function set_title( args ) {
80
+ if( $('#article_title').val().trim() === '' ) {
81
+ $('#article_title').val( args[0] );
82
+ $('#article_title').trigger( 'change' );
83
+ }
84
+ }
85
+ ```
86
+
87
+ - Custom function without action:
88
+
89
+ `f2.input :category, as: :select, collection: [ [ 'Cat 1', 'cat1' ], [ 'Cat 2', 'cat2' ], [ 'Cat 3', 'cat3' ] ], input_html: { 'data-function': 'on_change_category' }`
90
+
91
+ ```js
92
+ function on_change_category( el ) {
93
+ var target = el.closest( 'fieldset' ).find( '.pub' );
94
+ target.prop( 'checked', ( el.val() == 'cat2' ) );
95
+ target.trigger( 'change' );
96
+ }
97
+ ```
98
+
99
+ ## Example to open a dialog
100
+
101
+ Example with 2 models: *Author* and *Article*
102
+
103
+ Prepare the content dialog - in Active Admin Author config:
104
+
105
+ ```rb
106
+ ActiveAdmin.register Author do
107
+ # ...
108
+ member_action :dialog do
109
+ content = '<dl>'
110
+ [:name, :age, :created_at].each do |field|
111
+ content += "<dt>#{Author.human_attribute_name(field)}:</dt><dd>#{resource[field]}</dd>"
112
+ end
113
+ content += '</dl>'
114
+ render plain: content
115
+ end
116
+ # ...
117
+ end
118
+ ```
119
+
120
+ Add a link to show the dialog - in Active Admin Article config:
121
+
122
+ ```rb
123
+ ActiveAdmin.register Article do
124
+ # ...
125
+ show do |object|
126
+ attributes_table do
127
+ # ...
128
+ row :author do
129
+ link_to object.author.name, dialog_admin_author_path( object.author ), title: object.author.name, 'data-df-dialog': true, 'data-df-icon': true
130
+ end
131
+ end
132
+ end
133
+ # ...
134
+ end
135
+ ```
136
+
137
+ The link url is loaded via AJAX before opening the dialog.
138
+
139
+ ## Do you like it? Star it!
140
+
141
+ If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
142
+
143
+ ## Contributors
144
+
145
+ - [Mattia Roccoberton](http://blocknot.es) - creator, maintainer
146
+
147
+ ## License
148
+
149
+ [MIT](LICENSE.txt)
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'activeadmin/dynamic_fields/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'activeadmin_dynamic_fields'
7
+ spec.version = ActiveAdmin::DynamicFields::VERSION
8
+ spec.summary = 'Dynamic fields for ActiveAdmin'
9
+ spec.description = 'An Active Admin plugin to add dynamic behaviors to fields'
10
+ spec.license = 'MIT'
11
+ spec.authors = ['Mattia Roccoberton']
12
+ spec.email = 'mat@blocknot.es'
13
+ spec.homepage = 'https://github.com/blocknotes/activeadmin_dynamic_fields'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_runtime_dependency 'activeadmin', '~> 1.0'
19
+ end
@@ -0,0 +1,114 @@
1
+
2
+ function dfEvalCondition( el, args ) {
3
+ if( args.fn ) {
4
+ if( args.fn && window[args.fn] ) return !window[args.fn]( el );
5
+ else console.log( 'Warning - activeadmin_dynamic_fields: ' + args.fn + '() not available [1]' );
6
+ }
7
+ else if( args.if == 'checked' ) {
8
+ return !el.is(':checked');
9
+ }
10
+ else if( args.if == 'not_checked' ) {
11
+ return el.is(':checked');
12
+ }
13
+ else if( args.if == 'blank' ) {
14
+ return el.val().length === 0 || !el.val().trim();
15
+ }
16
+ else if( args.if == 'not_blank' ) {
17
+ return el.val().length !== 0 && el.val().trim();
18
+ }
19
+ else if( args.eq ) {
20
+ return el.val() == args.eq;
21
+ }
22
+ else if( args.not ) {
23
+ return el.val() != args.not;
24
+ }
25
+ return undefined;
26
+ }
27
+
28
+ function dfSetupField( el ) {
29
+ var action = el.data( 'action' );
30
+ var target, args = {};
31
+ args.if = el.data( 'if' );
32
+ args.eq = el.data( 'eq' );
33
+ args.not = el.data( 'not' );
34
+ args.fn = el.data( 'function' );
35
+ if( el.data( 'target' ) ) target = el.closest( 'fieldset' ).find( el.data( 'target' ) )
36
+ if( action == 'hide' ) {
37
+ if( dfEvalCondition( el, args ) ) target.show();
38
+ else target.hide();
39
+ el.on( 'change', function( event ) {
40
+ if( dfEvalCondition( $(this), args ) ) target.show();
41
+ else target.hide();
42
+ });
43
+ }
44
+ else if( action == 'slide' ) {
45
+ if( dfEvalCondition( el, args ) ) target.slideDown();
46
+ else target.slideUp();
47
+ el.on( 'change', function( event ) {
48
+ if( dfEvalCondition( $(this), args ) ) target.slideDown();
49
+ else target.slideUp();
50
+ });
51
+ }
52
+ else if( action == 'fade' ) {
53
+ if( dfEvalCondition( el, args ) ) target.fadeIn();
54
+ else target.fadeOut();
55
+ el.on( 'change', function( event ) {
56
+ if( dfEvalCondition( $(this), args ) ) target.fadeIn();
57
+ else target.fadeOut();
58
+ });
59
+ }
60
+ else if( action == 'setValue' ) {
61
+ var val = el.data( 'value' ) ? el.data( 'value' ) : '';
62
+ if( dfEvalCondition( el, args ) ) dfSetValue( target, val );
63
+ el.on( 'change', function( event ) {
64
+ if( dfEvalCondition( $(this), args ) ) dfSetValue( target, val );
65
+ });
66
+ }
67
+ else if( action == 'callback' ) {
68
+ var cb = el.data( 'callback' );
69
+ if( cb && window[cb] ) {
70
+ if( dfEvalCondition( el, args ) ) window[cb]( el.data( 'args' ) );
71
+ el.on( 'change', function( event ) {
72
+ if( dfEvalCondition( $(this), args ) ) window[cb]( el.data( 'args' ) );
73
+ });
74
+ }
75
+ else console.log( 'Warning - activeadmin_dynamic_fields: ' + cb + '() not available [2]' );
76
+ }
77
+ else if( args.fn ) { // function without action
78
+ dfEvalCondition( el, args );
79
+ el.on( 'change', function( event ) {
80
+ dfEvalCondition( el, args );
81
+ });
82
+ }
83
+ }
84
+
85
+ function dfSetValue( el, val ) {
86
+ if( el.attr('type') != 'checkbox' ) el.val( val );
87
+ else el.prop('checked', val == '1');
88
+ el.trigger( 'change' );
89
+ }
90
+
91
+ $(document).ready( function() {
92
+ $('[data-df-dialog]').on( 'click', function( event ) {
93
+ event.preventDefault();
94
+ if( $('#df-dialog').length == 0 ) $('body').append( '<div id="df-dialog"></div>' );
95
+ var title = $(this).attr( 'title' );
96
+ $.ajax({
97
+ url: $(this).attr( 'href' )
98
+ }).done( function( result ) {
99
+ if( title ) $('#df-dialog').attr( 'title', title );
100
+ $('#df-dialog').html( result );
101
+ $('#df-dialog').dialog({ modal: true });
102
+ });
103
+ });
104
+
105
+ $('[data-if], [data-function], [data-eq], [data-not]').each( function() {
106
+ dfSetupField( $(this) );
107
+ });
108
+
109
+ $('.has_many_container').on( 'has_many_add:after', function( e, fieldset, container ) {
110
+ $('[data-if], [data-function], [data-eq], [data-not]').each( function() {
111
+ dfSetupField( $(this) );
112
+ });
113
+ });
114
+ });
@@ -0,0 +1,13 @@
1
+ body.active_admin
2
+ a[data-df-icon]::after
3
+ content: ' •'
4
+ // .ui-dialog
5
+ // .ui-dialog-titlebar
6
+ // display: flex
7
+ // justify-content: space-between
8
+ // > *
9
+ // align-self: center
10
+ // // .ui-dialog-buttonset
11
+ // // text-align: right
12
+ #df-dialog
13
+ padding: 10px 15px 5px 15px
@@ -0,0 +1,9 @@
1
+ require 'active_admin'
2
+
3
+ module ActiveAdmin
4
+ module DynamicFields
5
+ class Engine < ::Rails::Engine
6
+ engine_name 'activeadmin_dynamic_fields'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveAdmin
2
+ module DynamicFields
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'activeadmin/dynamic_fields/engine'
@@ -0,0 +1 @@
1
+ require 'activeadmin/dynamic_fields'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeadmin_dynamic_fields
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mattia Roccoberton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activeadmin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: An Active Admin plugin to add dynamic behaviors to fields
28
+ email: mat@blocknot.es
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - Gemfile
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - activeadmin_dynamic_fields.gemspec
39
+ - app/assets/javascripts/activeadmin/dynamic_fields.js
40
+ - app/assets/stylesheets/activeadmin/_dynamic_fields.sass
41
+ - lib/activeadmin/dynamic_fields.rb
42
+ - lib/activeadmin/dynamic_fields/engine.rb
43
+ - lib/activeadmin/dynamic_fields/version.rb
44
+ - lib/activeadmin_dynamic_fields.rb
45
+ homepage: https://github.com/blocknotes/activeadmin_dynamic_fields
46
+ licenses:
47
+ - MIT
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.6.13
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Dynamic fields for ActiveAdmin
69
+ test_files: []