comparison app/models/paste.rb @ 70:8f0fb869e770

Limit pastes to 100/hour per IP address.
author Edho Arief <edho@myconan.net>
date Thu, 04 Oct 2012 10:38:08 +0700
parents 36d07f047ec2
children 7bb46d9febad
comparison
equal deleted inserted replaced
69:be561c3967ee 70:8f0fb869e770
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 :convert_newlines 4 before_validation :convert_newlines
4 before_validation :set_paste_hash 5 before_validation :set_paste_hash
5 validates :paste, :paste_hash, :ip, :presence => true 6 validates :paste, :paste_hash, :ip, :presence => true
6 validates :paste, :length => { :maximum => 1_000_000 } 7 validates :paste, :length => { :maximum => 1_000_000 }
7 8
11 12
12 def convert_newlines 13 def convert_newlines
13 self.paste = self.paste.gsub("\r\n", "\n").gsub("\r", "\n") 14 self.paste = self.paste.gsub("\r\n", "\n").gsub("\r", "\n")
14 end 15 end
15 16
17 def paste_limit
18 ip_post_recent_count = self.class.where(:ip => self.ip).where('created_at > ?', Time.now - 1.hour).count
19 errors.add :base, :limit if ip_post_recent_count > 100
20 end
21
16 def self.fix_all 22 def self.fix_all
17 self.all.each do |p| 23 self.all.each do |p|
18 p.save 24 p.save
19 end 25 end
20 end 26 end