Skip to content

Commit

Permalink
Message-User relations and message unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
zmcartor committed Mar 3, 2013
1 parent c6db318 commit e4b3c95
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
6 changes: 6 additions & 0 deletions app/models/message.rb
@@ -1,3 +1,9 @@
class Message < ActiveRecord::Base
attr_accessible :from, :text, :to

belongs_to :sender, class_name: User, :foreign_key => :from
belongs_to :receiver, class_name:User, :foreign_key => :to

validates :text, presence: true

end
4 changes: 4 additions & 0 deletions app/models/user.rb
@@ -1,4 +1,8 @@
class User < ActiveRecord::Base

has_many :messages_inbox, class_name: Message, :foreign_key => :to
has_many :messages_outbox, class_name: Message, :foreign_key => :from

# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
Expand Down
25 changes: 24 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130302195610) do
ActiveRecord::Schema.define(:version => 20130302202132) do

create_table "authentications", :force => true do |t|
t.integer "user_id"
Expand All @@ -23,6 +23,12 @@

add_index "authentications", ["user_id"], :name => "index_authentications_on_user_id"

create_table "images", :force => true do |t|
t.integer "imgur_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "matches", :force => true do |t|
t.integer "user_id"
t.integer "match_id"
Expand All @@ -34,6 +40,23 @@
add_index "matches", ["match_id"], :name => "index_matches_on_match_id"
add_index "matches", ["user_id"], :name => "index_matches_on_user_id"

create_table "messages", :force => true do |t|
t.text "text"
t.integer "to"
t.integer "from"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "rated_images", :force => true do |t|
t.integer "image_id"
t.integer "status"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

add_index "rated_images", ["image_id"], :name => "index_rated_images_on_image_id"

create_table "roles", :force => true do |t|
t.integer "user_id"
t.string "name"
Expand Down
6 changes: 3 additions & 3 deletions test/factories/messages.rb
Expand Up @@ -2,8 +2,8 @@

FactoryGirl.define do
factory :message do
text "MyText"
to 1
from 1
text "Ponies and butterflies"
to nil
from nil
end
end
6 changes: 6 additions & 0 deletions test/unit/message_test.rb
Expand Up @@ -4,4 +4,10 @@ class MessageTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end

test 'should have some message text' do
mess = build(:message , text:nil)
assert !mess.save , 'message saved with no text, bummer!'
end

end

0 comments on commit e4b3c95

Please sign in to comment.