acts_as_list

使ってるのEdge Railsというわけではない(r4573)ので微妙だけどメモ.

--- vendor/rails/activerecord/lib/active_record/acts/list.rb.orig	2006-08-18 14:40:37.302625000 +0900
+++ vendor/rails/activerecord/lib/active_record/acts/list.rb	2006-08-18 15:00:54.771375000 +0900
@@ -36,7 +36,8 @@
 
           configuration[:scope] = "#{configuration[:scope]}_id".intern if configuration[:scope].is_a?(Symbol) && configuration[:scope].to_s !~ /_id$/
           
-          if configuration[:scope].is_a?(Symbol)
+          case configuration[:scope]
+          when Symbol
             scope_condition_method = %(
               def scope_condition
                 if #{configuration[:scope].to_s}.nil?
@@ -46,6 +47,32 @@
                 end
               end
             )
+          when Array
+            scopes = configuration[:scope].collect{|s|
+              s = s.to_s
+              s = "#{s}_id" if s !~ /_id$/
+              "'#{s}'"
+            }.join(",")
+
+            scope_condition_method = %(
+              def scope_condition
+                cond = []
+            ) + 
+            configuration[:scope].collect{|s|
+              s = s.to_s
+              s = "#{s}_id" if s !~ /_id$/
+              %(
+                if #{s}.nil?
+                  cond << "#{s} IS NULL"
+                else
+                  cond << "#{s} = \#{#{s}}"
+                end
+              )
+            }.join + %(
+                cond.join(' AND ')
+              end
+            )
+            logger.info scope_condition_method
           else
             scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end"
           end

直接いじらない版(lib/とかにほおっておく)

class ActiveRecord::Base
  class << self
    def acts_as_list_with_arraysupport(configuration = {})
      acts_as_list_without_arraysupport(configuration)
      if configuration[:scope].is_a? Array
        scopes = configuration[:scope].collect{|s|
          s = s.to_s
          s = "#{s}_id" if s !~ /_id$/
          "'#{s}'"
        }.join(",")

        scope_condition_method = %(
          def scope_condition
            cond = []
        ) + 
        configuration[:scope].collect{|s|
          s = s.to_s
          s = "#{s}_id" if s !~ /_id$/
          %(
            if #{s}.nil?
              cond << "#{s} IS NULL"
            else
              cond << "#{s} = \#{#{s}}"
            end
          )
        }.join + %(
            cond.join(' AND ')
          end
        )
        class_eval scope_condition_method
      end
    end
    alias_method_chain :acts_as_list, :arraysupport
  end
end