Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pair heylittlehouse,ianlockwood #4

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
gem 'pry'

# Use Unicorn as the app server
# gem 'unicorn'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ GEM
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (6.0.2)
coderay (1.1.0)
debug_inspector (0.0.2)
erubis (2.7.0)
execjs (2.6.0)
Expand All @@ -62,13 +63,18 @@ GEM
nokogiri (>= 1.5.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.6.2)
mini_portile (0.6.2)
minitest (5.8.1)
multi_json (1.11.2)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
pg (0.18.3)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -108,6 +114,7 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
slop (3.6.0)
sprockets (3.4.0)
rack (> 1, < 3)
sprockets-rails (2.3.3)
Expand Down Expand Up @@ -138,6 +145,7 @@ DEPENDENCIES
jbuilder (~> 2.0)
jquery-rails
pg
pry
rails (= 4.2.1)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/conversations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/topics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/conversations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the conversations controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/messages.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the messages controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/session.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the session controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/topics.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the topics controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/users.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end

def logged_in?
!!current_user
end
end
18 changes: 18 additions & 0 deletions app/controllers/conversations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class ConversationsController < ApplicationController
def new
@conversation = Conversation.new
end

def create
@conversation = Conversation.new(user_id: current_user.id, topic_id: params[:topic_id], name: params[:name])
if @conversation.save
redirect_to topic_path(@conversation.topic_id)
else
redirect_to topic_path(@conversation.topic_id)
end
end

def show
@conversation = Conversation.find_by(id: params[:id])
end
end
14 changes: 14 additions & 0 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class MessagesController < ApplicationController
def new
@message = Message.new
end

def create
@message = Message.new(user_id: current_user.id, content: params[:content], conversation_id: params[:conversation_id])
if @message.save
redirect_to conversation_path(@message.conversation_id)
else
redirect_to conversation_path(@message.conversation_id)
end
end
end
17 changes: 17 additions & 0 deletions app/controllers/session_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class SessionController < ApplicationController

def create
u = User.find_by(username: params[:username])
if u && u.authenticate(params[:password])
session[:user_id] = u.id
redirect_to root_path
else
render '/session/new'
end
end

def destroy
session.clear
redirect_to root_path
end
end
12 changes: 12 additions & 0 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class TopicsController < ApplicationController

def index
@topics = Topic.all
end

def show
@topic = Topic.find_by(id: params[:id])
@recent_convos = Conversation.recent(@topic)
@old_convos = Conversation.old(@topic)
end
end
23 changes: 23 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class UsersController < ApplicationController
def new
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
redirect_to root_path
else
render :'users/new'
end
end

def show
@messages = Message.recent(current_user)
end

private
def user_params
params.require(:user).permit(:username, :email, :password)
end
end
7 changes: 7 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
module ApplicationHelper
def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end

def logged_in?
!!current_user
end
end
2 changes: 2 additions & 0 deletions app/helpers/conversations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ConversationsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/messages_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MessagesHelper
end
2 changes: 2 additions & 0 deletions app/helpers/session_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SessionHelper
end
2 changes: 2 additions & 0 deletions app/helpers/topics_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TopicsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
9 changes: 9 additions & 0 deletions app/models/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@ class Conversation < ActiveRecord::Base
belongs_to :topic
belongs_to :author, class_name: 'User', foreign_key: :user_id
has_many :messages

def self.recent(topic)
Conversation.where("updated_at >= ? and topic_id = ?", Date.today, topic.id)
end

def self.old(topic)
Conversation.where("updated_at < ? and topic_id = ?", Date.today, topic.id)
end

end
4 changes: 4 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :user

def self.recent(user)
Message.where("updated_at >= ? and user_id = ?", Date.today, user.id)
end
end
3 changes: 3 additions & 0 deletions app/views/conversations/_conversation.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<%= link_to conversation.name, conversation_path(conversation) %>
</div>
11 changes: 11 additions & 0 deletions app/views/conversations/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h2><%= @conversation.name %></h2>
<div>
<%= render @conversation.messages %>
</div>

<%= form_tag message_create_path do %>
<input type="hidden" name="conversation_id" value="<%= @conversation.id %>"/>
<%= label_tag 'Your Message:'%>
<%= text_area_tag :content %>
<%= submit_tag 'post it!' %>
<% end %>
12 changes: 11 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
<%= csrf_meta_tags %>
</head>
<body>

<header>
<div>
<% if logged_in? %>
<p>Hey, <%= link_to current_user.username, user_path(current_user.id) %>
| <%= link_to 'logout', logout_path %> </p>
<% else %>
<%= link_to 'login', login_path %>
<%= link_to 'register', new_user_path %>
<% end %>
</div>
</header>
<%= yield %>

</body>
Expand Down
13 changes: 13 additions & 0 deletions app/views/login.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2>Log in, baby!</h2>

<%= form_tag session_index_path do %>
<div>
<%= label_tag :username %>
<%= text_field_tag :username %>
</div>
<div>
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<%= submit_tag 'Log in!' %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/messages/_message.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<%= message.content %>
</div>
13 changes: 13 additions & 0 deletions app/views/session/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2>Log in, baby!</h2>

<%= form_tag session_index_path do %>
<div>
<%= label_tag :username %>
<%= text_field_tag :username %>
</div>
<div>
<%= label_tag :password %>
<%= password_field_tag :password %>
</div>
<%= submit_tag 'Log in!' %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/topics/_topic.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<%= link_to topic.name, topic_path(topic) %>
</div>
1 change: 1 addition & 0 deletions app/views/topics/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render @topics %>
19 changes: 19 additions & 0 deletions app/views/topics/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<h2><%= @topic.name %></h2>

<%= form_tag conversation_create_path do %>
<input type="hidden" name="topic_id" value="<%= @topic.id %>"/>
<%= label_tag 'Start a new conversation'%>
<%= text_field_tag :name %>
<%= submit_tag 'Get it started!' %>
<% end %>


<div>
<h3>Recently Updated</h3>
<%= render @recent_convos %>
</div>

<div>
<h3>Archived Conversations</h3>
<%= render @old_convos %>
</div>
17 changes: 17 additions & 0 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h2>So you wanna register, huh?</h2>

<%= form_for @user do |f| %>
<div>
<%= f.label :username %>
<%= f.text_field :username %>
</div>
<div>
<%= f.label :email %>
<%= f.email_field :email %>
</div>
<div>
<%= f.label :password %>
<%= f.password_field :password %>
</div>
<%= f.submit %>
<% end %>
7 changes: 7 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h2><%= current_user.username %></h2>
<h3> Your messages:</h3>
<%= render @messages %>


<h3> Your conversations:</h3>
<%= render current_user.conversations %>
10 changes: 10 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
Rails.application.routes.draw do
resources :users
resources :topics
root 'topics#index'
resources :session, only: [:create]
get '/login' => 'session#new'
get '/logout' => 'session#destroy'
resources :conversations, except: [:create]
post '/conversation/create' => 'conversations#create'
resources :messages, except: [:create]
post '/message/create' => 'messages#create'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down