Module: ActiveRecord::Acts::List::ClassMethods

Defined in:
lib/acts_as_list/active_record/acts/list.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_list(options = {}) ⇒ Object

Configuration options are:

  • column - specifies the column name to use for keeping the position integer (default: position)

  • scope - restricts what is to be considered a list. Given a symbol, it’ll attach _id (if it hasn’t already been added) and use that as the foreign key restriction. It’s also possible to give it an entire string that is interpolated if you need a tighter scope than just a foreign key. Example: acts_as_list scope: 'todo_list_id = #{todo_list_id} AND completed = 0'

  • top_of_list - defines the integer used for the top of the list. Defaults to 1. Use 0 to make the collection act more like an array in its indexing.

  • add_new_at - specifies whether objects get added to the :top or :bottom of the list. (default: bottom)

    `nil` will result in new items not being added to the list on create.
    
  • sequential_updates - specifies whether insert_at should update objects positions during shuffling one by one to respect position column unique not null constraint. Defaults to true if position column has unique index, otherwise false. If constraint is <tt>deferrable initially deferred<tt>, overriding it with false will speed up insert_at.

  • touch_on_update - configuration to disable the update of the model timestamps when the positions are updated.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/acts_as_list/active_record/acts/list.rb', line 24

def acts_as_list(options = {})
  configuration = { column: "position", scope: "1 = 1", top_of_list: 1, add_new_at: :bottom, touch_on_update: true }
  configuration.update(options) if options.is_a?(Hash)

  caller_class = self

  ActiveRecord::Acts::List::PositionColumnMethodDefiner.call(caller_class, configuration[:column], configuration[:touch_on_update])
  ActiveRecord::Acts::List::ScopeMethodDefiner.call(caller_class, configuration[:scope])
  ActiveRecord::Acts::List::TopOfListMethodDefiner.call(caller_class, configuration[:top_of_list])
  ActiveRecord::Acts::List::AddNewAtMethodDefiner.call(caller_class, configuration[:add_new_at])

  ActiveRecord::Acts::List::AuxMethodDefiner.call(caller_class)
  ActiveRecord::Acts::List::CallbackDefiner.call(caller_class, configuration[:add_new_at])
  ActiveRecord::Acts::List::SequentialUpdatesMethodDefiner.call(caller_class, configuration[:column], configuration[:sequential_updates])

  include ActiveRecord::Acts::List::InstanceMethods
  include ActiveRecord::Acts::List::NoUpdate
end