From 2ca2c94ef60372d027d4c2ee5b107603e80a3cf4 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Thu, 15 Oct 2015 10:01:42 -0500 Subject: [PATCH] allow messages to come from system instead of a user --- app/controllers/messages_controller.rb | 20 +++++---- app/mailers/email_message.rb | 2 +- app/models/message.rb | 14 +++++-- app/views/messages/index.html.erb | 8 +++- app/views/messages/show.html.erb | 57 ++++++++++++++++---------- 5 files changed, 64 insertions(+), 37 deletions(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index fb42ed5..f83b592 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -51,20 +51,22 @@ class MessagesController < ApplicationController @cur_url = "/messages" @title = @message.subject - @new_message = Message.new - @new_message.recipient_username = (@message.author_user_id == @user.id ? - @message.recipient.username : @message.author.username) + if @message.author + @new_message = Message.new + @new_message.recipient_username = (@message.author_user_id == @user.id ? + @message.recipient.username : @message.author.username) + + if @message.subject.match(/^re:/i) + @new_message.subject = @message.subject + else + @new_message.subject = "Re: #{@message.subject}" + end + end if @message.recipient_user_id == @user.id @message.has_been_read = true @message.save end - - if @message.subject.match(/^re:/i) - @new_message.subject = @message.subject - else - @new_message.subject = "Re: #{@message.subject}" - end end def destroy diff --git a/app/mailers/email_message.rb b/app/mailers/email_message.rb index 76fa853..85b196e 100644 --- a/app/mailers/email_message.rb +++ b/app/mailers/email_message.rb @@ -9,7 +9,7 @@ class EmailMessage < ActionMailer::Base mail( :to => user.email, :subject => "[#{Rails.application.name}] Private Message from " << - "#{message.author.username}: #{message.subject}" + "#{message.author_username}: #{message.subject}" ) end end diff --git a/app/models/message.rb b/app/models/message.rb index 2506276..1797de7 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -7,7 +7,6 @@ class Message < ActiveRecord::Base :foreign_key => "author_user_id" validates_presence_of :recipient - validates_presence_of :author attr_accessor :recipient_username @@ -27,6 +26,14 @@ class Message < ActiveRecord::Base self.short_id = ShortId.new(self.class).generate end + def author_username + if self.author + self.author.username + else + "System" + end + end + def check_for_both_deleted if self.deleted_by_author? && self.deleted_by_recipient? self.destroy @@ -51,10 +58,11 @@ class Message < ActiveRecord::Base if self.recipient.pushover_messages? self.recipient.pushover!({ :title => "#{Rails.application.name} message from " << - "#{self.author.username}: #{self.subject}", + "#{self.author_username}: #{self.subject}", :message => self.plaintext_body, :url => self.url, - :url_title => "Reply to #{self.author.username}", + :url_title => (self.author ? "Reply to #{self.author_username}" : + "View message"), }) end end diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb index 046c95b..e333aab 100644 --- a/app/views/messages/index.html.erb +++ b/app/views/messages/index.html.erb @@ -36,8 +36,12 @@ "> <%= check_box_tag "delete_#{message.short_id}" %> <% if @direction == :in %> - <%= - message.author.username %> + <% if message.author %> + <%= + message.author.username %> + <% else %> + <%= message.author_username %> + <% end %> <% else %> <%= message.recipient.username %> diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index d13dae2..a9ec567 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -11,12 +11,17 @@ <%= @message.subject %>
- Sent from <%= - @message.author.username %> - <% if @message.author.is_admin? %> - (administrator) - <% elsif @message.author.is_moderator? %> - (moderator) + Sent from + <% if @message.author %> + <%= + @message.author.username %> + <% if @message.author.is_admin? %> + (administrator) + <% elsif @message.author.is_moderator? %> + (moderator) + <% end %> + <% else %> + <%= @message.author_username %> <% end %> to <%= @@ -51,27 +56,35 @@
- Compose Reply To <%= @new_message.recipient_username %> + Compose Reply + <% if @new_message %> + To <%= @new_message.recipient_username %> + <% end %>
- <%= form_for @new_message, :method => :post do |f| %> - <%= f.hidden_field :recipient_username %> + <% if @new_message %> + <%= form_for @new_message, :method => :post do |f| %> + <%= f.hidden_field :recipient_username %> - <%= error_messages_for @new_message %> + <%= error_messages_for @new_message %> -
- <%= f.text_field :subject, :style => "width: 500px;", - :autocomplete => "off" %> -
+
+ <%= f.text_field :subject, :style => "width: 500px;", + :autocomplete => "off" %> +
-
- <%= f.text_area :body, :style => "width: 500px;", :rows => 5, - :autocomplete => "off" %> -
+
+ <%= f.text_area :body, :style => "width: 500px;", :rows => 5, + :autocomplete => "off" %> +
-
-

- <%= submit_tag "Send Message" %> -
+
+

+ <%= submit_tag "Send Message" %> +
+ <% end %> + <% else %> + This message cannot be replied to. Please contact a moderator for + assistance. <% end %>