From 052461c73fcb064b8c7c6fb7d5bfeb8f566e943f Mon Sep 17 00:00:00 2001 From: Bogdanov Anton Date: Mon, 21 Oct 2024 13:44:45 +0300 Subject: [PATCH] rename model Feedback to User::Feedback --- ...ommand.rb => add_user_feedback_command.rb} | 4 ++-- app/controllers/admin/feedbacks_controller.rb | 2 +- .../frontend/feedbacks_controller.rb | 21 ------------------ .../frontend/users/feedbacks_controller.rb | 22 +++++++++++++++++++ .../frontend/users/vacations_controller.rb | 1 - .../requests/createFeedbackRequest.jsx | 2 +- app/models/feedback.rb | 7 ------ app/models/user.rb | 2 +- app/models/user/feedback.rb | 11 ++++++++++ app/notifiers/admin_telegram_notifier.rb | 2 +- config/initializers/container.rb | 2 +- config/routes.rb | 2 +- ...c.rb => add_user_feedback_command_spec.rb} | 8 +++---- .../{ => users}/feedbacks_controller_spec.rb | 6 ++--- spec/factories/{ => user}/feedbacks.rb | 2 +- spec/models/{ => user}/feedback_spec.rb | 2 +- 16 files changed, 50 insertions(+), 46 deletions(-) rename app/commands/{add_feedback_command.rb => add_user_feedback_command.rb} (83%) delete mode 100644 app/controllers/frontend/feedbacks_controller.rb create mode 100644 app/controllers/frontend/users/feedbacks_controller.rb delete mode 100644 app/models/feedback.rb create mode 100644 app/models/user/feedback.rb rename spec/commands/{add_feedback_command_spec.rb => add_user_feedback_command_spec.rb} (82%) rename spec/controllers/frontend/{ => users}/feedbacks_controller_spec.rb (89%) rename spec/factories/{ => user}/feedbacks.rb (71%) rename spec/models/{ => user}/feedback_spec.rb (89%) diff --git a/app/commands/add_feedback_command.rb b/app/commands/add_user_feedback_command.rb similarity index 83% rename from app/commands/add_feedback_command.rb rename to app/commands/add_user_feedback_command.rb index 2e6fb313..55be9ebe 100644 --- a/app/commands/add_feedback_command.rb +++ b/app/commands/add_user_feedback_command.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class AddFeedbackCommand < BaseCommand +class AddUserFeedbackCommand < BaseCommand use_contract do params do required(:user).filled(type?: User) @@ -17,7 +17,7 @@ def do_prepare(input) end def do_persist(input) - feedback = Feedback.create!(input) + feedback = User::Feedback.create!(input) AdminDelivery.with(id: feedback.id).feedback_created.deliver_later diff --git a/app/controllers/admin/feedbacks_controller.rb b/app/controllers/admin/feedbacks_controller.rb index f275245f..658a80aa 100644 --- a/app/controllers/admin/feedbacks_controller.rb +++ b/app/controllers/admin/feedbacks_controller.rb @@ -13,7 +13,7 @@ def index; end private def find_feedbacks - @pagy, @feedbacks = pagy(Feedback.order(id: :desc), limit: PER_PAGE) + @pagy, @feedbacks = pagy(User::Feedback.order(id: :desc), limit: PER_PAGE) end end end diff --git a/app/controllers/frontend/feedbacks_controller.rb b/app/controllers/frontend/feedbacks_controller.rb deleted file mode 100644 index 579b1ba7..00000000 --- a/app/controllers/frontend/feedbacks_controller.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -module Frontend - class FeedbacksController < Frontend::BaseController - include Deps[add_feedback: 'commands.add_feedback'] - - def create - # commento: feedbacks.title, feedbacks.description, feedbacks.email, feedbacks.answerable - case add_feedback.call(feedback_params.merge(user: current_user)) - in { errors: errors } then render json: { errors: errors }, status: :ok - else render json: { status: 'ok' }, status: :ok - end - end - - private - - def feedback_params - params.require(:feedback).permit(:title, :description, :email, :answerable).to_h - end - end -end diff --git a/app/controllers/frontend/users/feedbacks_controller.rb b/app/controllers/frontend/users/feedbacks_controller.rb new file mode 100644 index 00000000..b0521071 --- /dev/null +++ b/app/controllers/frontend/users/feedbacks_controller.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Frontend + module Users + class FeedbacksController < Frontend::BaseController + include Deps[add_user_feedback: 'commands.add_user_feedback'] + + def create + case add_user_feedback.call(user_feedback_params.merge(user: current_user)) + in { errors: errors } then render json: { errors: errors }, status: :ok + else render json: { status: 'ok' }, status: :created + end + end + + private + + def user_feedback_params + params.require(:feedback).permit(:title, :description, :email, :answerable).to_h + end + end + end +end diff --git a/app/controllers/frontend/users/vacations_controller.rb b/app/controllers/frontend/users/vacations_controller.rb index 3fe815f0..a5c17de3 100644 --- a/app/controllers/frontend/users/vacations_controller.rb +++ b/app/controllers/frontend/users/vacations_controller.rb @@ -8,7 +8,6 @@ class VacationsController < Frontend::BaseController before_action :find_user_vacation, only: %i[destroy] def create - # commento: vacations.start_time, vacations.end_time case add_user_vacation.call(user_vacation_params.merge(user: current_user)) in { errors: errors } then render json: { errors: errors }, status: :ok in { result: result } diff --git a/app/javascript/components/compounds/Feedback/requests/createFeedbackRequest.jsx b/app/javascript/components/compounds/Feedback/requests/createFeedbackRequest.jsx index 03e197d2..4ab5829e 100644 --- a/app/javascript/components/compounds/Feedback/requests/createFeedbackRequest.jsx +++ b/app/javascript/components/compounds/Feedback/requests/createFeedbackRequest.jsx @@ -2,7 +2,7 @@ import { apiRequest, csrfToken } from '../../../../helpers'; export const createFeedbackRequest = async (payload) => { return await apiRequest({ - url: '/frontend/feedback.json', + url: '/frontend/users/feedback.json', options: { method: 'POST', headers: { diff --git a/app/models/feedback.rb b/app/models/feedback.rb deleted file mode 100644 index f9217fc9..00000000 --- a/app/models/feedback.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -class Feedback < ApplicationRecord - encrypts :email - - belongs_to :user -end diff --git a/app/models/user.rb b/app/models/user.rb index 5e730d82..ec95e532 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -26,7 +26,7 @@ class User < ApplicationRecord has_many :subscriptions, class_name: 'User::Subscription', foreign_key: :user_id, dependent: :destroy has_many :vacations, class_name: 'User::Vacation', foreign_key: :user_id, dependent: :destroy - has_many :feedbacks, dependent: :destroy + has_many :feedbacks, class_name: 'User::Feedback', dependent: :destroy has_many :api_access_tokens, dependent: :destroy diff --git a/app/models/user/feedback.rb b/app/models/user/feedback.rb new file mode 100644 index 00000000..bdff5ddc --- /dev/null +++ b/app/models/user/feedback.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class User + class Feedback < ApplicationRecord + self.table_name = :feedbacks + + encrypts :email + + belongs_to :user + end +end diff --git a/app/notifiers/admin_telegram_notifier.rb b/app/notifiers/admin_telegram_notifier.rb index cab60817..20b38a1b 100644 --- a/app/notifiers/admin_telegram_notifier.rb +++ b/app/notifiers/admin_telegram_notifier.rb @@ -26,7 +26,7 @@ def feedback_created private def feedback_created_payload(id) - feedback = Feedback.find_by(id: id) + feedback = User::Feedback.find_by(id: id) return '' unless feedback "User - #{feedback.user_id}\nFeedback created - #{feedback.title}\n#{feedback.description}" diff --git a/config/initializers/container.rb b/config/initializers/container.rb index 5106d917..93dbd3d9 100644 --- a/config/initializers/container.rb +++ b/config/initializers/container.rb @@ -39,7 +39,7 @@ def register(key) register('commands.associate_entities_with_identity') { AssociateEntitiesWithIdentityCommand.new } register('commands.add_repository') { AddRepositoryCommand.new } register('commands.add_webhook') { AddWebhookCommand.new } - register('commands.add_feedback') { AddFeedbackCommand.new } + register('commands.add_user_feedback') { AddUserFeedbackCommand.new } register('commands.add_notification') { AddNotificationCommand.new } register('commands.add_invite') { AddInviteCommand.new } register('commands.add_subscriber') { AddSubscriberCommand.new } diff --git a/config/routes.rb b/config/routes.rb index 450a147f..a93bd930 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,7 +52,6 @@ resources :notifications, only: %i[create destroy] resources :ignores, only: %i[create] resources :invites, only: %i[create] - resource :feedback, only: %i[create] resources :ignores, only: %i[destroy] resources :webhooks, only: %i[create destroy] namespace :excludes do @@ -65,6 +64,7 @@ resources :api_access_tokens, only: %i[create destroy] namespace :users do resources :vacations, only: %i[create destroy] + resource :feedback, only: %i[create] end end diff --git a/spec/commands/add_feedback_command_spec.rb b/spec/commands/add_user_feedback_command_spec.rb similarity index 82% rename from spec/commands/add_feedback_command_spec.rb rename to spec/commands/add_user_feedback_command_spec.rb index cf57ac8b..bce4120b 100644 --- a/spec/commands/add_feedback_command_spec.rb +++ b/spec/commands/add_user_feedback_command_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe AddFeedbackCommand do +describe AddUserFeedbackCommand do subject(:command) { instance.call(params.merge(user: user)) } let!(:instance) { described_class.new } @@ -10,7 +10,7 @@ let(:params) { { description: '', email: 'email@gmail.com' } } it 'does not create feedback', :aggregate_failures do - expect { command }.not_to change(Feedback, :count) + expect { command }.not_to change(User::Feedback, :count) expect(command[:errors]).not_to be_blank end end @@ -20,7 +20,7 @@ it 'creates feedback', :aggregate_failures do expect { command }.to change(user.feedbacks, :count).by(1) - expect(Feedback.last.email).to be_nil + expect(User::Feedback.last.email).to be_nil expect(command[:errors]).to be_nil end @@ -29,7 +29,7 @@ it 'creates feedback', :aggregate_failures do expect { command }.to change(user.feedbacks, :count).by(1) - expect(Feedback.last.email).to eq 'email@gmail.com' + expect(User::Feedback.last.email).to eq 'email@gmail.com' expect(command[:errors]).to be_nil end end diff --git a/spec/controllers/frontend/feedbacks_controller_spec.rb b/spec/controllers/frontend/users/feedbacks_controller_spec.rb similarity index 89% rename from spec/controllers/frontend/feedbacks_controller_spec.rb rename to spec/controllers/frontend/users/feedbacks_controller_spec.rb index 4ae8e02e..662660b2 100644 --- a/spec/controllers/frontend/feedbacks_controller_spec.rb +++ b/spec/controllers/frontend/users/feedbacks_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe Frontend::FeedbacksController do +describe Frontend::Users::FeedbacksController do describe 'POST#create' do let(:delivery_service) { double } @@ -19,7 +19,7 @@ let(:request) { post :create, params: { feedback: { title: '', description: '' }, pullmetry_access_token: access_token } } it 'does not create feedback', :aggregate_failures do - expect { request }.not_to change(Feedback, :count) + expect { request }.not_to change(User::Feedback, :count) expect(response).to have_http_status :ok expect(response.parsed_body.dig('errors', 0)).to eq 'Description must be filled' end @@ -32,7 +32,7 @@ it 'creates feedback', :aggregate_failures do expect { request }.to change(user.feedbacks, :count).by(1) - expect(response).to have_http_status :ok + expect(response).to have_http_status :created expect(response.parsed_body['errors']).to be_nil end end diff --git a/spec/factories/feedbacks.rb b/spec/factories/user/feedbacks.rb similarity index 71% rename from spec/factories/feedbacks.rb rename to spec/factories/user/feedbacks.rb index 7fa2a6c3..df572d05 100644 --- a/spec/factories/feedbacks.rb +++ b/spec/factories/user/feedbacks.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true FactoryBot.define do - factory :feedback do + factory :feedback, class: 'User::Feedback' do title { 'Title' } description { 'Text' } user diff --git a/spec/models/feedback_spec.rb b/spec/models/user/feedback_spec.rb similarity index 89% rename from spec/models/feedback_spec.rb rename to spec/models/user/feedback_spec.rb index 59417981..9848446e 100644 --- a/spec/models/feedback_spec.rb +++ b/spec/models/user/feedback_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -describe Feedback do +describe User::Feedback do it 'factory should be valid' do feedback = build :feedback