Currently, this plugin is not compatible with rails 2.1, due to some outstanding compatibility issues with ActiveScaffold.
This plugin enables enables ajax drag and drop sorting for subforms. Can work alongside the active_scaffold_sortable plugin, which provides similar functionality for the top level.
It works by hiding the position field display, and adding javascript to reset the value in each position each time the order is changed. So this is counter to the usual “reorder” method that dragdrop sortables usually use. This is intentional. It allows new items that aren’t in the database yet to be positioned, and for changes to take effect only when ‘Update’ is pressed, which is how the rest of the subform operates.
Note I add the patch to enable subforms to use helper overrides, and build on top of that. (see http://code.google.com/p/activescaffold/issues/detail?id=77)
You’ll need at least rails 2.3.
ruby script/plugin install git://github.com/activescaffold/active_scaffold.git -r rails-2.3 ruby script/plugin install git://github.com/andrewroth/active_scaffold_sortable_subforms
Create a model A with relation B, and column named ‘position’ on B:
ruby script/generate model entry title:string birthdyate:date
# app/models/entry.rb # id integer # title string(255) # birthday date class Entry < ActiveRecord::Base has_many :items, :order => :position end
ruby script/generate model item name:string entry_id:integer position:integer
# app/models/item.rb # id integer # name string(255) # entry_id integer # position integer class Item < ActiveRecord::Base belongs_to :entry end
Create your scaffold controller
# app/controllers/entries_controller.rb class EntriesController < ApplicationController active_scaffold end # app/controllers/items_controller.rb class ItemsController < ApplicationController active_scaffold end
and layout (note the javascript as_dd_reorder.js include)
- entry_id integer
# app/views/layouts/application.rhtml <html> <head> <title>active scaffold demo application</title> <%= javascript_include_tag :defaults %> <%= active_scaffold_includes %> <%= javascript_include_tag 'as_dd_reorder' %> </head> <body> <%= yield %> </body> </html>
Add one include and one define to helper, see below.
The active_scaffold_sortable_subform expects parameters of the form :subform_model => :subform_column
In this case:
# app/helpers/entries_helper.rb module EntriesHelper include ActiveScaffoldSortableSubforms active_scaffold_sortable_subform :item => :position end
http://localhost:3000/entries and create a new entry. You should see Items there with a position drag image.
Fork, hack, push, and request a pull:
http://github.com/andrewroth/active_scaffold_sortable_subforms/
Contact me:
Andrew Roth – [email protected]
Thanks to Tim Harper for active_scaffold_sortable:
Tim Harper - irb(main):001:0> ( 'tim_see_harperATgmail._see_om'.gsub('_see_', 'c').gsub('AT', '@') )