# -*- encoding : utf-8 -*-
class ImageFile < ActiveRecord::Base
	mount_uploader :file, ImageUploader

	before_save do
		if !self.name?
			self.name = File.basename(self.file.filename, File.extname(self.file.filename)).to_s if self.file?
		end
			
	end
	
	before_validation do 
	 	if self.tags?
			self.tags = self.tags.gsub(/ +?,/, ',')
			self.tags = self.tags.gsub(/, +?/, ',')
			self.tags = self.tags.gsub(/,/, ', ')
		end

	end
	
	after_save do 
		tags_to_save = self.tags.to_s.split(',')
		tags_to_save.each do |t|
			name = t.to_s.strip
			
			Tag.find_or_create_by_name(t.to_s.strip) if name.size > 0
		end
	end
end
