Skip to content

Commit

Permalink
fix after reviw
Browse files Browse the repository at this point in the history
  • Loading branch information
usernaimandrey committed Jul 28, 2024
1 parent b2a7c2f commit 61ed5aa
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 91 deletions.
33 changes: 19 additions & 14 deletions app/controllers/web/admin/vacancies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ def index
end

def new
@vacancy = vacancy_form.new
@vacancy = Web::Admin::VacancyForm.new
end

def edit
@vacancy = resource_vacancy.becomes(vacancy_form)
@vacancy = resource_vacancy.becomes(Web::Admin::VacancyForm)
end

def new_cancelation
@go_to = params[:go_to]
@vacancy = resource_vacancy.becomes(vacancy_form)
@vacancy = resource_vacancy.becomes(Web::Admin::VacancyForm)
end

def create
@vacancy = vacancy_form.new(params[:vacancy])
@vacancy = Web::Admin::VacancyForm.new(params[:vacancy])
@vacancy.creator = current_user

if @vacancy.save
Expand All @@ -58,8 +58,8 @@ def create
end

def update
@vacancy = resource_vacancy.becomes(vacancy_form)
if vacancy_service.call(@vacancy, params[:vacancy]).success?
@vacancy = resource_vacancy.becomes(Web::Admin::VacancyForm)
if Admin::VacancyMutator.update!(@vacancy, params.permit![:vacancy])
f(:success)
redirect_to params[:go_to] || edit_admin_vacancy_path(@vacancy)
else
Expand All @@ -86,6 +86,19 @@ def restore
redirect_to params[:go_to] || admin_vacancies_path(page: params[:page])
end

def cancel
vacancy = resource_vacancy.becomes(Web::Admin::VacancyForm)
@vacancy = Admin::VacancyMutator.cancel!(vacancy, params.permit![:vacancy])

if @vacancy.canceled?
f(:success)
redirect_to params[:go_to] || new_cancelation_admin_vacancy_path(@vacancy)
else
f(:error)
render :new_cancelation, status: :unprocessable_entity
end
end

private

def query_params(default_params = {})
Expand All @@ -95,12 +108,4 @@ def query_params(default_params = {})
def resource_vacancy
@resource_vacancy ||= Vacancy.find params[:id]
end

def vacancy_form
Web::Admin::VacancyForm
end

def vacancy_service
Admin::VacancyService
end
end
2 changes: 1 addition & 1 deletion app/lib/notifications_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def vacancy_publish_params(resource)
}
end

def vacancy_cancele_params(resource)
def vacancy_cancel_params(resource)
{
vacancy_path: vacancy_path(resource, locale: I18n.locale),
cancelation_reason: resource.cancelation_reason_text
Expand Down
2 changes: 1 addition & 1 deletion app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Notification < ApplicationRecord
career_member_finish
next_step_open_source
vacancy_publish
vacancy_cancele
vacancy_cancel
].freeze

enumerize :kind, in: NOTIFICATION_KIND
Expand Down
8 changes: 2 additions & 6 deletions app/models/vacancy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Vacancy < ApplicationRecord
unless: -> { salary_amount_type == :depends || salary_from&.positive? }
validates :salary_currency, presence: true
# validates :programming_language, presence: true
validate :cancelation_reason_presence
validates :cancelation_reason, presence: true, if: -> { canceled? || state_event == 'cancel' }

belongs_to :creator, class_name: 'User'
belongs_to :country, optional: true
Expand Down Expand Up @@ -116,7 +116,7 @@ class Vacancy < ApplicationRecord
transitions from: %i[archived], to: :on_moderate
end

event :cancele do
event :cancel do
transitions from: %i[on_moderate], to: :canceled
end
end
Expand Down Expand Up @@ -150,8 +150,4 @@ def self.ransackable_associations(_auth_object = nil)
def need_escape_html?
!habr?
end

def cancelation_reason_presence
errors.add(:cancelation_reason, I18n.t('cancelation_reason_present')) if state_event == 'cancele' && cancelation_reason.nil?
end
end
40 changes: 40 additions & 0 deletions app/mutators/admin/vacancy_mutator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

