From a418221984101a2a75792b25541537d49e206da4 Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 12:30:21 -0500 Subject: [PATCH 01/12] Add topics index and show pages with associated paritials and routes --- app/assets/javascripts/topics.js | 2 ++ app/assets/stylesheets/topics.scss | 3 +++ app/controllers/topics_controller.rb | 10 ++++++++++ app/helpers/topics_helper.rb | 2 ++ app/views/conversations/_conversation.html.erb | 3 +++ app/views/topics/_topic.html.erb | 3 +++ app/views/topics/index.html.erb | 1 + app/views/topics/show.html.erb | 4 ++++ config/routes.rb | 3 +++ 9 files changed, 31 insertions(+) create mode 100644 app/assets/javascripts/topics.js create mode 100644 app/assets/stylesheets/topics.scss create mode 100644 app/controllers/topics_controller.rb create mode 100644 app/helpers/topics_helper.rb create mode 100644 app/views/conversations/_conversation.html.erb create mode 100644 app/views/topics/_topic.html.erb create mode 100644 app/views/topics/index.html.erb create mode 100644 app/views/topics/show.html.erb diff --git a/app/assets/javascripts/topics.js b/app/assets/javascripts/topics.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/topics.js @@ -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. diff --git a/app/assets/stylesheets/topics.scss b/app/assets/stylesheets/topics.scss new file mode 100644 index 0000000..9c30683 --- /dev/null +++ b/app/assets/stylesheets/topics.scss @@ -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/ diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb new file mode 100644 index 0000000..ebc4396 --- /dev/null +++ b/app/controllers/topics_controller.rb @@ -0,0 +1,10 @@ +class TopicsController < ApplicationController + + def index + @topics = Topic.all + end + + def show + @topic = Topic.find_by(id: params[:id]) + end +end diff --git a/app/helpers/topics_helper.rb b/app/helpers/topics_helper.rb new file mode 100644 index 0000000..488eed5 --- /dev/null +++ b/app/helpers/topics_helper.rb @@ -0,0 +1,2 @@ +module TopicsHelper +end diff --git a/app/views/conversations/_conversation.html.erb b/app/views/conversations/_conversation.html.erb new file mode 100644 index 0000000..7664b05 --- /dev/null +++ b/app/views/conversations/_conversation.html.erb @@ -0,0 +1,3 @@ +
+<%= conversation.name %> +
diff --git a/app/views/topics/_topic.html.erb b/app/views/topics/_topic.html.erb new file mode 100644 index 0000000..288c581 --- /dev/null +++ b/app/views/topics/_topic.html.erb @@ -0,0 +1,3 @@ +
+<%= topic.name %> +
diff --git a/app/views/topics/index.html.erb b/app/views/topics/index.html.erb new file mode 100644 index 0000000..df06c56 --- /dev/null +++ b/app/views/topics/index.html.erb @@ -0,0 +1 @@ +<%= render @topics %> diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb new file mode 100644 index 0000000..7756737 --- /dev/null +++ b/app/views/topics/show.html.erb @@ -0,0 +1,4 @@ +<%= @topic.name %> +
+ <%= render @topic.conversations %> +
diff --git a/config/routes.rb b/config/routes.rb index 3f66539..e0a8b53 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Rails.application.routes.draw do + resources :topics + root 'topics#index' + resources :conversations # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 1e782fd5935a65a6464d016ec154683ee0dae256 Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 15:55:06 -0500 Subject: [PATCH 02/12] add view messages and controller --- app/assets/javascripts/conversations.js | 2 ++ app/assets/stylesheets/conversations.scss | 3 +++ app/controllers/conversations_controller.rb | 5 +++++ app/helpers/conversations_helper.rb | 2 ++ app/views/conversations/_conversation.html.erb | 2 +- app/views/conversations/show.html.erb | 4 ++++ app/views/messages/_message.html.erb | 3 +++ app/views/topics/_topic.html.erb | 2 +- app/views/topics/show.html.erb | 2 +- 9 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 app/assets/javascripts/conversations.js create mode 100644 app/assets/stylesheets/conversations.scss create mode 100644 app/controllers/conversations_controller.rb create mode 100644 app/helpers/conversations_helper.rb create mode 100644 app/views/conversations/show.html.erb create mode 100644 app/views/messages/_message.html.erb diff --git a/app/assets/javascripts/conversations.js b/app/assets/javascripts/conversations.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/conversations.js @@ -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. diff --git a/app/assets/stylesheets/conversations.scss b/app/assets/stylesheets/conversations.scss new file mode 100644 index 0000000..188d82c --- /dev/null +++ b/app/assets/stylesheets/conversations.scss @@ -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/ diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb new file mode 100644 index 0000000..9d55367 --- /dev/null +++ b/app/controllers/conversations_controller.rb @@ -0,0 +1,5 @@ +class ConversationsController < ApplicationController + def show + @conversation = Conversation.find_by(id: params[:id]) + end +end diff --git a/app/helpers/conversations_helper.rb b/app/helpers/conversations_helper.rb new file mode 100644 index 0000000..fe30c45 --- /dev/null +++ b/app/helpers/conversations_helper.rb @@ -0,0 +1,2 @@ +module ConversationsHelper +end diff --git a/app/views/conversations/_conversation.html.erb b/app/views/conversations/_conversation.html.erb index 7664b05..6175b94 100644 --- a/app/views/conversations/_conversation.html.erb +++ b/app/views/conversations/_conversation.html.erb @@ -1,3 +1,3 @@
-<%= conversation.name %> + <%= link_to conversation.name, conversation_path(conversation) %>
diff --git a/app/views/conversations/show.html.erb b/app/views/conversations/show.html.erb new file mode 100644 index 0000000..c84e425 --- /dev/null +++ b/app/views/conversations/show.html.erb @@ -0,0 +1,4 @@ +

