Pull short id generation into separate class
This commit is contained in:
parent
36b51d46c7
commit
0a48959cda
|
@ -83,18 +83,7 @@ class Comment < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def assign_short_id_and_upvote
|
def assign_short_id_and_upvote
|
||||||
10.times do |try|
|
self.short_id = ShortId.new(self.class).generate
|
||||||
if try == 10
|
|
||||||
raise "too many hash collisions"
|
|
||||||
end
|
|
||||||
|
|
||||||
self.short_id = Utils.random_str(6).downcase
|
|
||||||
|
|
||||||
if !Comment.find_by_short_id(self.short_id)
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self.upvotes = 1
|
self.upvotes = 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@ class Invitation < ActiveRecord::Base
|
||||||
attr_accessible nil
|
attr_accessible nil
|
||||||
|
|
||||||
validate do
|
validate do
|
||||||
if !email.to_s.match(/\A[^@ ]+@[^ @]+\.[^ @]+\z/)
|
unless email.to_s.match(/\A[^@ ]+@[^ @]+\.[^ @]+\z/)
|
||||||
errors.add(:email, "is not valid")
|
errors.add(:email, "is not valid")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
42
app/models/short_id.rb
Normal file
42
app/models/short_id.rb
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
class ShortId
|
||||||
|
attr_accessor :klass, :generation_attempts
|
||||||
|
|
||||||
|
def initialize(klass)
|
||||||
|
self.klass = klass
|
||||||
|
self.generation_attempts = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate
|
||||||
|
until (generated_id = candidate_id) && generated_id.valid? do
|
||||||
|
self.generation_attempts += 1
|
||||||
|
raise 'too many hash collisions' if generation_attempts == 10
|
||||||
|
end
|
||||||
|
generated_id.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def candidate_id
|
||||||
|
CandidateId.new(klass)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
class CandidateId
|
||||||
|
attr_accessor :klass, :id
|
||||||
|
|
||||||
|
def initialize(klass)
|
||||||
|
self.klass = klass
|
||||||
|
self.id = generate_id
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
id
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_id
|
||||||
|
Utils.random_str(6).downcase
|
||||||
|
end
|
||||||
|
|
||||||
|
def valid?
|
||||||
|
!klass.exists?(short_id: id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -128,17 +128,7 @@ class Story < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def assign_short_id
|
def assign_short_id
|
||||||
10.times do |try|
|
self.short_id = ShortId.new(self.class).generate
|
||||||
if try == 10
|
|
||||||
raise "too many hash collisions"
|
|
||||||
end
|
|
||||||
|
|
||||||
self.short_id = Utils.random_str(6).downcase
|
|
||||||
|
|
||||||
if !Story.find_by_short_id(self.short_id)
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def log_moderation
|
def log_moderation
|
||||||
|
|
9
spec/models/comment_spec.rb
Normal file
9
spec/models/comment_spec.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe Comment do
|
||||||
|
it "should get a short id" do
|
||||||
|
c = Comment.make!(:comment => "hello")
|
||||||
|
|
||||||
|
c.short_id.should match(/^\A[a-zA-Z0-9]{1,10}\z/)
|
||||||
|
end
|
||||||
|
end
|
|
@ -22,3 +22,9 @@ Story.blueprint do
|
||||||
url { "http://example.com/#{sn}" }
|
url { "http://example.com/#{sn}" }
|
||||||
tags_a { [ "tag1", "tag2" ] }
|
tags_a { [ "tag1", "tag2" ] }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Comment.blueprint do
|
||||||
|
user_id { User.make!.id }
|
||||||
|
story_id { Story.make!.id }
|
||||||
|
comment { "comment text #{sn}" }
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue