From 08a8165fa956fced8dbbc6f59bcc772cb14d85eb Mon Sep 17 00:00:00 2001 From: Jon Evans Date: Tue, 22 Jan 2013 23:32:45 -0700 Subject: [PATCH] Use new ShortId class to generate short id --- app/models/message.rb | 22 ++++++---------------- spec/models/message_spec.rb | 8 ++++++++ spec/support/blueprints.rb | 7 +++++++ 3 files changed, 21 insertions(+), 16 deletions(-) create mode 100644 spec/models/message_spec.rb diff --git a/app/models/message.rb b/app/models/message.rb index 84a5aef..27d26e8 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -15,24 +15,14 @@ class Message < ActiveRecord::Base validates_length_of :subject, :in => 1..150 validates_length_of :body, :maximum => (64 * 1024) - + before_create :assign_short_id after_create :deliver_reply_notifications after_save :check_for_both_deleted after_save :update_unread_counts - + def assign_short_id - 10.times do |try| - 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 + self.short_id = ShortId.new(self.class).generate end def check_for_both_deleted @@ -44,7 +34,7 @@ class Message < ActiveRecord::Base def update_unread_counts self.recipient.update_unread_message_count! end - + def deliver_reply_notifications begin if self.recipient.email_messages? @@ -81,12 +71,12 @@ class Message < ActiveRecord::Base def body=(b) self[:body] = b.to_s.remove_mb4 end - + # TODO: remove remove_mb4 hack def subject=(s) self[:subject] = s.to_s.remove_mb4 end - + def linkified_body Markdowner.to_html(self.body) end diff --git a/spec/models/message_spec.rb b/spec/models/message_spec.rb new file mode 100644 index 0000000..a08390a --- /dev/null +++ b/spec/models/message_spec.rb @@ -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 diff --git a/spec/support/blueprints.rb b/spec/support/blueprints.rb index 1097d92..8d862ff 100644 --- a/spec/support/blueprints.rb +++ b/spec/support/blueprints.rb @@ -28,3 +28,10 @@ Comment.blueprint do story_id { Story.make!.id } comment { "comment text #{sn}" } 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