<%= @conversation.name %>

+
+ <%= render @conversation.messages %> +
diff --git a/app/views/messages/_message.html.erb b/app/views/messages/_message.html.erb new file mode 100644 index 0000000..cff0633 --- /dev/null +++ b/app/views/messages/_message.html.erb @@ -0,0 +1,3 @@ +
+ <%= message.content %> +
diff --git a/app/views/topics/_topic.html.erb b/app/views/topics/_topic.html.erb index 288c581..55a2810 100644 --- a/app/views/topics/_topic.html.erb +++ b/app/views/topics/_topic.html.erb @@ -1,3 +1,3 @@
-<%= topic.name %> + <%= link_to topic.name, topic_path(topic) %>
diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb index 7756737..c649d92 100644 --- a/app/views/topics/show.html.erb +++ b/app/views/topics/show.html.erb @@ -1,4 +1,4 @@ -<%= @topic.name %> +

<%= @topic.name %>

<%= render @topic.conversations %>
From f36de660893459a085cb647712857c3d6d17fe9b Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 16:18:30 -0500 Subject: [PATCH 03/12] Finish Release 0 Added recent/archived conversation functionality --- app/controllers/topics_controller.rb | 2 ++ app/models/conversation.rb | 9 +++++++++ app/views/topics/show.html.erb | 17 ++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index ebc4396..53dc636 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -6,5 +6,7 @@ def index def show @topic = Topic.find_by(id: params[:id]) + @recent_convos = Conversation.recent(@topic) + @old_convos = Conversation.old(@topic) end end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 7014762..2370a78 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -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 diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb index c649d92..b8d8833 100644 --- a/app/views/topics/show.html.erb +++ b/app/views/topics/show.html.erb @@ -1,4 +1,19 @@

<%= @topic.name %>

+
- <%= render @topic.conversations %> +

Recently Updated

+ <% @recent_convos.each do |convo| %> +
+ <%= link_to convo.name, conversation_path(convo) %> +
+ <% end %> +
+ +
+

Archived Conversations

+ <% @old_convos.each do |convo| %> +
+ <%= link_to convo.name, conversation_path(convo) %> +
+ <% end %>
From df3ccce80475940d55fd065c3128fd097984eabb Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 16:28:29 -0500 Subject: [PATCH 04/12] Change each display methods to render --- app/views/topics/show.html.erb | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb index b8d8833..96dd8cb 100644 --- a/app/views/topics/show.html.erb +++ b/app/views/topics/show.html.erb @@ -2,18 +2,10 @@

Recently Updated

- <% @recent_convos.each do |convo| %> -
- <%= link_to convo.name, conversation_path(convo) %> -
- <% end %> + <%= render @recent_convos %>

Archived Conversations

