打印

[一般问题] acts_as_list函数怎么理解?

acts_as_list函数怎么理解?

这个函数,不理解,写了那么多,看不懂

 # File vendor/php?name=rails" onclick="tagshow(event)" class="t_tag">rails/activerecord/lib/active_record/acts/list.rb, line 33
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:                 if #{configuration[:scope].to_s}.nil?
43:                   "#{configuration[:scope].to_s} IS NULL"
44:                 else
45:                   "#{configuration[:scope].to_s} = \#{#{configuration[:scope].to_s}}"
46:                 end
47:               end
48:             )
49:           else
50:             scope_condition_method = "def scope_condition() \"#{configuration[:scope]}\" end"
51:           end
52:           
53:           class_eval "include ActiveRecord::Acts::List::InstanceMethods\n\ndef acts_as_list_class\n::\#{self.name}\nend\n\ndef position_column\n'\#{configuration[:column]}'\nend\n\n\#{scope_condition_method}\n\nbefore_destroy :remove_from_list\nbefore_create  :add_to_list_bottom\n"
54:         end


TOP

前面部分应该是判断传入的参数以及合并,后面是动态定义了一些方法。等待更详细的解答中。
谢谢大家加入Ruby中文社区!
[寻找您身边的Rubyist.]

TOP

2008-11-22 18:30 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61