class Admin::VacancyMutator
NOTIFIED_EVENTS = %w[publish].freeze

class << self
def cancel!(vacancy, params = {})
user = vacancy.creator
cancelation_reason = params[:cancelation_reason]
vacancy.assign_attributes(cancelation_reason:)

ActiveRecord::Base.transaction do
vacancy.cancel!
user.notifications.create!(kind: :vacancy_cancel, resource: vacancy)
end

vacancy
end

def update!(vacancy, params = {})
event = params[:state_event]

if event.nil? || NOTIFIED_EVENTS.exclude?(event)
vacancy.update!(params)
return true
end

user = vacancy.creator

ActiveRecord::Base.transaction do
vacancy.update!(params)
user.notifications.create!(kind: "vacancy_#{event}", resource: vacancy)
end

true
rescue ActiveRecord::RecordInvalid
false
end
end
end
41 changes: 0 additions & 41 deletions app/services/admin/vacancy_service.rb

This file was deleted.

9 changes: 0 additions & 9 deletions app/views/web/admin/vacancies/_cancelation_form.html.slim

This file was deleted.

4 changes: 2 additions & 2 deletions app/views/web/admin/vacancies/_vacancies_table.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ table.table
span.bi.bi-eye-fill
= link_to edit_admin_vacancy_path(vacancy), class: 'btn btn-outline-primary btn-sm', title: t('.edit') do
span.bi.bi-gear-fill
- if vacancy.may_cancele?
= link_to new_cancelation_admin_vacancy_path(vacancy), class: 'btn btn-outline-warning btn-sm', title: t('.canceled') do
- if vacancy.may_cancel?
= link_to new_cancelation_admin_vacancy_path(vacancy, go_to: @go_to), class: 'btn btn-outline-warning btn-sm', title: t('.cancel') do
span.bi.bi-x-circle
- if vacancy.may_restore?
= link_to restore_admin_vacancy_path(vacancy, go_to: @go_to), method: :patch, class: 'btn btn-outline-success btn-sm', data: { confirm: t('.confirm_restore') }, title: t('.restore') do
Expand Down
8 changes: 7 additions & 1 deletion app/views/web/admin/vacancies/new_cancelation.html.slim
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
= render 'cancelation_form', vacancy: @vacancy, url: admin_vacancy_path(@vacancy, go_to: new_cancelation_admin_vacancy_path(@vacancy))
= simple_form_for @vacancy, as: :vacancy, url: cancel_admin_vacancy_path(go_to: @go_to), wrapper: 'horizontal_form' do |f|
.mb-3
.mb-3
= f.input :cancelation_reason, include_blank: false
.row.mt-5
.col-sm.d-flex.mb-3
.me-3 = f.button :submit, class: 'btn-primary'
3 changes: 3 additions & 0 deletions config/locales/admin/en.flash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ en:
success: Resume updated successfully
error: Failed to save changes. Please check form fields and try again.
vacancies:
cancel:
success: Vacancy canceled successfull
error: Failed to save changes. Please check form fields and try again.
archive:
success: Vacancy archived successfully
restore:
Expand Down
2 changes: 1 addition & 1 deletion config/locales/admin/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ en:
confirm_restore: Are you sure you want to post your selected vacancy?
vacancies_table:
edit: Editing Job
canceled: Canceled
cancel: Cancel
home:
index:
admins: Administrators
Expand Down
3 changes: 3 additions & 0 deletions config/locales/admin/ru.flash.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ ru:
success: Вакансия успешно обновлена
create:
success: Вакансия созданна
cancel:
success: Вакансия отклонена
error: Исправьте ошибки в форме
users:
update:
success: Данные пользователя успешно обновлены
Expand Down
2 changes: 1 addition & 1 deletion config/locales/admin/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ ru:
restore: Вернуть на модерацию
confirm_archive: Вы уверенны что хотите отправить вакансию в архив
confirm_restore: Вы уверенны что хотите вернуть вакансию на модерацию
canceled: Отклонить
cancel: Отклонить
home:
menu:
list: Список
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.activerecord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ en:
archive: To archive
restore: Restore from archive
send_to_moderate: Send to moderate
canceled: Canceled
cancele: Canceled
attributes:
resume/answer:
content: Answer
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.notification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ en:
The first half of the <a href="%{career_path}" class="fw-bolder">Career Track</a> is over! A very important step lies ahead - Participation in Open Source projects<br>
<a href="https://ru.hexlet.io/blog/posts/kak-vybrat-svoy-pervyy-open-sors-proekt-instruktsiya-ot-heksleta class="fw-bolder">How to choose your first open source project</a>
vacancy_publish_html: Your <a href="%{vacancy_path}" class="fw-bolder">vacancy</a> has been published
vacancy_cancele_html: Your <a href="%{vacancy_path}" class="fw-bolder">vacancy</a> was rejected due to %{cancelation_reason}
vacancy_cancel_html: Your <a href="%{vacancy_path}" class="fw-bolder">vacancy</a> was rejected due to %{cancelation_reason}

