Use new ShortId class to generate short id
This commit is contained in:
parent
0a48959cda
commit
08a8165fa9
|
@ -15,24 +15,14 @@ class Message < ActiveRecord::Base
|
||||||
|
|
||||||
validates_length_of :subject, :in => 1..150
|
validates_length_of :subject, :in => 1..150
|
||||||
validates_length_of :body, :maximum => (64 * 1024)
|
validates_length_of :body, :maximum => (64 * 1024)
|
||||||
|
|
||||||
before_create :assign_short_id
|
before_create :assign_short_id
|
||||||
after_create :deliver_reply_notifications
|
after_create :deliver_reply_notifications
|
||||||
after_save :check_for_both_deleted
|
after_save :check_for_both_deleted
|
||||||
after_save :update_unread_counts
|
after_save :update_unread_counts
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
if !Message.find_by_short_id(self.short_id)
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_for_both_deleted
|
def check_for_both_deleted
|
||||||
|
@ -44,7 +34,7 @@ class Message < ActiveRecord::Base
|
||||||
def update_unread_counts
|
def update_unread_counts
|
||||||
self.recipient.update_unread_message_count!
|
self.recipient.update_unread_message_count!
|
||||||
end
|
end
|
||||||
|
|
||||||
def deliver_reply_notifications
|
def deliver_reply_notifications
|
||||||
begin
|
begin
|
||||||
if self.recipient.email_messages?
|
if self.recipient.email_messages?
|
||||||
|
@ -81,12 +71,12 @@ class Message < ActiveRecord::Base
|
||||||
def body=(b)
|
def body=(b)
|
||||||
self[:body] = b.to_s.remove_mb4
|
self[:body] = b.to_s.remove_mb4
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: remove remove_mb4 hack
|
# TODO: remove remove_mb4 hack
|
||||||
def subject=(s)
|
def subject=(s)
|
||||||
self[:subject] = s.to_s.remove_mb4
|
self[:subject] = s.to_s.remove_mb4
|
||||||
end
|
end
|
||||||
|
|
||||||
def linkified_body
|
def linkified_body
|
||||||
Markdowner.to_html(self.body)
|
Markdowner.to_html(self.body)
|
||||||
end
|
end
|
||||||
|
|
8
spec/models/message_spec.rb
Normal file
8
spec/models/message_spec.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe Message do
|
||||||
|
it "should get a short id" do
|
||||||
|
m = Message.make!
|
||||||
|
m.short_id.should match(/^\A[a-zA-Z0-9]{1,10}\z/)
|
||||||
|
end
|
||||||
|
end
|
|
@ -28,3 +28,10 @@ Comment.blueprint do
|
||||||
story_id { Story.make!.id }
|
story_id { Story.make!.id }
|
||||||
comment { "comment text #{sn}" }
|
comment { "comment text #{sn}" }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Message.blueprint do
|
||||||
|
recipient_user_id { User.make!.id }
|
||||||
|
author_user_id { User.make!.id }
|
||||||
|
subject { "message subject #{sn}" }
|
||||||
|
body { "message body #{sn}" }
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue