On Apr 8, 2005 8:12 PM, Glenn Smith <glenn.ruby / gmail.com> wrote:
> In particular, similar code sort of repeats itself, which makes me
> wonder if it could be better structured (using a 'proc' maybe?).

Here's my play with it, though as Daniel said, you didn't include
tests so I'm not 100% sure it does the same thing as your code :)

def format(date)
		unless date.to_s.strip.empty?
			res = ParseDate.parsedate date
			Time.local(*res)
		end
end

def save_activity_dates(change_sent, change_received, org, year)
	
	return true unless change_sent or change_received

	dates = @params['activity_dates']
	
	sent     = format(dates['sent'])    || Time.now
	due      = format(dates['due'])     || Time.now + (21 * 86400) # Add
21 days by default
	received = format(dates['received'])|| Time.now
	
	Activitydate.new do |ad|
		ad['org_id']   = org.id
		ad['year']     = year
		ad['sent']     = sent     if change_sent
		ad['due']      = due      if change_sent 
		ad['received'] = received if change_received
	end.save
	
end

Cheers,
Douglas