2 changes: 1 addition & 1 deletion config/locales/ru.activerecord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ru:
archive: Архивировать
restore: Восстановить из архива
send_to_moderate: Отправить на модерацию
cancele: Отклонить
cancel: Отклонить

models:
career: Карьерный трек
Expand Down
2 changes: 1 addition & 1 deletion config/locales/ru.notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ ru:
Первая половина <a href="%{career_path}" class="fw-bolder">Карьерного трека</a> позади! Впереди очень важный шаг - Участие в Open Source проектах<br>
<a href="https://ru.hexlet.io/blog/posts/kak-vybrat-svoy-pervyy-open-sors-proekt-instruktsiya-ot-heksleta class="fw-bolder">Как выбрать свой первый опенсорс проект</a>
vacancy_publish_html: Ваша <a href="%{vacancy_path}" class="fw-bolder">вакансия</a> опубликована
vacancy_cancele_html: Ваша <a href="%{vacancy_path}" class="fw-bolder">вакансия</a> отклонена по причине - %{cancelation_reason}
vacancy_cancel_html: Ваша <a href="%{vacancy_path}" class="fw-bolder">вакансия</a> отклонена по причине - %{cancelation_reason}
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
get :new_cancelation
patch :archive
patch :restore
patch :cancel
end
end
resources :careers, only: [] do
Expand Down
14 changes: 6 additions & 8 deletions test/controllers/web/admin/vacancies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,24 @@ class Web::Admin::VacanciesControllerTest < ActionDispatch::IntegrationTest

test '#cancele' do
vacancy = vacancies(:on_moderate)
go_to = new_cancelation_admin_vacancy_path(vacancy)
state_event = :cancele
attrs = vacancy.attributes.merge(state_event:, cancelation_reason: :high_requirements)
go_to = on_moderate_admin_vacancies_path
attrs = vacancy.attributes.merge(cancelation_reason: :high_requirements)

patch admin_vacancy_path(vacancy), params: { vacancy: attrs, go_to: }
patch cancel_admin_vacancy_path(vacancy), params: { vacancy: attrs, go_to: }

assert_redirected_to go_to
vacancy.reload
notification = Notification.find_by(resource: vacancy, kind: "vacancy_#{state_event}")
notification = Notification.find_by(resource: vacancy, kind: :vacancy_cancel)

assert { notification }
assert { vacancy.canceled? }
end

test '#cancele with invalid params' do
vacancy = vacancies(:on_moderate)
attrs = vacancy.attributes

attrs = vacancy.attributes.merge(state_event: :cancele)

patch admin_vacancy_path(vacancy), params: { vacancy: attrs }
patch cancel_admin_vacancy_path(vacancy), params: { vacancy: attrs }

assert_response :unprocessable_entity
vacancy.reload
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ vacancy_cancel_one:
user: one
resource: canceled (Vacancy)
state: unread
kind: vacancy_cancele
kind: vacancy_cancel

# FIXME: add likes to resume and answers
2 changes: 1 addition & 1 deletion test/fixtures/vacancies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ canceled:
salary_from: 1000
salary_to: 2000
salary_currency: rub
cancelation_reason: stack_irrelevant
cancelation_reason: stack_irrelevant

over_month_old:
<<: *DEFAULTS
Expand Down

0 comments on commit 61ed5aa

Please sign in to comment.