acts_as_list_with_sti_support

Description

This acts_as extension provides the capabilities for sorting and reordering a number of objects in a list. The class that has this specified needs to have a position column defined as an integer on the mapped database table.

Usage

If you just want a global list, simply add acts_as_list to the model.

class ListItem < ActiveRecord::Base
  acts_as_list
end

ListItem.first.move_to_bottom
ListItem.last.move_higher

The plugin accepts two optional parameters.

column

specifies the name of the column to use for the position (default: position)

scope

specifies the scope within which lists are ordered (default: 1 = 1, i.e., a global list)

class FunkyList < ActiveRecord::Base
  has_many :funky_list_items
end

class FunkyListItem < ActiveRecord::Base
  belongs_to   :funky_list
  acts_as_list :column => :pos, :scope => :funky_list
end

The scope option can be supplied as a symbol or as a lambda. The generated scope condition will differ depending on the value’s Type and the schema of the associated table.

# yields "type_id = '99'", if type_id is defined on the table
# yields "type = '99'", if type_id is not defined on the table 
class MyListItem < ActiveRecord::Base
  acts_as_list :scope => :type									
end

# yields "type_id = '99'"
class MyListItem < ActiveRecord::Base
  acts_as_list :scope => lambda { |instance| "type_id = '#{instance.type_id}'" }
end

If you use the plugin within a single table inheritance (STI) design, no extra configuration is needed to cause the position values to be scoped by type.

class Label < ActiveRecord::Base
  acts_as_list
end

class BillingFrequency < Label
end

Installation (Rails 3)

Install me from RubyGems.org by adding a gem dependency to your Gemfile. Bundler does the rest.

gem “acts_as_list_with_sti_support”

$ bundle install

Installation (Rails 2)

Install me from RubyGems.org and add a gem dependency in the appropriate file.

$ gem install acts_as_list_with_sti_support

Or install me as a plugin.

$ script/plugin install git://github.com/coroutine/acts_as_list_with_sti_support.git

Gemroll

Other gems by Coroutine include:

License

Copyright © 2010 Coroutine LLC.

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.

The plugin is a modestly modified version of the rails acts_as_list plugin released under the MIT license by David Heinemeier Hansson.