Isn’t it great when you try to track down the source of an error and find it in some code you wrote… while you were apparently on CRAZY PILLS?

Here’s something I just found this morning. Maybe I need another cup of coffee (as it’s not even 9am yet), but… wtf??

   1  def create_thread(user, contacts, params)
2 mail_thread = Mail.new
3 mail_thread.attributes = valid_mail_params(params)
4 # ...
5 end
6 def assign_mail_params(valid_params, k,v)
7 valid_params[k] = v if self.attributes.keys.include? k
8 end
9
10 def valid_mail_params(params, valid_params = {})
11 params.each_pair do |k,v|
12 (v.is_a? Hash) ? valid_mail_params(v, valid_params) : assign_mail_params(valid_params, k, v)
13 end
14 valid_params
15 end
16
17 def self.valid_mail_params(params, valid_params = {})
18 self.new.valid_mail_params(params, valid_params = {})
19 end


I’m going to guess I was trying to ensure that params unrelated to the object were not getting assigned as attributes (thus causing errors…) but, still, that’s insane. Time to refactor, after this next cup of coffee :)