Skip to content

Commit

Permalink
now users can send messages
Browse files Browse the repository at this point in the history
  • Loading branch information
strix3000 committed Mar 12, 2012
1 parent 8bd10e6 commit ce0b365
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 108 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/message.js.coffee
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/message.css.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the message controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
37 changes: 37 additions & 0 deletions app/controllers/messages_controller.rb
@@ -0,0 +1,37 @@
class MessagesController < ApplicationController

def create
@recipient = User.find(params[:id])
@message = Message.new()
@message.content = params[:content]
@message.user = current_user
@message.save
mes_recipient = MessageRecipient.new()
mes_recipient.message_id = @message.id
mes_recipient.user_id = 1
mes_recipient.save
end

def show
@message = Message.find(params[:id])
end

def index
@messages = current_user.messages + current_user.recieved_messages
end

def update
@message = Message.find(params[:id])

respond_to do |format|
if @message.update_attributes(params[:message])
format.html { redirect_to @message, notice: 'message was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @message.errors, status: :unprocessable_entity }
end
end
end

end
2 changes: 1 addition & 1 deletion app/controllers/uploads_controller.rb
Expand Up @@ -39,7 +39,7 @@ def edit
# POST /files.json
def create
@file = Upload.new(params[:upload])
@file.user = current_user
@file.user = current_user
respond_to do |format|
if @file.save
format.html { redirect_to(:uploads, :notice => 'file was successfully created.') }
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/message_helper.rb
@@ -0,0 +1,2 @@
module MessageHelper
end
9 changes: 9 additions & 0 deletions app/models/message.rb
@@ -0,0 +1,9 @@
class Message < ActiveRecord::Base
#Relations
belongs_to :user
has_many :message_recipients
has_many :recipients, :through => :message_recipients, :source => :user
#Validations
validate :user_id, :presence => true

end
10 changes: 10 additions & 0 deletions app/models/message_recipient.rb
@@ -0,0 +1,10 @@
class MessageRecipient < ActiveRecord::Base
#Relations
belongs_to :message
belongs_to :user

#Validations
validate :user_id, :presence => true
validate :message_id , :presence => true

end
8 changes: 0 additions & 8 deletions app/models/post.rb.save

This file was deleted.

9 changes: 5 additions & 4 deletions app/models/user.rb
Expand Up @@ -4,12 +4,13 @@ class User < ActiveRecord::Base

#Relations
has_many :uploads

has_many :users_upload
has_many :saved_uploads, :through => :users_upload, :source => :upload
has_many :posts, :dependent => :destroy
# has_many :uploads, :through => :users_uploads

has_many :posts, :dependent => :destroy

has_many :messages
has_many :message_recipient
has_many :recieved_messages, :through => :message_recipient, :source => :message



Expand Down
15 changes: 9 additions & 6 deletions app/views/layouts/_header.html.erb
@@ -1,16 +1,19 @@
<header>
<%= image_tag("logo.png", :alt => "Ruby School", :class => "round") %>
<header>
<%= image_tag("logo.png", :alt => "Ruby School", :class => "round") %>
<nav class="round">
<ul>

<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Users", :userslist %></li>
<% if current_user %>
<li><%= link_to "Edit Profile", edit_user_path(current_user.id) %></li>
<li><%= link_to "Logout", :logout %></li>
<li><%= link_to "my_downloads", :downloads %></li>
<% else %>
<li><%= link_to "my_downloads", :my_downloads %></li>

<% else %>
<li><%= link_to "Register", new_user_path %></li>
<li><%= link_to "Login", :login %></li>
<% end %>
</ul>
</nav>
</nav>

</header>
16 changes: 16 additions & 0 deletions app/views/layouts/_style_and_meta.html.erb
@@ -0,0 +1,16 @@
<head>
<title>Social Network | <%= @title %></title>

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
<%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
<!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
<%= stylesheet_link_tag 'custom', :media => 'screen' %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>

42 changes: 3 additions & 39 deletions app/views/layouts/application.html.erb
@@ -1,43 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Social Network | <%= @title %></title>

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
<%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
<!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
<%= stylesheet_link_tag 'custom', :media => 'screen' %>

<%= render 'layouts/style_and_meta' %>

<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="container">

<header>
<%= image_tag("logo.png", :alt => "Ruby School", :class => "round") %>
<nav class="round">
<ul>
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Users", :userslist %></li>
<% if current_user %>
<li><%= link_to "Edit Profile", edit_user_path(current_user.id) %></li>
<li><%= link_to "Logout", :logout %></li>
<li><%= link_to "downloads", :downloads %></li>
<li><%= link_to "my_downloads", :my_downloads %></li>

<% else %>
<li><%= link_to "Register", new_user_path %></li>
<li><%= link_to "Login", :login %></li>
<% end %>
</ul>
</nav>
</header>

<%= render 'layouts/header' %>

<div>
<p id="notice"><%= notice %></p>
<p id="alert"><%= alert %></p>
Expand All @@ -47,11 +17,5 @@
<%= yield %>
</section>
</div>

<div>
<!--<% if current_user %><% else %>
<%= link_to "Register", new_user_path, :class => "signup_button round" %>
<% end %>-->
</div>
</body>
</html>
42 changes: 3 additions & 39 deletions app/views/layouts/application.html.erb~
@@ -1,43 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Social Network | <%= @title %></title>

<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<%= stylesheet_link_tag 'blueprint/screen', :media => 'screen' %>
<%= stylesheet_link_tag 'blueprint/print', :media => 'print' %>
<!--[if lt IE 8]><%= stylesheet_link_tag 'blueprint/ie' %><![endif]-->
<%= stylesheet_link_tag 'custom', :media => 'screen' %>

<%= render 'layouts/head' %>

<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="container">

<header>
<%= image_tag("logo.png", :alt => "Ruby School", :class => "round") %>
<nav class="round">
<ul>
<li><%= link_to "Home", :home %></li>
<li><%= link_to "Users", :userslist %></li>
<% if current_user %>
<li><%= link_to "Edit Profile", edit_user_path(current_user.id) %></li>
<li><%= link_to "Logout", :logout %></li>
<li><%= link_to "downloads", :downloads %></li>
<li><%= link_to "my_downloads", :my_downloads %></li>

<% else %>
<li><%= link_to "Register", new_user_path %></li>
<li><%= link_to "Login", :login %></li>
<% end %>
</ul>
</nav>
</header>

<%= render 'layouts/header' %>

<div>
<p id="notice"><%= notice %></p>
<p id="alert"><%= alert %></p>
Expand All @@ -47,11 +17,5 @@
<%= yield %>
</section>
</div>

<div>
<!--<% if current_user %><% else %>
<%= link_to "Register", new_user_path, :class => "signup_button round" %>
<% end %>-->
</div>
</body>
</html>
12 changes: 12 additions & 0 deletions app/views/messages/create.html.erb
@@ -0,0 +1,12 @@
<h1>Writing message to <%= @recipient.username %></h1>

<%= form_for @message do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_field :content %>
</div>
<div class="actions">
<%= f.submit "Submit" %>
</div>
<% end %>
<%= link_to 'Back', @recipient %>
7 changes: 7 additions & 0 deletions app/views/messages/index.html.erb
@@ -0,0 +1,7 @@
<% @messages.each do |message|%>
<div class="field">
<%= message.content %>
<%= message.user.username %>
</div>

<% end %>
11 changes: 11 additions & 0 deletions app/views/messages/show.html.erb
@@ -0,0 +1,11 @@
<p>
<b>From:</b>
<%= @message.user.username %>
</p>

<p>
<b>Message:</b>
<%= @message.content %>
</p>

<%= link_to 'Back', :root %>
2 changes: 0 additions & 2 deletions app/views/pages/home.html.erb
Expand Up @@ -14,5 +14,3 @@
</tr>
</table>

<h1>Social Network</h1>
<%= link_to 'New User', new_user_path %>
4 changes: 1 addition & 3 deletions app/views/posts/_post.html.erb
Expand Up @@ -8,9 +8,7 @@
<% user = post.user rescue User.find(post.user_id) %>
<% if current_user?(user) %>
<td>
<%= link_to "delete", post, :method => :delete,
:confirm => "You sure?",
:title => post.content %>
<%= link_to "delete", post, :method => :delete,:confirm => "You sure?",:title => post.content %>
</td>
<% end %>

Expand Down
3 changes: 1 addition & 2 deletions app/views/users/index.html.erb
Expand Up @@ -15,8 +15,7 @@
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %></td>
</tr>
</tr>
<% end %>
</table>

Expand Down
8 changes: 4 additions & 4 deletions app/views/users/show.html.erb
@@ -1,12 +1,12 @@
<table class="profile">
<tr>
<td class="sidebar round">
<strong>Name</strong> <%= @user.username %><br />
<strong>Name</strong> <%= link_to @user.username, @user %><br />
<strong>E-mail</strong> <%= @user.email %><br />
<strong>URL</strong> <%= link_to user_path(@user), @user %><br />
<strong>Posts</strong> <%= @user.posts.count %>
<br>
<%= link_to 'Edit', edit_user_path(@user) %> | <%= link_to 'Back', users_path %>
<%= link_to "Downloads", :downloads %>
<br>
<%= link_to 'Edit', edit_user_path(@user) %> | <%= link_to 'Back', users_path %> | <%= link_to 'write', send_message_path(@user) %>
</td>
</tr>
</table>
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20120305154705_create_messages.rb
@@ -0,0 +1,10 @@
class CreateMessages < ActiveRecord::Migration
def change
create_table :messages do |t|
t.integer :user_id
t.text :content

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20120305154753_create_messages_recipients.rb
@@ -0,0 +1,10 @@
class CreateMessagesRecipients < ActiveRecord::Migration
def change
create_table :message_recipients do |t|
t.integer :user_id
t.integer :message_id

t.timestamps
end
end
end

0 comments on commit ce0b365

Please sign in to comment.