diff --git a/app/controllers/admin_posts_controller.rb b/app/controllers/admin_posts_controller.rb index 9012cbfb518..c11a8d92cd3 100644 --- a/app/controllers/admin_posts_controller.rb +++ b/app/controllers/admin_posts_controller.rb @@ -153,7 +153,7 @@ def load_admin_posts def admin_post_params params.require(:admin_post).permit( - :admin_id, :title, :content, :translated_post_id, :language_id, :tag_list, + :title, :content, :translated_post_id, :language_id, :tag_list, :comment_permissions, :moderated_commenting_enabled ) end diff --git a/app/mailers/comment_mailer.rb b/app/mailers/comment_mailer.rb index 3b0be240979..775a9afd9f5 100644 --- a/app/mailers/comment_mailer.rb +++ b/app/mailers/comment_mailer.rb @@ -1,10 +1,10 @@ class CommentMailer < ApplicationMailer # Sends email to an owner of the top-level commentable when a new comment is created - # This may be an admin, in which case we use the admin address instead - def comment_notification(user, comment) + # The recipient may be a User or a plain email address to send to + def comment_notification(recipient, comment) @comment = comment @owner = true - email = user.is_a?(Admin) ? ArchiveConfig.ADMIN_ADDRESS : user.email + email = recipient.is_a?(User) ? recipient.email : recipient mail( to: email, # i18n-tasks-use t("comment_mailer.comment_notification.subject.chapter") @@ -15,11 +15,11 @@ def comment_notification(user, comment) end # Sends email to an owner of the top-level commentable when a comment is edited - # This may be an admin, in which case we use the admin address instead - def edited_comment_notification(user, comment) + # The recipient may be a User or a plain email address to send to + def edited_comment_notification(recipient, comment) @comment = comment @owner = true - email = user.is_a?(Admin) ? ArchiveConfig.ADMIN_ADDRESS : user.email + email = recipient.is_a?(User) ? recipient.email : recipient mail( to: email, # i18n-tasks-use t("comment_mailer.edited_comment_notification.subject.chapter") diff --git a/app/models/admin_post.rb b/app/models/admin_post.rb index 839d28f6f69..f5edcdce432 100644 --- a/app/models/admin_post.rb +++ b/app/models/admin_post.rb @@ -46,6 +46,7 @@ class AdminPost < ApplicationRecord scope :unposted, -> { where(posted: false) } scope :posted, -> { where(posted: true) } + before_validation :set_admin_id before_validation :inherit_translated_post_attributes before_save :apply_tag_list before_save :set_published_at, if: :posted_changed? @@ -59,11 +60,9 @@ def commentable_name end def commentable_owners - begin - [Admin.find(self.admin_id)] - rescue - [] - end + [Admin.find(self.admin_id)] + rescue ActiveRecord::RecordNotFound + [] end def draft? @@ -135,6 +134,12 @@ def expire_cached_home_admin_posts end end + def set_admin_id + return unless User.current_user.is_a?(Admin) + + self.admin_id = User.current_user.id + end + def inherit_translated_post_attributes return if translated_post.blank? diff --git a/app/models/comment.rb b/app/models/comment.rb index 85bf3cc4675..6053e59d0e1 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -217,6 +217,11 @@ def after_update end end + if self.ultimate_parent.is_a?(AdminPost) + CommentMailer.edited_comment_notification(ArchiveConfig.ADMIN_ADDRESS, self).deliver_after_commit + return + end + # send notification to the owner(s) of the ultimate parent, who can be users or admins # at this point, users contains those who've already been notified if users.empty? @@ -264,6 +269,11 @@ def after_create users << parent_comment_owner end + if self.ultimate_parent.is_a?(AdminPost) + CommentMailer.comment_notification(ArchiveConfig.ADMIN_ADDRESS, self).deliver_after_commit + return + end + # send notification to the owner(s) of the ultimate parent, who can be users or admins # at this point, users contains those who've already been notified if users.empty? diff --git a/app/views/admin_posts/_admin_post_form.html.erb b/app/views/admin_posts/_admin_post_form.html.erb index 60c47f0a6ab..be7265da518 100644 --- a/app/views/admin_posts/_admin_post_form.html.erb +++ b/app/views/admin_posts/_admin_post_form.html.erb @@ -4,8 +4,7 @@ <%= error_messages_for @admin_post %>
<%= ts("Post News") %> -

- <%= f.text_field :title, :title =>"title" %>

+

<%= f.text_field :title, title: "title" %>