Skip to content

Silvaire/activeadmin_polymorphic

 
 

Repository files navigation

ActiveadminPolymorphic

Activeadmin Polymorphic gem is made to bring has_many polymorphic nested forms into your ActiveAdmin. ActiveAdmin users formtastic to build awesome forms, but formatstic itself doesn't support polymorphic relations. activeadmin_polymorphic gem is trying to solve that problem.

Features

  • polymorphic forms
  • validation
  • sortable behaviour
  • file uploads

Installation

Add this to your Gemfile:

gem "activeadmin_polymorphic"

and run bundle install.

Include assets in js and css manifests

#= require activeadmin_polymorphic
@import "activeadmin_polymorphic";

Usage

To use gem, your model should have related model, which works as a proxy to polymorphic relations.

Gem extrands activeadmin's form builder, so to enable has_many_polymorphic method you need to override form builder using builder option:

...
SECTIONABLES = [Image, Text]

form builder: ActiveadminPolymorphic::FormBuilder do |f|
  f.polymorphic_has_many :sections, :sectionable, types: SECTIONABLES
end
...

There are few options available:

  • first option is a name of polymorphic has_many association
  • second option referes to polymorphied version of association name (sectionable_id and sectaionable_type for example)
  • types - list of related models you want to use
  • allow_destroy - weather or not to allow to destroy related objects
  • sortable - enables drag'n'drop for nested forms, accepts sortable column name, for example sortable: :priority

Subforms for polymorphic relations are forms which you define in your ActiveAdmin. Gem fetches and submits them using some ajax magic.

Under the hood

This gem is a set of dirty hacks and tricks. Calling polymorphic_has_many makes it to do the following things:

  • for new records it generates dropdown with polymorphic types
  • for exising records it generates two hidden fields with id and type
  • then the real javascript starts, it extracts whole forms from polymorphic models new or edit pages, strips form actions, and inserts that forms right into parent form
  • when you try to submit forms, javascripts submit subforms first; if subforms are invalid, it reloads them with erros and interupts main form submission process
  • after all sub forms successfully saved, it strips them (because forms nested into other forms are simantically invalid, right?) and submits parent form

File uploads in subforms

Gem relies on rails ajax form submissions, which doesn't allow to submit files directly. Workaround for it is asynchronous file submission using for example remotipart for CarrierWave or refile with refile-input. Note: before subforms submissions javascript strips all file inputs from forms.

Testing

Tests stucture is mostly copied from original ActiveAdmin.

Install development dependencies with bundle install. To setup test suit run rake test. Run tests with bundle exec guard. There aren't many of them, let's say there are quite a few.

In plan

  • allow to reuse existing polymorphic objects
  • check who it works with models under sertain namespace
  • improve tests

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 88.5%
  • CoffeeScript 9.4%
  • CSS 2.1%