|
| 1 | +module ActiveRecord |
| 2 | + module Acts #:nodoc: |
| 3 | + module List #:nodoc: |
| 4 | + def self.included(base) |
| 5 | + base.extend(ClassMethods) |
| 6 | + end |
| 7 | + |
| 8 | + # This +acts_as+ extension provides the capabilities for sorting and reordering a number of objects in a list. |
| 9 | + # The class that has this specified needs to have a +position+ column defined as an integer on |
| 10 | + # the mapped database table. |
| 11 | + # |
| 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 |
| 25 | + module ClassMethods |
| 26 | + # Configuration options are: |
| 27 | + # |
| 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 <tt>_id</tt> |
| 30 | + # (if it hasn't already been added) 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> |
| 33 | + def acts_as_list(options = {}) |
| 34 | + configuration = { :column => "position", :scope => "1 = 1" } |
| 35 | + configuration.update(options) if options.is_a?(Hash) |
| 36 | + |
| 37 | + configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/ |
| 38 | + |
| 39 | + if configuration[:scope].is_a?(Symbol) |
| 40 | + scope_condition_method = %( |
| 41 | + def scope_condition |
| 42 | + self.class.send(:sanitize_sql_hash_for_conditions, { :#{configuration[:scope].to_s} => send(:#{configuration[:scope].to_s}) }) |
| 43 | + end |
| 44 | + ) |
| 45 | + elsif configuration[:scope].is_a?(Array) |
| 46 | + scope_condition_method = %( |
| 47 | + def scope_condition |
| 48 | + attrs = %w(#{configuration[:scope].join(" ")}).inject({}) do |memo,column| |
| 49 | + memo[column.intern] = send(column.intern); memo |
| 50 | + end |
| 51 | + self.class.send(:sanitize_sql_hash_for_conditions, attrs) |
| 52 | + end |
| 53 | + ) |
| 54 | + else |
| 55 | + scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end" |
| 56 | + end |
| 57 | + |
| 58 | + class_eval <<-EOV |
| 59 | + include ActiveRecord::Acts::List::InstanceMethods |
| 60 | +
|
| 61 | + def acts_as_list_class |
| 62 | + ::#{self.name} |
| 63 | + end |
| 64 | +
|
| 65 | + def position_column |
| 66 | + '#{configuration[:column]}' |
| 67 | + end |
| 68 | +
|
| 69 | + #{scope_condition_method} |
| 70 | +
|
| 71 | + before_destroy :decrement_positions_on_lower_items |
| 72 | + before_create :add_to_list_bottom |
| 73 | + EOV |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + # All the methods available to a record that has had <tt>acts_as_list</tt> specified. Each method works |
| 78 | + # by assuming the object to be the item in the list, so <tt>chapter.move_lower</tt> would move that chapter |
| 79 | + # lower in the list of all chapters. Likewise, <tt>chapter.first?</tt> would return +true+ if that chapter is |
| 80 | + # the first in the list of all chapters. |
| 81 | + module InstanceMethods |
| 82 | + # Insert the item at the given position (defaults to the top position of 1). |
| 83 | + def insert_at(position = 1) |
| 84 | + insert_at_position(position) |
| 85 | + end |
| 86 | + |
| 87 | + # Swap positions with the next lower item, if one exists. |
| 88 | + def move_lower |
| 89 | + return unless lower_item |
| 90 | + |
| 91 | + acts_as_list_class.transaction do |
| 92 | + lower_item.decrement_position |
| 93 | + increment_position |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + # Swap positions with the next higher item, if one exists. |
| 98 | + def move_higher |
| 99 | + return unless higher_item |
| 100 | + |
| 101 | + acts_as_list_class.transaction do |
| 102 | + higher_item.increment_position |
| 103 | + decrement_position |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + # Move to the bottom of the list. If the item is already in the list, the items below it have their |
| 108 | + # position adjusted accordingly. |
| 109 | + def move_to_bottom |
| 110 | + return unless in_list? |
| 111 | + acts_as_list_class.transaction do |
| 112 | + decrement_positions_on_lower_items |
| 113 | + assume_bottom_position |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + # Move to the top of the list. If the item is already in the list, the items above it have their |
| 118 | + # position adjusted accordingly. |
| 119 | + def move_to_top |
| 120 | + return unless in_list? |
| 121 | + acts_as_list_class.transaction do |
| 122 | + increment_positions_on_higher_items |
| 123 | + assume_top_position |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + # Removes the item from the list. |
| 128 | + def remove_from_list |
| 129 | + if in_list? |
| 130 | + decrement_positions_on_lower_items |
| 131 | + update_attribute position_column, nil |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + # Increase the position of this item without adjusting the rest of the list. |
| 136 | + def increment_position |
| 137 | + return unless in_list? |
| 138 | + update_attribute position_column, self.send(position_column).to_i + 1 |
| 139 | + end |
| 140 | + |
| 141 | + # Decrease the position of this item without adjusting the rest of the list. |
| 142 | + def decrement_position |
| 143 | + return unless in_list? |
| 144 | + update_attribute position_column, self.send(position_column).to_i - 1 |
| 145 | + end |
| 146 | + |
| 147 | + # Return +true+ if this object is the first in the list. |
| 148 | + def first? |
| 149 | + return false unless in_list? |
| 150 | + self.send(position_column) == 1 |
| 151 | + end |
| 152 | + |
| 153 | + # Return +true+ if this object is the last in the list. |
| 154 | + def last? |
| 155 | + return false unless in_list? |
| 156 | + self.send(position_column) == bottom_position_in_list |
| 157 | + end |
| 158 | + |
| 159 | + # Return the next higher item in the list. |
| 160 | + def higher_item |
| 161 | + return nil unless in_list? |
| 162 | + acts_as_list_class.find(:first, :conditions => |
| 163 | + "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i - 1).to_s}" |
| 164 | + ) |
| 165 | + end |
| 166 | + |
| 167 | + # Return the next lower item in the list. |
| 168 | + def lower_item |
| 169 | + return nil unless in_list? |
| 170 | + acts_as_list_class.find(:first, :conditions => |
| 171 | + "#{scope_condition} AND #{position_column} = #{(send(position_column).to_i + 1).to_s}" |
| 172 | + ) |
| 173 | + end |
| 174 | + |
| 175 | + # Test if this record is in a list |
| 176 | + def in_list? |
| 177 | + !send(position_column).nil? |
| 178 | + end |
| 179 | + |
| 180 | + private |
| 181 | + def add_to_list_top |
| 182 | + increment_positions_on_all_items |
| 183 | + end |
| 184 | + |
| 185 | + def add_to_list_bottom |
| 186 | + self[position_column] = bottom_position_in_list.to_i + 1 |
| 187 | + end |
| 188 | + |
| 189 | + # Overwrite this method to define the scope of the list changes |
| 190 | + def scope_condition() "1" end |
| 191 | + |
| 192 | + # Returns the bottom position number in the list. |
| 193 | + # bottom_position_in_list # => 2 |
| 194 | + def bottom_position_in_list(except = nil) |
| 195 | + item = bottom_item(except) |
| 196 | + item ? item.send(position_column) : 0 |
| 197 | + end |
| 198 | + |
| 199 | + # Returns the bottom item |
| 200 | + def bottom_item(except = nil) |
| 201 | + conditions = scope_condition |
| 202 | + conditions = "#{conditions} AND #{self.class.primary_key} != #{except.id}" if except |
| 203 | + acts_as_list_class.find(:first, :conditions => conditions, :order => "#{position_column} DESC") |
| 204 | + end |
| 205 | + |
| 206 | + # Forces item to assume the bottom position in the list. |
| 207 | + def assume_bottom_position |
| 208 | + update_attribute(position_column, bottom_position_in_list(self).to_i + 1) |
| 209 | + end |
| 210 | + |
| 211 | + # Forces item to assume the top position in the list. |
| 212 | + def assume_top_position |
| 213 | + update_attribute(position_column, 1) |
| 214 | + end |
| 215 | + |
| 216 | + # This has the effect of moving all the higher items up one. |
| 217 | + def decrement_positions_on_higher_items(position) |
| 218 | + acts_as_list_class.update_all( |
| 219 | + "#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} <= #{position}" |
| 220 | + ) |
| 221 | + end |
| 222 | + |
| 223 | + # This has the effect of moving all the lower items up one. |
| 224 | + def decrement_positions_on_lower_items |
| 225 | + return unless in_list? |
| 226 | + acts_as_list_class.update_all( |
| 227 | + "#{position_column} = (#{position_column} - 1)", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}" |
| 228 | + ) |
| 229 | + end |
| 230 | + |
| 231 | + # This has the effect of moving all the higher items down one. |
| 232 | + def increment_positions_on_higher_items |
| 233 | + return unless in_list? |
| 234 | + acts_as_list_class.update_all( |
| 235 | + "#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} < #{send(position_column).to_i}" |
| 236 | + ) |
| 237 | + end |
| 238 | + |
| 239 | + # This has the effect of moving all the lower items down one. |
| 240 | + def increment_positions_on_lower_items(position) |
| 241 | + acts_as_list_class.update_all( |
| 242 | + "#{position_column} = (#{position_column} + 1)", "#{scope_condition} AND #{position_column} >= #{position}" |
| 243 | + ) |
| 244 | + end |
| 245 | + |
| 246 | + # Increments position (<tt>position_column</tt>) of all items in the list. |
| 247 | + def increment_positions_on_all_items |
| 248 | + acts_as_list_class.update_all( |
| 249 | + "#{position_column} = (#{position_column} + 1)", "#{scope_condition}" |
| 250 | + ) |
| 251 | + end |
| 252 | + |
| 253 | + def insert_at_position(position) |
| 254 | + remove_from_list |
| 255 | + increment_positions_on_lower_items(position) |
| 256 | + self.update_attribute(position_column, position) |
| 257 | + end |
| 258 | + end |
| 259 | + end |
| 260 | + end |
| 261 | +end |
0 commit comments