Skip to content

Commit

Permalink
feat(announce): can destroy a note and add at the same date
Browse files Browse the repository at this point in the history
  • Loading branch information
colinux committed Nov 6, 2023
1 parent 88c1554 commit 2a37cbc
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
en:
delete: Delete this note
new: New note
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fr:
delete: Supprimer cette note
new: Ajouter une note
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@


.fr-fieldset__element
= f.submit "Valider", class: "fr-btn fr-btn--lg"
%ul.fr-btns-group.fr-btns-group--inline.fr-btns-group--icon-left
%li= f.button "Valider", class: "fr-btn fr-icon-check-line"

- if release_note.persisted?
%li= link_to t(".new"), new_super_admins_release_note_path(date: release_note.released_on), class: "fr-btn fr-btn--secondary fr-icon-add-line"

- if release_note.persisted?
%li
= link_to t('.delete'), super_admins_release_note_path(release_note),
class: "fr-btn fr-btn--secondary fr-icon-delete-line",
data: { method: :delete, confirm: "Supprimer cette note ?" }
10 changes: 8 additions & 2 deletions app/controllers/super_admins/release_notes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class SuperAdmins::ReleaseNotesController < ApplicationController
before_action :authenticate_super_admin!
before_action :set_note, only: [:edit, :update]
before_action :set_note, only: [:edit, :update, :destroy]

def index
@release_notes = ReleaseNote
Expand All @@ -14,7 +14,7 @@ def show
end

def new
@release_note = ReleaseNote.new(released_on: Date.current)
@release_note = ReleaseNote.new(released_on: params[:date].presence || Date.current, published: true)
end

def create
Expand All @@ -40,6 +40,12 @@ def update
end
end

def destroy
@release_note.destroy!

redirect_to super_admins_release_notes_path, notice: t('.success')
end

private

def release_note_params
Expand Down
2 changes: 2 additions & 0 deletions config/locales/release_notes.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ en:
error: Release note was not saved.
update:
<<: *save
destroy:
success: Release note was successfully deleted.
activerecord:
attributes:
release_note:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/release_notes.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ fr:
error: L’annonce n’a pas pu être sauvegardée.
update:
<<: *save
destroy:
success: La note a été supprimée.
activerecord:
attributes:
release_note:
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
}

namespace :super_admins do
resources :release_notes, only: [:index, :new, :create, :edit, :update, :show]
resources :release_notes
end

get 'super_admins/edit_otp', to: 'super_admins#edit_otp', as: 'edit_super_admin_otp'
Expand Down
16 changes: 15 additions & 1 deletion spec/controllers/super_admins/release_notes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,24 @@
expect(response.status).to eq(302)
expect(flash[:alert]).to be_present
end

it 'is not allowed to destroy' do
delete :destroy, params: { id: release_note.id }
expect(response.status).to eq(302)
expect(flash[:alert]).to be_present
expect(release_note.reload).to be_persisted
end
end

context 'when user is signed as super admin' do
let(:release_note) { create(:release_note, published: false) }

it 'is allowed to post' do
expect { post :create, params: { release_note: { categories: ['api'], released_on: Date.current, published: "0", body: "bam" } } }.to change(ReleaseNote, :count).by(1)
expect(flash[:notice]).to be_present
end

it 'is allowed to put' do
release_note = create(:release_note, published: false)
put :update, params: {
id: release_note.id,
release_note: {
Expand All @@ -63,6 +71,12 @@
expect(release_note.body.to_plain_text).to eq("new body")
expect(release_note.published).to be_truthy
end

it 'is allowed to destroy' do
delete :destroy, params: { id: release_note.id }
expect(flash[:notice]).to be_present
expect { release_note.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end

0 comments on commit 2a37cbc

Please sign in to comment.