- <% @old_convos.each do |convo| %> -
- <%= link_to convo.name, conversation_path(convo) %> -
- <% end %> + <%= render @old_convos %>
From 1f4b3a350aa386e69e577c9dd2c8a7c58233d55d Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 16:48:14 -0500 Subject: [PATCH 05/12] Add login feature --- app/assets/javascripts/session.js | 2 ++ app/assets/stylesheets/session.scss | 3 +++ app/controllers/session_controller.rb | 14 ++++++++++++++ app/helpers/session_helper.rb | 2 ++ app/views/login.html.erb | 13 +++++++++++++ config/routes.rb | 3 +++ 6 files changed, 37 insertions(+) create mode 100644 app/assets/javascripts/session.js create mode 100644 app/assets/stylesheets/session.scss create mode 100644 app/controllers/session_controller.rb create mode 100644 app/helpers/session_helper.rb create mode 100644 app/views/login.html.erb diff --git a/app/assets/javascripts/session.js b/app/assets/javascripts/session.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/session.js @@ -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. diff --git a/app/assets/stylesheets/session.scss b/app/assets/stylesheets/session.scss new file mode 100644 index 0000000..6fa5e44 --- /dev/null +++ b/app/assets/stylesheets/session.scss @@ -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/ diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb new file mode 100644 index 0000000..259525e --- /dev/null +++ b/app/controllers/session_controller.rb @@ -0,0 +1,14 @@ +class SessionController < ApplicationController + def create + u = User.find_by(username: params[:username]) + if u && u.authenticate(params[:password]) + session[:user_id] = u.id + else + render '/login' + end + end + + def destroy + + end +end diff --git a/app/helpers/session_helper.rb b/app/helpers/session_helper.rb new file mode 100644 index 0000000..f867f86 --- /dev/null +++ b/app/helpers/session_helper.rb @@ -0,0 +1,2 @@ +module SessionHelper +end diff --git a/app/views/login.html.erb b/app/views/login.html.erb new file mode 100644 index 0000000..dd1661a --- /dev/null +++ b/app/views/login.html.erb @@ -0,0 +1,13 @@ +

Log in, baby!

+ +<%= form_tag login_path do %> +
+ <%= label_tag :username %> + <%= text_field_tag :username %> +
+
+ <%= label_tag :password %> + <%= password_field_tag :password %> +
+<%= submit_tag 'Log in!' %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index e0a8b53..e6adf43 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,9 @@ resources :topics root 'topics#index' resources :conversations + resources :session, only: [:create] + get '/login' => 'session#create' + delete '/logout' => 'session#destroy' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 0f27184418d6e959bce832dfce54463f92335eed Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 17:05:19 -0500 Subject: [PATCH 06/12] Add create new user feature --- app/assets/javascripts/users.js | 2 ++ app/assets/stylesheets/users.scss | 3 +++ app/controllers/session_controller.rb | 2 +- app/controllers/users_controller.rb | 9 +++++++++ app/helpers/users_helper.rb | 2 ++ app/views/layouts/application.html.erb | 7 ++++++- app/views/users/new.html.erb | 17 +++++++++++++++++ config/routes.rb | 1 + 8 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/users.js create mode 100644 app/assets/stylesheets/users.scss create mode 100644 app/controllers/users_controller.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/views/users/new.html.erb diff --git a/app/assets/javascripts/users.js b/app/assets/javascripts/users.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/users.js @@ -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. diff --git a/app/assets/stylesheets/users.scss b/app/assets/stylesheets/users.scss new file mode 100644 index 0000000..1efc835 --- /dev/null +++ b/app/assets/stylesheets/users.scss @@ -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/ diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index 259525e..978a06c 100644 --- a/app/controllers/session_controller.rb +++ b/app/controllers/session_controller.rb @@ -9,6 +9,6 @@ def create end def destroy - + session.clear end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..b441f55 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,9 @@ +class UsersController < ApplicationController + def new + @user = User.new + end + + def create + @user = User.new(params[:user]) + end +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000..2310a24 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2e8bd07..9fad925 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,7 +7,12 @@ <%= csrf_meta_tags %> - +
+
+ <%= link_to 'login', login_path %> + <%= link_to 'register', new_user_path %> +
+
<%= yield %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb new file mode 100644 index 0000000..d7c278c --- /dev/null +++ b/app/views/users/new.html.erb @@ -0,0 +1,17 @@ +

So you wanna register, huh?

