Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notification #607

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/controllers/web/account/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

class Web::Account::NotificationsController < Web::Account::ApplicationController
def index
@notifications = current_user.notifications.includes(:resource).order(created_at: :desc)
query = { s: 'created_at desc' }.merge(params.permit![:q] || {})
@q = current_user.notifications.includes(:resource).ransack(query)
@notifications = @q.result.page(params[:page])
end

def update
notification = Notification.find(params[:id])
authorize notification
notification.mark_as_read!
f(:success)
redirect_to account_notifications_path
end
end
4 changes: 2 additions & 2 deletions app/controllers/web/admin/careers/members_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def new
end

def create
@career_member = resource_career.members.build(career_member_params)
if @career_member.save
@career_member = Career::MemberMutator.create(resource_career, career_member_params)
if @career_member.persisted?
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а это как связано с уведомлениями?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Уведомление с kind: new_career_member создается когда создается новый career_member то есть назначается карерный трек

EmailSender.send_new_career_member_email(@career_member)
f(:success)
redirect_to admin_career_path(resource_career)
Expand Down
8 changes: 8 additions & 0 deletions app/lib/notifications_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,13 @@ def new_answer_like_params(resource)
answer_path: resume_path(resource.resume, locale: I18n.locale, anchor: "answer-#{resource.answer.id}")
}
end

def new_career_member_params(resource)
career = resource.career
{
career: career.name,
career_path: career_member_path(career, resource, locale: I18n.locale)
}
end
end
end
8 changes: 6 additions & 2 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
class Notification < ApplicationRecord
include AASM

validates :kind, inclusion: { in: %w[new_answer new_comment new_answer_like new_answer_comment answer_applied] }
validates :kind, inclusion: { in: %w[new_answer new_comment new_answer_like new_answer_comment answer_applied new_career_member] }
validates :resource_type, presence: true

belongs_to :user
belongs_to :resource, polymorphic: true

aasm column: :state do
aasm :state do
state :unread, initial: true
state :read

event :mark_as_read do
transitions from: :unread, to: :read
end
end

def self.ransackable_attributes(_auth_object = nil)
%w[state created_at]
end
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class User < ApplicationRecord
has_many :notifications, dependent: :destroy
has_many :career_members, class_name: 'Career::Member', dependent: :destroy
has_many :careers, through: :career_members
has_many :unread_notifications, -> { unread }, class_name: 'Notification', inverse_of: :user, dependent: :nullify

aasm :state, column: :state do
state :permitted, initial: true
Expand Down
13 changes: 13 additions & 0 deletions app/mutators/career/member_mutator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class Career::MemberMutator
def self.create(career, params)
career_member = career.members.build(params)
user = career_member.user
if career_member.save
user.notifications.create!(kind: :new_career_member, resource: career_member)
end

career_member
end
end
7 changes: 7 additions & 0 deletions app/policies/notification_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class NotificationPolicy < ApplicationPolicy
def update?
author?
end
end
5 changes: 4 additions & 1 deletion app/views/layouts/shared/_nav.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ nav.navbar.navbar-expand-lg.bg-body-tertiary
- if user_signed_in?
li.nav-item
= link_to account_notifications_path, class: 'nav-link px-2' do
span.bi.bi-bell
- if current_user.unread_notifications.any?
span.bi.bi-bell-fill.text-danger
-else
span.bi.bi-bell
li.nav-item.dropdown
a.nav-link.px-2.dropdown-toggle href="#" data-bs-toggle='dropdown' aria-haspopup='true' aria-expanded='false' = t('.add')
ul.dropdown-menu.dropdown-menu-end
Expand Down
26 changes: 21 additions & 5 deletions app/views/web/account/notifications/index.html.slim
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
- content_for :header do
= t('.header')

- @notifications.each do |notification|
.d-flex
i.mt-1.me-2.far class=class_for_notification(notification)
p.me-3 == message_for_notification(notification)
.text-muted.text-nowrap.ms-auto = l notification.created_at, format: :short
table.table
thead
tr
th = t('.content')
th = han('notification', 'state')
th = sort_link(@q, 'created_at')
th = t('actions')
tbody
- @notifications.each do |notification|
tr
td == message_for_notification(notification)
td = notification.aasm(:state).human_state
td = l notification.created_at, format: :short
td
- if notification.may_mark_as_read?
= link_to account_notification_path(notification), method: :patch, class: 'btn btn-outline-primary btn-sm me-1', title: t('.mark_as_read') do
span.bi.bi-bookmark-check
- else
span.h4.bi.bi-check-circle-fill.text-success[aria-label="#{t('.notification_read')}"]

= paginate @notifications

