comparison app/models/paste.rb @ 210:d59731c3c7bf

Add support for is_private flag
author Edho Arief <edho@myconan.net>
date Sun, 12 May 2013 02:40:13 +0900
parents af80541dc915
children 388504e43bcf
comparison
equal deleted inserted replaced
209:e26119fe21de 210:d59731c3c7bf
1 class Paste < ActiveRecord::Base 1 class Paste < ActiveRecord::Base
2 attr_accessor :is_private
2 before_validation :paste_limit 3 before_validation :paste_limit
3 before_validation :convert_newlines 4 before_validation :convert_newlines
4 before_validation :set_paste_hash 5 before_validation :set_paste_hash
5 before_validation :set_paste_key 6 before_validation :set_paste_key
7 before_validation :set_paste_secret
6 validates :paste, :paste_hash, :key, :ip, :presence => true 8 validates :paste, :paste_hash, :key, :ip, :presence => true
7 validates :paste, :length => { :maximum => 1_000_000 } 9 validates :paste, :length => { :maximum => 1_000_000 }
10
11 def to_param
12 path
13 end
14
15 def self.safe_find(raw_id)
16 id, secret = raw_id.split("-")
17 return unless id.to_i.to_s == id
18 self.where(secret: secret).find(id)
19 end
20
21 def path
22 [id, secret.presence].compact.join("-")
23 end
8 24
9 def set_paste_hash 25 def set_paste_hash
10 self.paste_hash = Digest::SHA512.hexdigest("#{paste}\n") 26 self.paste_hash = Digest::SHA512.hexdigest("#{paste}\n")
11 end 27 end
12 28
13 def set_paste_key 29 def set_paste_key
14 self.key ||= SecureRandom.hex(4) 30 self.key ||= SecureRandom.hex(4)
31 end
32
33 def set_paste_secret
34 self.secret = SecureRandom.hex(4) if self.is_private
15 end 35 end
16 36
17 def convert_newlines 37 def convert_newlines
18 self.paste = self.paste.to_s.gsub("\r\n", "\n").gsub("\r", "\n") 38 self.paste = self.paste.to_s.gsub("\r\n", "\n").gsub("\r", "\n")
19 end 39 end