+ +<%= form_for @user do |f| %> +
+ <%= f.label :username %> + <%= f.text_field :username %> +
+
+ <%= f.label :email %> + <%= f.email_field :email %> +
+
+ <%= f.label :password %> + <%= f.password_field :password %> +
+ <%= f.submit %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index e6adf43..2eaa49b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ resources :session, only: [:create] get '/login' => 'session#create' delete '/logout' => 'session#destroy' + resources :users # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 2f5cc1c5b145b10cf769faa7a98f875258225415 Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 17:10:28 -0500 Subject: [PATCH 07/12] Add permitted_params method for user controller --- app/controllers/users_controller.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index b441f55..6c73ea8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,6 +4,12 @@ def new end def create - @user = User.new(params[:user]) + @user = User.new(permitted_params) end + + private + def permitted_params + params.require(:user).permit(:username, :email, :password) + end + end From d19447a2dea6f1faf5494714cd60b1add99729da Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 18:00:17 -0500 Subject: [PATCH 08/12] Fix login feature --- app/controllers/application_controller.rb | 8 ++++++++ app/controllers/session_controller.rb | 4 +++- app/controllers/users_controller.rb | 10 +++++++--- app/helpers/application_helper.rb | 7 +++++++ app/views/layouts/application.html.erb | 4 ++++ app/views/{login.html.erb => session/new.html.erb} | 2 +- config/routes.rb | 4 ++-- 7 files changed, 32 insertions(+), 7 deletions(-) rename app/views/{login.html.erb => session/new.html.erb} (85%) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..afb62e3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index 978a06c..321a627 100644 --- a/app/controllers/session_controller.rb +++ b/app/controllers/session_controller.rb @@ -1,10 +1,12 @@ 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 '/login' + render '/session/new' end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 6c73ea8..4323507 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,12 +4,16 @@ def new end def create - @user = User.new(permitted_params) + @user = User.new(user_params) + if @user.save + redirect_to root_path + else + render :'users/new' + end end private - def permitted_params + def user_params params.require(:user).permit(:username, :email, :password) end - end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..ef9c60f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9fad925..d65dd65 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,8 +9,12 @@
+ <% if logged_in? %> + <%= link_to 'logout', logout_path %> + <% else %> <%= link_to 'login', login_path %> <%= link_to 'register', new_user_path %> + <% end %>
<%= yield %> diff --git a/app/views/login.html.erb b/app/views/session/new.html.erb similarity index 85% rename from app/views/login.html.erb rename to app/views/session/new.html.erb index dd1661a..6b8a367 100644 --- a/app/views/login.html.erb +++ b/app/views/session/new.html.erb @@ -1,6 +1,6 @@

Log in, baby!

-<%= form_tag login_path do %> +<%= form_tag session_index_path do %>
<%= label_tag :username %> <%= text_field_tag :username %> diff --git a/config/routes.rb b/config/routes.rb index 2eaa49b..7cfbbec 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,8 +3,8 @@ root 'topics#index' resources :conversations resources :session, only: [:create] - get '/login' => 'session#create' - delete '/logout' => 'session#destroy' + get '/login' => 'session#new' + get '/logout' => 'session#destroy' resources :users # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From eaa0fce2bcec36d986ae612d2343c6ae38711530 Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 18:07:24 -0500 Subject: [PATCH 09/12] Add greeting for logged in user --- app/views/layouts/application.html.erb | 3 ++- app/views/login.html.erb | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 app/views/login.html.erb diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index d65dd65..f465581 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,7 +10,8 @@
<% if logged_in? %> - <%= link_to 'logout', logout_path %> +

Hey, <%= current_user.username %> + | <%= link_to 'logout', logout_path %>

<% else %> <%= link_to 'login', login_path %> <%= link_to 'register', new_user_path %> diff --git a/app/views/login.html.erb b/app/views/login.html.erb new file mode 100644 index 0000000..6b8a367 --- /dev/null +++ b/app/views/login.html.erb @@ -0,0 +1,13 @@ +

Log in, baby!

