Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7982: acts_as_list.diff

File acts_as_list.diff, 3.1 kB (added by fearoffish, 1 year ago)
  • list.rb

    old new  
    99      # The class that has this specified needs to have a "position" column defined as an integer on 
    1010      # the mapped database table. 
    1111      # 
    12       # Todo list example: 
    13       # 
    14       #   class TodoList < ActiveRecord::Base 
    15       #     has_many :todo_items, :order => "position" 
    16       #   end 
    17       # 
    18       #   class TodoItem < ActiveRecord::Base 
    19       #     belongs_to :todo_list 
    20       #     acts_as_list :scope => :todo_list 
    21       #   end 
    22       # 
    23       #   todo_list.first.move_to_bottom 
    24       #   todo_list.last.move_higher 
     12      # See acts_as_list for more detailed information. 
    2513      module ClassMethods 
    26         # Configuration options are: 
     14        # Providing the capability to sort and reorder a models records.  The model that will be sorted  
     15        # should be referenced frm another model. 
     16        #  
     17        # ==== Options 
     18        # * +column+ - specifies the column name to use for keeping the position integer (default: position) 
     19        # * +scope+ - restricts what is to be considered part of the list. Typically this will be the name  
     20        #   of another model in lowercase as a symbol, acts_as_list will append "_id" onto the end (if it's not 
     21        #   there already) and use that to reference another model. It's also possible to give an entire SQL string  
     22        #   that is interpolated if you need a tighter scope than just a foreign key. 
    2723        # 
    28         # * +column+ - specifies the column name to use for keeping the position integer (default: position) 
    29         # * +scope+ - restricts what is to be considered a list. Given a symbol, it'll attach "_id"  
    30         #   (if that hasn't been already) and use that as the foreign key restriction. It's also possible  
    31         #   to give it an entire string that is interpolated if you need a tighter scope than just a foreign key. 
    32         #   Example: <tt>acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0'</tt> 
     24        # ==== Examples 
     25        # 
     26        #   class TodoList < ActiveRecord::Base 
     27        #     has_many :todo_items, :order => "position" 
     28        #   end 
     29        #    
     30        #   class TodoItem < ActiveRecord::Base 
     31        #     belongs_to :todo_list 
     32        #     acts_as_list :scope => :todo_list # this could also be todo_list_id 
     33        #   end 
     34        #    
     35        #   # Gives you the following instance methods on the list model 
     36        #   todo_list.first.move_to_bottom 
     37        #   todo_list.last.move_higher 
     38        # 
     39        #   Using the example from above and using an SQL query to reduce the scope to completed items only: 
     40        #    
     41        #   class TodoItem < ActiveRecord::Base 
     42        #     belongs_to :todo_list 
     43        #     acts_as_list :scope => 'todo_list_id = #{todo_list_id} AND completed = 0' 
     44        #   end 
    3345        def acts_as_list(options = {}) 
    3446          configuration = { :column => "position", :scope => "1 = 1" } 
    3547          configuration.update(options) if options.is_a?(Hash)