- if @notifications.empty?
= render partial: 'web/shared/empty_list'
5 changes: 5 additions & 0 deletions config/locales/en.activerecord.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
en:
activerecord:
models:
notification: Notification
resume: Resume
user: User
resume/answer: Recommendation
Expand Down Expand Up @@ -71,6 +72,10 @@ en:
state/published: Published
name: Name
company: Company
notification:
state: State Notification
state/read: Read
state/unread: Unread
user:
password: password
resume_answer_likes_count: Likes
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.flash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ en:
flash:
web:
account:
notifications:
update:
success: Notification read.
profiles:
update:
success: Data updated
Expand Down
8 changes: 8 additions & 0 deletions config/locales/en.notification.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
en:
notifications:
new_answer_html: User <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> left <a href="%{answer_path}" class="fw-bolder">new recommendation</a> to your resume
new_comment_html: User <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> left <a href="%{comment_path}" class="fw-bolder">new comment</a> to your resume
new_answer_comment_html: User <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> left <a href="%{answer_comment_path}" class="fw-bolder">new comment</a> to your recommendation
new_answer_like_html: User <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> likes your <a href="%{answer_path}" class="fw-bolder"> recommendation</a>
answer_applied_html: User <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> improved his resume using your <a href="%{answer_path}" class="fw-bolder ">recommendation</a>
new_career_member_html: Hooray! You have been assigned a career track <a href="%{career_path}" class="fw-bolder">%{career}</a>. Get started and get ready for a job.
3 changes: 3 additions & 0 deletions config/locales/en.views.yml
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ en:
my_answers: My recommendations
notifications:
index:
notification_read: Notification read
mark_as_read: Mark as Read
header: Notifications
content: Content
newsletters:
edit:
header: Email settings
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
en:
actions: Actions
application_name: Hexlet CV
in_the_city: "in the city %{city_name}"
months_nominative_case:
Expand Down
5 changes: 5 additions & 0 deletions config/locales/ru.activerecord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ru:
models:
resume: Резюме
user: Пользователь
notification: Уведомление
resume/answer: Рекомендация
resume/comment: Комментарий
answer/comment: Комментарий
Expand Down Expand Up @@ -50,6 +51,10 @@ ru:
locale: Язык
tasks_text: Задачи
review_needed: Нужно ли ревью
notification:
state: Состаяние уведомления
state/read: Прочитанно
state/unread: Не прочитанно
vacancy:
conditions_description: Условия и Бонусы
requirements_description: Требования
Expand Down
4 changes: 3 additions & 1 deletion config/locales/ru.flash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
ru:
flash:
web:
account:
account:
profiles:
update:
success: Данные обновлены
Expand All @@ -24,6 +24,8 @@ ru:
notifications:
read_all:
success: Все уведомления помечены прочитанными
update:
success: Уведомление прочитанно.
newsletters:
update:
success: Настройки рассылки сохранены
Expand Down
11 changes: 6 additions & 5 deletions config/locales/ru.notifications.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ru:
notifications:
new_answer_html: Пользователь <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> оставил <a href="%{answer_path}">новую рекомендацию</a> к вашему резюме
new_comment_html: Пользователь <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> оставил <a href="%{comment_path}">новый комментарий</a> к вашему резюме
new_answer_comment_html: Пользователь <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> оставил <a href="%{answer_comment_path}">новый комментарий</a> к вашей рекомендации
new_answer_like_html: Пользователю <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> нравится ваша <a href="%{answer_path}">рекомендация</a>
answer_applied_html: Пользователь <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> улучшил резюме используя вашу <a href="%{answer_path}">рекомендацию</a>
new_answer_html: Пользователь <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> оставил <a href="%{answer_path}" class="fw-bolder">новую рекомендацию</a> к вашему резюме
new_comment_html: Пользователь <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> оставил <a href="%{comment_path}" class="fw-bolder">новый комментарий</a> к вашему резюме
new_answer_comment_html: Пользователь <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> оставил <a href="%{answer_comment_path}" class="fw-bolder">новый комментарий</a> к вашей рекомендации
new_answer_like_html: Пользователю <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> нравится ваша <a href="%{answer_path}" class="fw-bolder">рекомендация</a>
answer_applied_html: Пользователь <a href="%{user_path}" class="text-secondary fw-bolder">%{user}</a> улучшил резюме используя вашу <a href="%{answer_path}" class="fw-bolder">рекомендацию</a>
new_career_member_html: Ура! Вам назначен карьерный трек <a href="%{career_path}" class="fw-bolder">%{career}</a>. Приступайте к прохождению и готовьтесь к трудоустройству.
3 changes: 3 additions & 0 deletions config/locales/ru.views.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ ru:
my_answers: Мои рекомендации
notifications:
index:
notification_read: Уведомление прочитанно
mark_as_read: Пометить как прочитанное
content: Содержание
header: Уведомления
newsletters:
edit:
Expand Down
1 change: 1 addition & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ru:
actions: Действия
career: Карьерный трек
steps: Шаг карьерного трека
months_nominative_case:
Expand Down
11 changes: 11 additions & 0 deletions test/controllers/web/account/notifications_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ class Web::Account::NotificationsControllerTest < ActionDispatch::IntegrationTes
get account_notifications_url
assert_response :success
end

test '#update' do
notification = notifications(:resume_comment_one_by_one)
patch account_notification_path(notification)

assert_redirected_to account_notifications_path

notification.reload

assert { notification.read? }
end
end
3 changes: 3 additions & 0 deletions test/controllers/web/admin/careers/members_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ class Web::Admin::Careers::MembersControllerTest < ActionDispatch::IntegrationTe
assert_redirected_to admin_career_path(@career)

member = Career::Member.find_by(user_id: attrs[:user_id])
notification = Notification.find_by(user: @user, resource: member, kind: :new_career_member)

assert { member }
assert { notification }
end

test '#archive' do
Expand Down
8 changes: 7 additions & 1 deletion test/fixtures/notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ full_one_comment_full:
two_full_one_answer_comment_full:
user: full
resource: two_full_one (Resume::Answer::Comment)
state: unread
state: read
kind: new_answer_comment

career_member_full_by_full:
user: full
resource: member_full (Career::Member)
state: unread
kind: new_career_member

# FIXME: add likes to resume and answers