Skip to content

Provides ajax drag and drop sorting for active scaffold subforms. For example, if a List has_many Items that have a position, when editing lists, you can reorder items right in the subform.

Notifications You must be signed in to change notification settings

andrewroth/active_scaffold_sortable_subforms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Drag-Drop Sortable Subforms for ActiveScaffold

Currently, this plugin is not compatible with rails 2.1, due to some outstanding compatibility issues with ActiveScaffold.

Overview

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)

Installation

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

Usage

Step 1

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

Step 2

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)

  1. 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>

Step 3

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.

Contributing

Fork, hack, push, and request a pull:

http://github.com/andrewroth/active_scaffold_sortable_subforms/

Author

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', '@') )

About

Provides ajax drag and drop sorting for active scaffold subforms. For example, if a List has_many Items that have a position, when editing lists, you can reorder items right in the subform.

Resources

Stars

Watchers

Forks

Packages

No packages published