+ +<%= form_tag session_index_path do %> +
+ <%= label_tag :username %> + <%= text_field_tag :username %> +
+
+ <%= label_tag :password %> + <%= password_field_tag :password %> +
+<%= submit_tag 'Log in!' %> +<% end %> From 3be0ec49294a714abefc0cf6b831f9752b6ccd30 Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 18:46:28 -0500 Subject: [PATCH 10/12] Add create message feature --- Gemfile | 1 + Gemfile.lock | 8 ++++++++ app/assets/javascripts/messages.js | 2 ++ app/assets/stylesheets/messages.scss | 3 +++ app/controllers/messages_controller.rb | 14 ++++++++++++++ app/helpers/messages_helper.rb | 2 ++ app/views/conversations/show.html.erb | 7 +++++++ config/routes.rb | 6 ++++-- 8 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/messages.js create mode 100644 app/assets/stylesheets/messages.scss create mode 100644 app/controllers/messages_controller.rb create mode 100644 app/helpers/messages_helper.rb diff --git a/Gemfile b/Gemfile index dfcb289..e652ec9 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index bad1603..6769007 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -62,6 +63,7 @@ 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) @@ -69,6 +71,10 @@ GEM 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) @@ -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) @@ -138,6 +145,7 @@ DEPENDENCIES jbuilder (~> 2.0) jquery-rails pg + pry rails (= 4.2.1) sass-rails (~> 5.0) sdoc (~> 0.4.0) diff --git a/app/assets/javascripts/messages.js b/app/assets/javascripts/messages.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/messages.js @@ -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. diff --git a/app/assets/stylesheets/messages.scss b/app/assets/stylesheets/messages.scss new file mode 100644 index 0000000..830c7b3 --- /dev/null +++ b/app/assets/stylesheets/messages.scss @@ -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/ diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb new file mode 100644 index 0000000..2a36606 --- /dev/null +++ b/app/controllers/messages_controller.rb @@ -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 diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb new file mode 100644 index 0000000..f1bca9f --- /dev/null +++ b/app/helpers/messages_helper.rb @@ -0,0 +1,2 @@ +module MessagesHelper +end diff --git a/app/views/conversations/show.html.erb b/app/views/conversations/show.html.erb index c84e425..db0ead0 100644 --- a/app/views/conversations/show.html.erb +++ b/app/views/conversations/show.html.erb @@ -2,3 +2,10 @@
<%= render @conversation.messages %>
+ +<%= form_tag message_create_path do %> + +<%= label_tag 'Your Message:'%> +<%= text_area_tag :content %> +<%= submit_tag 'post it!' %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 7cfbbec..32e5495 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,13 @@ Rails.application.routes.draw do + resources :users resources :topics root 'topics#index' - resources :conversations resources :session, only: [:create] get '/login' => 'session#new' get '/logout' => 'session#destroy' - resources :users + resources :conversations + 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". From 6328485724d6342aecb6c1ae56cc03bf7424068d Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 19:11:28 -0500 Subject: [PATCH 11/12] Add create conversation functionality --- app/controllers/conversations_controller.rb | 13 +++++++++++++ app/views/topics/show.html.erb | 8 ++++++++ config/routes.rb | 3 ++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index 9d55367..44b642d 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -1,4 +1,17 @@ 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 diff --git a/app/views/topics/show.html.erb b/app/views/topics/show.html.erb index 96dd8cb..aba71ff 100644 --- a/app/views/topics/show.html.erb +++ b/app/views/topics/show.html.erb @@ -1,5 +1,13 @@

<%= @topic.name %>

+<%= form_tag conversation_create_path do %> + +<%= label_tag 'Start a new conversation'%> +<%= text_field_tag :name %> +<%= submit_tag 'Get it started!' %> +<% end %> + +

Recently Updated

<%= render @recent_convos %> diff --git a/config/routes.rb b/config/routes.rb index 32e5495..fe8e848 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,8 @@ resources :session, only: [:create] get '/login' => 'session#new' get '/logout' => 'session#destroy' - resources :conversations + 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. From 16a2f60844b2df9c24153b635de202b5b070be72 Mon Sep 17 00:00:00 2001 From: Michael & Ian Lockwood Date: Mon, 30 Nov 2015 19:44:58 -0500 Subject: [PATCH 12/12] Add recent messages and conversations on user show page --- app/controllers/session_controller.rb | 1 + app/controllers/users_controller.rb | 4 ++++ app/models/message.rb | 4 ++++ app/views/layouts/application.html.erb | 2 +- app/views/users/show.html.erb | 7 +++++++ 5 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 app/views/users/show.html.erb diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index 321a627..53cede0 100644 --- a/app/controllers/session_controller.rb +++ b/app/controllers/session_controller.rb @@ -12,5 +12,6 @@ def create def destroy session.clear + redirect_to root_path end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4323507..a4d99a4 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -12,6 +12,10 @@ def create end end + def show + @messages = Message.recent(current_user) + end + private def user_params params.require(:user).permit(:username, :email, :password) diff --git a/app/models/message.rb b/app/models/message.rb index 6f26294..b5420b1 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index f465581..236d5aa 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,7 +10,7 @@
<% if logged_in? %> -

Hey, <%= current_user.username %> +

Hey, <%= link_to current_user.username, user_path(current_user.id) %> | <%= link_to 'logout', logout_path %>

<% else %> <%= link_to 'login', login_path %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000..daa6f4e --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,7 @@ +

<%= current_user.username %>

+

Your messages:

+<%= render @messages %> + + +

Your conversations:

+<%= render current_user.conversations %>