# HG changeset patch # User Nattee Niparnan # Date 2019-10-10 04:38:18 # Node ID 4b83dfaa53e245ec0bdf1c8f79db82d9590eb1aa # Parent 3ce2a4681a0f0f4e2dc72ea49d7ba29539192529 update message console diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -1,54 +1,50 @@ class MessagesController < ApplicationController - before_action :authenticate + before_action :check_valid_login + before_action :set_message, only: ['show', 'reply'] - before_filter :admin_authorization, :only => ['console','show', + before_action :admin_authorization, :only => ['console','show', 'reply','hide','list_all'] - def list - @user = User.find(session[:user_id]) - @messages = Message.find_all_sent_by_user(@user) + def index + @messages = Message.find_all_sent_by_user(@current_user) end def console - @user = User.find(session[:user_id]) @messages = Message.find_all_system_unreplied_messages end def show - @message = Message.find(params[:id]) end def list_all - @user = User.find(session[:user_id]) @messages = Message.where(receiver_id: nil).order(:created_at) end def create - user = User.find(session[:user_id]) - @message = Message.new(params[:message]) - @message.sender = user + @message = Message.new(message_params) + @message.sender = @current_user if @message.body == '' or !@message.save flash[:notice] = 'An error occurred' else flash[:notice] = 'New message posted' end - redirect_to :action => 'list' + redirect_to action: 'index' end def reply - user = User.find(session[:user_id]) - @message = Message.new(params[:r_message]) - @message.sender = user + @r_message = Message.new(message_params) + @r_message.receiver = @message.sender + @r_message.sender = @current_user if @message.body == '' or !@message.save flash[:notice] = 'An error occurred' redirect_to :action => 'show', :id => @message.replying_message_id else flash[:notice] = 'Message replied' - rep_msg = @message.replying_message - rep_msg.replied = true - rep_msg.save + @message.replied = true + @message.replying_message = @r_message + @message.save redirect_to :action => 'console' end end @@ -62,23 +58,31 @@ end protected - def build_replying_message_hierarchy(user) - @all_messages = {} + def build_replying_message_hierarchy(user) + @all_messages = {} - # manually build replies hierarchy (to improve efficiency) - [@messages, @replied_messages].each do |collection| - collection.each do |m| - @all_messages[m.id] = {:msg => m, :replies => []} + # manually build replies hierarchy (to improve efficiency) + [@messages, @replied_messages].each do |collection| + collection.each do |m| + @all_messages[m.id] = {:msg => m, :replies => []} + end + end + + @all_messages.each do |m| + rep_id = m.replying_message_id + if @all_messages[rep_id]!=nil + @all_messages[rep_id][:replies] << m + end end end - @all_messages.each do |m| - rep_id = m.replying_message_id - if @all_messages[rep_id]!=nil - @all_messages[rep_id][:replies] << m - end + def set_message + @message = Message.find(params[:id]) end - end + + def message_params + params.require(:message).permit(:body) + end end diff --git a/app/views/messages/console.html.haml b/app/views/messages/console.html.haml --- a/app/views/messages/console.html.haml +++ b/app/views/messages/console.html.haml @@ -1,4 +1,3 @@ -= user_title_bar(@user) %h1 Console: active messages diff --git a/app/views/messages/index.html.haml b/app/views/messages/index.html.haml new file mode 100644 --- /dev/null +++ b/app/views/messages/index.html.haml @@ -0,0 +1,24 @@ +.announcementbox + %span{:class => 'title'} + How to submit clarification requests + .announcement + %p + :markdown + The clarification requests should be phrased as yes/no questions. + The answers will be one of the following: + (1) **YES**, + (2) NO, + (3) **ANSWERED IN TASK DESCRIPTION (EXPLICITLY OR IMPLICITLY)**, + (4) **INVALID QUESTION**, and + (5) **NO COMMENT**. + += form_for 'message', :url => { :action => 'create'} do |f| + %p + %b New clarification request + = submit_tag "Post" + %br/ + = f.text_area :body, :rows => 5, :cols => 100 + +%hr/ + += render :partial => 'message', :collection => @messages, :locals => {:reply => false} diff --git a/app/views/messages/list.html.haml b/app/views/messages/list.html.haml deleted file mode 100644 --- a/app/views/messages/list.html.haml +++ /dev/null @@ -1,26 +0,0 @@ -= user_title_bar(@user) - -.announcementbox - %span{:class => 'title'} - How to submit clarification requests - .announcement - %p - :markdown - The clarification requests should be phrased as yes/no questions. - The answers will be one of the following: - (1) **YES**, - (2) NO, - (3) **ANSWERED IN TASK DESCRIPTION (EXPLICITLY OR IMPLICITLY)**, - (4) **INVALID QUESTION**, and - (5) **NO COMMENT**. - -= form_for 'message', :url => { :action => 'create'} do |f| - %p - %b New clarification request - = submit_tag "Post" - %br/ - = f.text_area :body, :rows => 5, :cols => 100 - -%hr/ - -= render :partial => 'message', :collection => @messages, :locals => {:reply => false} diff --git a/app/views/messages/list_all.html.haml b/app/views/messages/list_all.html.haml --- a/app/views/messages/list_all.html.haml +++ b/app/views/messages/list_all.html.haml @@ -1,5 +1,3 @@ -= user_title_bar(@user) - %h1 Console: all messages = link_to '[active messages]', :action => 'list_all' diff --git a/app/views/messages/show.html.haml b/app/views/messages/show.html.haml --- a/app/views/messages/show.html.haml +++ b/app/views/messages/show.html.haml @@ -6,10 +6,10 @@ .body= simple_format(@message.body) %h3 Your reply: -= form_for 'r_message', :url => { :action => 'reply'} do |f| += form_for 'message', url: reply_message_path(@message) do |f| = f.text_area :body, :rows => 5, :cols => 100 - = f.hidden_field :receiver_id, {:value => @message.sender_id } - = f.hidden_field :replying_message_id, {:value => @message.id } + -#= f.hidden_field :receiver_id, {:value => @message.sender_id } + -#= f.hidden_field :replying_message_id, {:value => @message.id } = submit_tag "Post" %p diff --git a/config/routes.rb b/config/routes.rb --- a/config/routes.rb +++ b/config/routes.rb @@ -12,8 +12,13 @@ resources :test resources :messages do + member do + get 'hide' + post 'reply' + end collection do get 'console' + get 'list_all' end end