changeset 210:d59731c3c7bf

Add support for is_private flag
author Edho Arief <edho@myconan.net>
date Sun, 12 May 2013 02:40:13 +0900
parents e26119fe21de
children 5e1d728975a4
files app/models/paste.rb
diffstat 1 files changed, 20 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/app/models/paste.rb	Sun May 12 00:43:23 2013 +0900
+++ b/app/models/paste.rb	Sun May 12 02:40:13 2013 +0900
@@ -1,11 +1,27 @@
 class Paste < ActiveRecord::Base
+  attr_accessor :is_private
   before_validation :paste_limit
   before_validation :convert_newlines
   before_validation :set_paste_hash
   before_validation :set_paste_key
+  before_validation :set_paste_secret
   validates :paste, :paste_hash, :key, :ip, :presence => true
   validates :paste, :length => { :maximum => 1_000_000 }
 
+  def to_param
+    path
+  end
+
+  def self.safe_find(raw_id)
+    id, secret = raw_id.split("-")
+    return unless id.to_i.to_s == id
+    self.where(secret: secret).find(id)
+  end
+
+  def path
+    [id, secret.presence].compact.join("-")
+  end
+
   def set_paste_hash
     self.paste_hash = Digest::SHA512.hexdigest("#{paste}\n")
   end
@@ -14,6 +30,10 @@
     self.key ||= SecureRandom.hex(4)
   end
 
+  def set_paste_secret
+    self.secret = SecureRandom.hex(4) if self.is_private
+  end
+
   def convert_newlines
     self.paste = self.paste.to_s.gsub("\r\n", "\n").gsub("\r", "\n")
   end