Permalink
Browse files

Message: support for associating a UUID

  • Loading branch information...
1 parent b3012b8 commit 4c23285870aa506c91bb6fd7b4a706b1323a17ab @dhedlund committed Dec 1, 2011
Showing with 41 additions and 1 deletion.
  1. +11 −0 app/models/message.rb
  2. +10 −0 db/migrate/20111201063352_add_uuid_to_messages.rb
  3. +3 −1 db/schema.rb
  4. +17 −0 test/unit/message_test.rb
View
@@ -1,7 +1,11 @@
+require 'uuid'
+
class Message < ActiveRecord::Base
belongs_to :message_stream
has_many :notifications
+ before_create :generate_uuid
+
validates :message_stream_id, :presence => true
validates :name, :presence => true, :uniqueness => { :scope => :message_stream_id }
validates :title, :presence => true
@@ -28,4 +32,11 @@ def sms_text(variables=nil)
variables.inject(self[:sms_text]) { |r,(k,v)| r.gsub("%#{k}%", v) }
end
+
+ protected
+
+ def generate_uuid
+ write_attribute :uuid, UUID.generate
+ end
+
end
@@ -0,0 +1,10 @@
+class AddUuidToMessages < ActiveRecord::Migration
+ def self.up
+ add_column :messages, :uuid, :string
+ add_index :messages, :uuid, :unique => true
+ end
+
+ def self.down
+ remove_column :messages, :uuid
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111201053320) do
+ActiveRecord::Schema.define(:version => 20111201063352) do
create_table "delayed_jobs", :force => true do |t|
t.integer "priority", :default => 0
@@ -81,9 +81,11 @@
t.string "title"
t.string "language"
t.integer "expire_days"
+ t.string "uuid"
end
add_index "messages", ["message_stream_id", "name"], :name => "index_messages_on_message_stream_id_and_name", :unique => true
+ add_index "messages", ["uuid"], :name => "index_messages_on_uuid", :unique => true
create_table "nexmo_inbound_messages", :force => true do |t|
t.string "ext_message_id"
@@ -229,4 +229,21 @@ class MessagesTest < ActiveSupport::TestCase
assert @message.errors[:title].any?
end
+ #----------------------------------------------------------------------------#
+ # uuid:
+ #------
+ test "should generate a new uuid on creation" do
+ message = Factory.build(:message, :uuid => nil)
+ message.save!
+ assert_not_nil message.uuid
+ end
+
+ test "should not generate a new uuid on update" do
+ @message.save!
+ old_uuid = @message.uuid
+ @message.title = 'Foo Msg'
+ @message.save!
+ assert_equal old_uuid, @message.uuid
+ end
+
end

0 comments on commit 4c23285

Please sign in to comment.