README.md in activeadmin_dynamic_fields-0.5.2 vs README.md in activeadmin_dynamic_fields-0.6.0

- old
+ new

@@ -1,30 +1,51 @@ -# ActiveAdmin Dynamic Fields [![Gem Version](https://badge.fury.io/rb/activeadmin_dynamic_fields.svg)](https://badge.fury.io/rb/activeadmin_dynamic_fields) [![CircleCI](https://circleci.com/gh/blocknotes/activeadmin_dynamic_fields.svg?style=svg)](https://circleci.com/gh/blocknotes/activeadmin_dynamic_fields) +# ActiveAdmin Dynamic Fields [![Gem Version](https://badge.fury.io/rb/activeadmin_dynamic_fields.svg)](https://badge.fury.io/rb/activeadmin_dynamic_fields) [![Gem downloads](https://badgen.net/rubygems/dt/activeadmin_dynamic_fields)](https://rubygems.org/gems/activeadmin_dynamic_fields) [![Specs](https://github.com/blocknotes/activeadmin_dynamic_fields/actions/workflows/specs.yml/badge.svg)](https://github.com/blocknotes/activeadmin_dynamic_fields/actions/workflows/specs.yml) An Active Admin plugin to add dynamic behaviors to some fields. Features: + - set conditional checks on fields - trigger actions on target elements - inline field editing - create links to load some content in a dialog The easiest way to show how this plugin works is looking the examples [below](#examples). +Please :star: if you like it. + ## Install -- Add to your Gemfile: `gem 'activeadmin_dynamic_fields'` -- Execute bundle -- Add at the end of your ActiveAdmin javascripts (_app/assets/javascripts/active_admin.js_): +First, add the gem to your ActiveAdmin project: `gem 'activeadmin_dynamic_fields'` (and execute `bundle`) + +If you installed Active Admin **without Webpacker** support: +- add at the end of your ActiveAdmin javascripts (_app/assets/javascripts/active_admin.js_): + ```js //= require activeadmin/dynamic_fields ``` +Otherwise **with Webpacker**: + +- Execute in your project root: + +```sh +yarn add blocknotes/activeadmin_dynamic_fields +``` + +- Add to your *app/javascript/packs/active_admin.js*: + +```js +require('activeadmin_dynamic_fields') +``` + ## Options + Options are passed to fields using *input_html* parameter as *data* attributes. Conditions: + - **data-if**: check a condition, values: + **checked**: check if a checkbox is checked (ex. `"data-if": "checked"`) + **not_checked**: check if a checkbox is not checked + **blank**: check if a field is blank + **not_blank**: check if a field is not blank @@ -34,10 +55,11 @@ - **data-match**: check if a field match a regexp - **data-mismatch**: check if a field doesn't match a regexp (ex. `"data-mismatch": "^\d+$"`) - **data-function**: check the return value of a custom function (ex. `"data-function": "my_check"`) Actions: + - **data-then**: action to trigger (alias **data-action**), values: + **hide**: hides elements (ex. `"data-then": "hide", "data-target": ".errors"`) + **slide**: hides elements (using sliding) + **fade**: hides elements (using fading) + **addClass**: adds classes (ex. `"data-then": "addClass red"`) @@ -47,18 +69,20 @@ + **callback**: call a function (with arguments: **data-args**) (ex. `"data-then": "callback a_fun"`) - **data-else**: action to trigger when the condition check is not true - **data-args**: arguments passed to the callback function Targets: + - **data-target**: target css selector (from parent fieldset, look for the closest match) - **data-gtarget**: target css selector globally A check condition or a custom check function are required. A trigger action is required too, unless you are using a custom function (in that case it is optional). ## Examples ### Dynamic fields examples + - A checkbox that hides other fields if is checked (ex. model *Article*): ```rb form do |f| f.inputs 'Article' do @@ -126,10 +150,11 @@ target.trigger('change'); } ``` ### Inline editing examples + - Prepare a custom member action to save data, an *update* helper function is available (third parameter is optional, allow to filter using strong parameters): ```rb member_action :save, method: [:post] do render ActiveAdmin::DynamicFields.update(resource, params) @@ -163,10 +188,11 @@ div row.title, ActiveAdmin::DynamicFields.edit_string(:title, save_admin_article_path(row.id)) end ``` ### Dialog example + Example with 2 models: *Author* and *Article* Prepare the content dialog - in Active Admin Author config: ```rb @@ -206,15 +232,18 @@ ``` The link url is loaded via AJAX before opening the dialog. ## Do you like it? Star it! + If you use this component just star it. A developer is more motivated to improve a project when there is some interest. My other [Active Admin components](https://github.com/blocknotes?utf8=✓&tab=repositories&q=activeadmin&type=source). Or consider offering me a coffee, it's a small thing but it is greatly appreciated: [about me](https://www.blocknot.es/about-me). ## Contributors + - [Mattia Roccoberton](http://blocknot.es): author - The good guys that opened issues and pull requests from time to time ## License + The gem is available as open-source under the terms of the [MIT](LICENSE.txt).