Show More
Commit Description:
upgrade to rails 5.2
Commit Description:
upgrade to rails 5.2
References:
File last commit:
Show/Diff file:
Action:
app/controllers/messages_controller.rb
| 86 lines
| 2.2 KiB
| text/x-ruby
| RubyLexer
|
|
r102 | class MessagesController < ApplicationController | ||
r745 | before_action :authenticate | |||
|
r102 | |||
verify :method => :post, :only => ['create'], | ||||
:redirect_to => { :action => 'list' } | ||||
|
r173 | before_filter :admin_authorization, :only => ['console','show', | ||
|
r357 | 'reply','hide','list_all'] | ||
|
r102 | |||
def list | ||||
@user = User.find(session[:user_id]) | ||||
@messages = Message.find_all_sent_by_user(@user) | ||||
end | ||||
r456 | ||||
|
r102 | def console | ||
@user = User.find(session[:user_id]) | ||||
@messages = Message.find_all_system_unreplied_messages | ||||
end | ||||
def show | ||||
@message = Message.find(params[:id]) | ||||
end | ||||
|
r357 | def list_all | ||
@user = User.find(session[:user_id]) | ||||
@messages = Message.where(receiver_id: nil).order(:created_at) | ||||
end | ||||
|
r102 | def create | ||
user = User.find(session[:user_id]) | ||||
@message = Message.new(params[:message]) | ||||
@message.sender = user | ||||
|
r377 | if @message.body == '' or !@message.save | ||
flash[:notice] = 'An error occurred' | ||||
|
r102 | else | ||
flash[:notice] = 'New message posted' | ||||
end | ||||
|
r377 | redirect_to :action => 'list' | ||
|
r102 | end | ||
def reply | ||||
user = User.find(session[:user_id]) | ||||
@message = Message.new(params[:r_message]) | ||||
@message.sender = user | ||||
|
r377 | if @message.body == '' or !@message.save | ||
flash[:notice] = 'An error occurred' | ||||
redirect_to :action => 'show', :id => @message.replying_message_id | ||||
|
r102 | else | ||
flash[:notice] = 'Message replied' | ||||
rep_msg = @message.replying_message | ||||
rep_msg.replied = true | ||||
rep_msg.save | ||||
redirect_to :action => 'console' | ||||
end | ||||
end | ||||
|
r173 | def hide | ||
message = Message.find(params[:id]) | ||||
message.replied = true | ||||
message.save | ||||
|
r377 | flash[:notice] = 'Message hidden (just marked replied)' | ||
|
r173 | redirect_to :action => 'console' | ||
end | ||||
|
r102 | protected | ||
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 => []} | ||||
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 | ||||
end | ||||