Merge pull request #2580 from consul/admin_notifications
Admin notifications
This commit is contained in:
@@ -79,6 +79,7 @@
|
||||
//= require send_newsletter_alert
|
||||
//= require managers
|
||||
//= require globalize
|
||||
//= require send_admin_notification_alert
|
||||
|
||||
var initialize_modules = function() {
|
||||
App.Comments.initialize();
|
||||
@@ -124,6 +125,7 @@ var initialize_modules = function() {
|
||||
App.SendNewsletterAlert.initialize();
|
||||
App.Managers.initialize();
|
||||
App.Globalize.initialize();
|
||||
App.SendAdminNotificationAlert.initialize();
|
||||
};
|
||||
|
||||
$(function(){
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
App.SendAdminNotificationAlert =
|
||||
initialize: ->
|
||||
$('#js-send-admin_notification-alert').on 'click', ->
|
||||
confirm(this.dataset.alert);
|
||||
67
app/controllers/admin/admin_notifications_controller.rb
Normal file
67
app/controllers/admin/admin_notifications_controller.rb
Normal file
@@ -0,0 +1,67 @@
|
||||
class Admin::AdminNotificationsController < Admin::BaseController
|
||||
|
||||
def index
|
||||
@admin_notifications = AdminNotification.all
|
||||
end
|
||||
|
||||
def show
|
||||
@admin_notification = AdminNotification.find(params[:id])
|
||||
end
|
||||
|
||||
def new
|
||||
@admin_notification = AdminNotification.new
|
||||
end
|
||||
|
||||
def create
|
||||
@admin_notification = AdminNotification.new(admin_notification_params)
|
||||
|
||||
if @admin_notification.save
|
||||
notice = t("admin.admin_notifications.create_success")
|
||||
redirect_to [:admin, @admin_notification], notice: notice
|
||||
else
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
def edit
|
||||
@admin_notification = AdminNotification.find(params[:id])
|
||||
end
|
||||
|
||||
def update
|
||||
@admin_notification = AdminNotification.find(params[:id])
|
||||
|
||||
if @admin_notification.update(admin_notification_params)
|
||||
notice = t("admin.admin_notifications.update_success")
|
||||
redirect_to [:admin, @admin_notification], notice: notice
|
||||
else
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@admin_notification = AdminNotification.find(params[:id])
|
||||
@admin_notification.destroy
|
||||
|
||||
notice = t("admin.admin_notifications.delete_success")
|
||||
redirect_to admin_admin_notifications_path, notice: notice
|
||||
end
|
||||
|
||||
def deliver
|
||||
@admin_notification = AdminNotification.find(params[:id])
|
||||
|
||||
if @admin_notification.valid?
|
||||
@admin_notification.deliver
|
||||
flash[:notice] = t("admin.admin_notifications.send_success")
|
||||
else
|
||||
flash[:error] = t("admin.segment_recipient.invalid_recipients_segment")
|
||||
end
|
||||
|
||||
redirect_to [:admin, @admin_notification]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def admin_notification_params
|
||||
params.require(:admin_notification).permit(:title, :body, :link, :segment_recipient)
|
||||
end
|
||||
end
|
||||
@@ -44,7 +44,11 @@ class NotificationsController < ApplicationController
|
||||
when "Topic"
|
||||
community_topic_path @notification.linkable_resource.community, @notification.linkable_resource
|
||||
else
|
||||
url_for @notification.linkable_resource
|
||||
if @notification.linkable_resource.is_a?(AdminNotification)
|
||||
@notification.linkable_resource.link || notifications_path
|
||||
else
|
||||
url_for @notification.linkable_resource
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
44
app/models/admin_notification.rb
Normal file
44
app/models/admin_notification.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
class AdminNotification < ActiveRecord::Base
|
||||
include Notifiable
|
||||
|
||||
validates :title, presence: true
|
||||
validates :body, presence: true
|
||||
validates :segment_recipient, presence: true
|
||||
validate :validate_segment_recipient
|
||||
|
||||
before_validation :complete_link_url
|
||||
|
||||
def list_of_recipients
|
||||
UserSegments.send(segment_recipient) if valid_segment_recipient?
|
||||
end
|
||||
|
||||
def valid_segment_recipient?
|
||||
segment_recipient && UserSegments.respond_to?(segment_recipient)
|
||||
end
|
||||
|
||||
def draft?
|
||||
sent_at.nil?
|
||||
end
|
||||
|
||||
def list_of_recipients_count
|
||||
list_of_recipients.try(:count) || 0
|
||||
end
|
||||
|
||||
def deliver
|
||||
list_of_recipients.each { |user| Notification.add(user, self) }
|
||||
self.update(sent_at: Time.current, recipients_count: list_of_recipients.count)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validate_segment_recipient
|
||||
errors.add(:segment_recipient, :invalid) unless valid_segment_recipient?
|
||||
end
|
||||
|
||||
def complete_link_url
|
||||
return unless link.present?
|
||||
unless self.link[/\Ahttp:\/\//] || self.link[/\Ahttps:\/\//]
|
||||
self.link = "http://#{self.link}"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -53,9 +53,19 @@ class Notification < ActiveRecord::Base
|
||||
"proposal_notification"
|
||||
when "Comment"
|
||||
"replies_to"
|
||||
when "AdminNotification"
|
||||
nil
|
||||
else
|
||||
"comments_on"
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
def link
|
||||
if notifiable.is_a?(AdminNotification) && notifiable.link.blank?
|
||||
nil
|
||||
else
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -79,18 +79,21 @@
|
||||
</li>
|
||||
<% end %>
|
||||
|
||||
<% messages_sections = %w(newsletters emails_download) %>
|
||||
<% messages_menu_active = messages_sections.include?(controller_name) %>
|
||||
<li class="section-title" <%= "class=is-active" if messages_menu_active %>>
|
||||
<% newsletters_notifications_sections = %w(newsletters emails_download admin_notifications) %>
|
||||
<% newsletters_menu_active = newsletters_notifications_sections.include?(controller_name) %>
|
||||
<li class="section-title" <%= "class=active" if newsletters_menu_active %>>
|
||||
<a href="#">
|
||||
<span class="icon-zip"></span>
|
||||
<strong><%= t("admin.menu.emails") %></strong>
|
||||
<strong><%= t("admin.menu.newsletters_and_notifications") %></strong>
|
||||
</a>
|
||||
<ul <%= "class=is-active" if messages_menu_active %>>
|
||||
<li <%= "class=is-active" if controller_name == "newsletters" %>>
|
||||
<ul id="newsletters_and_notifications_menu" <%= "class=is-active" if newsletters_menu_active %>>
|
||||
<li <%= "class=active" if controller_name == "newsletters" %>>
|
||||
<%= link_to t("admin.menu.newsletters"), admin_newsletters_path %>
|
||||
</li>
|
||||
<li <%= "class=is-active" if controller_name == "emails_download" %>>
|
||||
<li <%= "class=active" if controller_name == "admin_notifications" %>>
|
||||
<%= link_to t("admin.menu.admin_notifications"), admin_admin_notifications_path %>
|
||||
</li>
|
||||
<li <%= "class=active" if controller_name == "emails_download" %>>
|
||||
<%= link_to t("admin.menu.emails_download"), admin_emails_download_index_path %>
|
||||
</li>
|
||||
</ul>
|
||||
@@ -231,7 +234,6 @@
|
||||
<li <%= "class=is-active" if controller_name == "content_blocks" %>>
|
||||
<%= link_to t("admin.menu.site_customization.content_blocks"), admin_site_customization_content_blocks_path%>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
13
app/views/admin/admin_notifications/_form.html.erb
Normal file
13
app/views/admin/admin_notifications/_form.html.erb
Normal file
@@ -0,0 +1,13 @@
|
||||
<%= form_for [:admin, @admin_notification] do |f| %>
|
||||
<%= render 'shared/errors', resource: @admin_notification %>
|
||||
|
||||
<%= f.select :segment_recipient, options_for_select(user_segments_options,
|
||||
@admin_notification[:segment_recipient]) %>
|
||||
<%= f.text_field :title %>
|
||||
<%= f.text_field :link %>
|
||||
<%= f.text_area :body %>
|
||||
|
||||
<div class="margin-top">
|
||||
<%= f.submit class: "button success" %>
|
||||
</div>
|
||||
<% end %>
|
||||
4
app/views/admin/admin_notifications/edit.html.erb
Normal file
4
app/views/admin/admin_notifications/edit.html.erb
Normal file
@@ -0,0 +1,4 @@
|
||||
<%= back_link_to %>
|
||||
<h2><%= t("admin.admin_notifications.edit.section_title") %></h2>
|
||||
|
||||
<%= render "form" %>
|
||||
56
app/views/admin/admin_notifications/index.html.erb
Normal file
56
app/views/admin/admin_notifications/index.html.erb
Normal file
@@ -0,0 +1,56 @@
|
||||
<h2 class="inline-block"><%= t("admin.admin_notifications.index.section_title") %></h2>
|
||||
<%= link_to t("admin.admin_notifications.index.new_notification"), new_admin_admin_notification_path,
|
||||
class: "button float-right" %>
|
||||
|
||||
<% if @admin_notifications.any? %>
|
||||
<table id="admin_notifications">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><%= t("admin.admin_notifications.index.title") %></th>
|
||||
<th><%= t("admin.admin_notifications.index.segment_recipient") %></th>
|
||||
<th><%= t("admin.admin_notifications.index.sent") %></th>
|
||||
<th class="small-5 text-right"><%= t("admin.admin_notifications.index.actions") %></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @admin_notifications.order(created_at: :desc).each do |admin_notification| %>
|
||||
<tr id="<%= dom_id(admin_notification) %>" class="admin_notification">
|
||||
<td>
|
||||
<%= admin_notification.title %>
|
||||
</td>
|
||||
<td>
|
||||
<%= segment_name(admin_notification.segment_recipient) %>
|
||||
</td>
|
||||
<td>
|
||||
<% if admin_notification.draft? %>
|
||||
<%= t("admin.admin_notifications.index.draft") %>
|
||||
<% else %>
|
||||
<%= l admin_notification.sent_at.to_date %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td class="text-right">
|
||||
<% if admin_notification.draft? %>
|
||||
<%= link_to t("admin.admin_notifications.index.edit"),
|
||||
edit_admin_admin_notification_path(admin_notification),
|
||||
method: :get, class: "button hollow" %>
|
||||
<%= link_to t("admin.admin_notifications.index.delete"),
|
||||
admin_admin_notification_path(admin_notification),
|
||||
method: :delete, class: "button hollow alert" %>
|
||||
<%= link_to t("admin.admin_notifications.index.preview"),
|
||||
admin_admin_notification_path(admin_notification),
|
||||
class: "button" %>
|
||||
<% else %>
|
||||
<%= link_to t("admin.admin_notifications.index.view"),
|
||||
admin_admin_notification_path(admin_notification),
|
||||
class: "button" %>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<% else %>
|
||||
<div data-alert class="callout primary margin-top clear">
|
||||
<%= t("admin.admin_notifications.index.empty_notifications") %>
|
||||
</div>
|
||||
<% end %>
|
||||
4
app/views/admin/admin_notifications/new.html.erb
Normal file
4
app/views/admin/admin_notifications/new.html.erb
Normal file
@@ -0,0 +1,4 @@
|
||||
<%= back_link_to %>
|
||||
<h2><%= t("admin.admin_notifications.new.section_title") %></h2>
|
||||
|
||||
<%= render "form" %>
|
||||
77
app/views/admin/admin_notifications/show.html.erb
Normal file
77
app/views/admin/admin_notifications/show.html.erb
Normal file
@@ -0,0 +1,77 @@
|
||||
<%= back_link_to admin_admin_notifications_path %>
|
||||
|
||||
<h2><%= t("admin.admin_notifications.show.section_title") %></h2>
|
||||
|
||||
<div class="small-12 column">
|
||||
<div class="callout highlight">
|
||||
<div class="row">
|
||||
<div class="small-12 medium-6 column">
|
||||
<strong><%= t("admin.admin_notifications.show.sent_at") %></strong><br>
|
||||
<% if @admin_notification.draft? %>
|
||||
<%= t("admin.admin_notifications.index.draft") %>
|
||||
<% else %>
|
||||
<%= l(@admin_notification.sent_at.to_date) %>
|
||||
<% end %>
|
||||
</div>
|
||||
<div class="small-12 medium-6 column">
|
||||
<strong><%= t("admin.admin_notifications.show.title") %></strong><br>
|
||||
<%= @admin_notification.title %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
|
||||
<div class="small-12 medium-6 column">
|
||||
<strong><%= t("admin.admin_notifications.show.body") %></strong><br>
|
||||
<%= @admin_notification.body %>
|
||||
</div>
|
||||
<div class="small-12 medium-6 column">
|
||||
<strong><%= t("admin.admin_notifications.show.link") %></strong><br>
|
||||
<%= @admin_notification.link %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="small-12 column">
|
||||
<strong><%= t("admin.admin_notifications.show.segment_recipient") %></strong><br>
|
||||
<%= segment_name(@admin_notification.segment_recipient) %>
|
||||
<% if @admin_notification.draft? %>
|
||||
<%= t("admin.admin_notifications.show.will_get_notified",
|
||||
n: @admin_notification.list_of_recipients_count) %>
|
||||
<% else %>
|
||||
<%= t("admin.admin_notifications.show.got_notified",
|
||||
n: @admin_notification.recipients_count) %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="help-text" id="phase-description-help-text">
|
||||
<% if @admin_notification.draft? %>
|
||||
<%= t("admin.admin_notifications.show.preview_guide") %>
|
||||
<% else %>
|
||||
<%= t("admin.admin_notifications.show.sent_guide") %>
|
||||
<% end %>
|
||||
</p>
|
||||
<hr>
|
||||
<div class="admin_notification-body-content">
|
||||
<ul class="no-bullet clear notifications-list">
|
||||
<li class="notification unread">
|
||||
<% locals = { notification: nil,
|
||||
title: @admin_notification.title,
|
||||
body: @admin_notification.body,
|
||||
timestamp: Time.current } %>
|
||||
<% link_text = render partial: '/notifications/notification_body', locals: locals %>
|
||||
<%= link_to_if @admin_notification.link.present?, link_text, @admin_notification.link %>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<hr>
|
||||
</div>
|
||||
<% if @admin_notification.draft? && @admin_notification.valid_segment_recipient? %>
|
||||
<%= link_to t("admin.admin_notifications.show.send"),
|
||||
deliver_admin_admin_notification_path(@admin_notification),
|
||||
"data-alert": t("admin.admin_notifications.show.send_alert",
|
||||
n: @admin_notification.list_of_recipients_count),
|
||||
method: :post,
|
||||
id: "js-send-admin_notification-alert",
|
||||
class: "button success" %>
|
||||
<% end %>
|
||||
@@ -1,21 +1,11 @@
|
||||
<li id="<%= dom_id(notification) %>" class="notification <%= "unread" if notification.unread? %>">
|
||||
|
||||
<% if notification.notifiable_available? %>
|
||||
<%= link_to notification do %>
|
||||
<p>
|
||||
<em>
|
||||
<%= t("notifications.notification.action.#{notification.notifiable_action}",
|
||||
count: notification.counter) %>
|
||||
</em>
|
||||
<strong id="<%= dom_id(notification) %>_title">
|
||||
<%= notification.notifiable_title %>
|
||||
</strong>
|
||||
</p>
|
||||
|
||||
<p class="time">
|
||||
<%= l notification.timestamp, format: :datetime %>
|
||||
</p>
|
||||
<% end %>
|
||||
<li id="<%= dom_id(notification) %>" class="notification <%= 'unread' if notification&.unread? %>">
|
||||
<% if notification.try(:notifiable_available?) %>
|
||||
<% locals = { notification: notification,
|
||||
timestamp: notification.timestamp,
|
||||
title: notification.notifiable_title,
|
||||
body: notification.notifiable.try(:body) } %>
|
||||
<% link_text = render partial: '/notifications/notification_body', locals: locals %>
|
||||
<%= link_to_if notification.link.present?, link_text, notification.link %>
|
||||
<% else %>
|
||||
<p>
|
||||
<strong>
|
||||
|
||||
17
app/views/notifications/_notification_body.html.erb
Normal file
17
app/views/notifications/_notification_body.html.erb
Normal file
@@ -0,0 +1,17 @@
|
||||
<p>
|
||||
<% if notification && notification.notifiable_action %>
|
||||
<em>
|
||||
<%= t("notifications.notification.action.#{notification.notifiable_action}",
|
||||
count: notification.counter) %>
|
||||
</em>
|
||||
<% end %>
|
||||
<strong id="<%= dom_id(notification) if notification %>_title">
|
||||
<%= title %>
|
||||
</strong>
|
||||
<% if body %>
|
||||
<p><%= body %></p>
|
||||
<% end %>
|
||||
</p>
|
||||
<p class="time">
|
||||
<%= l(timestamp, format: :datetime) %>
|
||||
</p>
|
||||
@@ -24,7 +24,9 @@
|
||||
</div>
|
||||
<% else %>
|
||||
<ul class="no-bullet clear notifications-list">
|
||||
<%= render @notifications %>
|
||||
<% @notifications.each do |notification| %>
|
||||
<%= render partial: '/notifications/notification', locals: {notification: notification} %>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user