comparison app/models/paste.rb @ 176:a9dba6a3008b

Initial work to add paste deletion.
author Edho Arief <edho@myconan.net>
date Fri, 01 Feb 2013 22:24:08 +0700
parents 7bb46d9febad
children af80541dc915
comparison
equal deleted inserted replaced
175:e4a39071d132 176:a9dba6a3008b
1 class Paste < ActiveRecord::Base 1 class Paste < ActiveRecord::Base
2 attr_accessible :paste 2 attr_accessible :paste
3 before_validation :paste_limit 3 before_validation :paste_limit
4 before_validation :convert_newlines 4 before_validation :convert_newlines
5 before_validation :set_paste_hash 5 before_validation :set_paste_hash
6 validates :paste, :paste_hash, :ip, :presence => true 6 before_validation :set_paste_key
7 validates :paste, :paste_hash, :key, :ip, :presence => true
7 validates :paste, :length => { :maximum => 1_000_000 } 8 validates :paste, :length => { :maximum => 1_000_000 }
8 9
9 def set_paste_hash 10 def set_paste_hash
10 self.paste_hash = Digest::SHA512.hexdigest("#{paste}\n") 11 self.paste_hash = Digest::SHA512.hexdigest("#{paste}\n")
12 end
13
14 def set_paste_key
15 self.key ||= SecureRandom.hex(4)
11 end 16 end
12 17
13 def convert_newlines 18 def convert_newlines
14 self.paste = self.paste.to_s.gsub("\r\n", "\n").gsub("\r", "\n") 19 self.paste = self.paste.to_s.gsub("\r\n", "\n").gsub("\r", "\n")
15 end 20 end