Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/languages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def create
@language = Language.new(language_params)

if @language.save
redirect_to languages_path
redirect_to languages_path, notice: "Language was successfully created."
else
render :new
end
Expand All @@ -25,7 +25,7 @@ def edit

def update
@language.update(language_params)
redirect_to languages_path
redirect_to languages_path, notice: "Language was successfully updated."
end

private
Expand Down
16 changes: 10 additions & 6 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create

case mutator.create
in [ :ok, _topic ]
redirect_to topics_path
redirect_to topics_path, notice: "Topic was successfully created."
in [ :error, errors ]
@errors = errors
render :new, status: :unprocessable_entity
Expand All @@ -36,7 +36,7 @@ def edit
def update
case mutator.update
in [ :ok, _topic ]
redirect_to topics_path
redirect_to topics_path, notice: "Topic was successfully updated."
in [ :error, errors ]
@errors = errors
render :edit, status: :unprocessable_entity
Expand All @@ -46,18 +46,22 @@ def update
def destroy
redirect_to topics_path and return unless Current.user.is_admin?

Topics::Mutator.new(topic: @topic).destroy
redirect_to topics_path
if params[:confirmed]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we use native confirmation dialog boxes in browser?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I did it this way because that's how we do it for Tags (you can see a video here in the PR that introduced it). Should I leave it like this using Turbo stream or change to a browser dialog box for both Tags and Topics?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we check here if it possible to do just simply with dialog box?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's circle back to this once we're past the launch. We can refactor if necessary.

Topics::Mutator.new(topic: @topic).destroy
redirect_to topics_path, notice: "Topic was successfully destroyed."
else
respond_to { |format| format.turbo_stream }
end
end

def archive
Topics::Mutator.new(topic: @topic).archive
redirect_to topics_path
redirect_to topics_path, notice: "Topic was successfully archived."
end

def unarchive
Topics::Mutator.new(topic: @topic).unarchive
redirect_to topics_path
redirect_to topics_path, notice: "Topic was successfully reinstated."
end

private
Expand Down
14 changes: 14 additions & 0 deletions app/views/topics/destroy.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%= turbo_stream.update "delete_topic_#{@topic.id}" do %>
<div class="confirmation-dialog mt-4">
<h5>Destroying a topic is permanent. Are you sure you want to delete this topic?</h5>

<div class="mt-4 hstack gap-3">
<div>
<%= button_to "Confirm Delete", topic_path(@topic, confirmed: true), method: :delete, class: "btn btn-danger" %>
</div>
<div>
<%= link_to "Cancel", topic_path(@topic), class: "btn btn-secondary" %>
</div>
</div>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/topics/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<% end %>

<% if Current.user.is_admin? %>
<div>
<div id="delete_topic_<%= @topic.id %>">
<%= button_to "Delete this topic", @topic, method: :delete, class: "btn btn-danger mt-4" %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions spec/requests/languages/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@
expect(Language.last.name).to eq("french")
expect(Language.last.file_storage_prefix).to eq("FR_")
end

it "displays a success message" do
post languages_url, params: { language: language_params }

expect(flash[:notice]).to eq("Language was successfully created.")
end
end
end
11 changes: 8 additions & 3 deletions spec/requests/languages/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
describe "Languages", type: :request do
describe "PUT /languages" do
let(:user) { create(:user, :admin) }
let(:language) { create(:language) }
let(:language_params) { { name: "french" } }

before { sign_in(user) }

it "updates a Language" do
language = create(:language)
language_params = { name: "french" }

put language_url(language), params: { language: language_params }

language.reload
expect(response).to redirect_to(languages_path)
expect(language.name).to eq("french")
expect(language.file_storage_prefix).to eq("FR_")
end

it "displays a success message" do
put language_url(language), params: { language: language_params }

expect(flash[:notice]).to eq("Language was successfully updated.")
end
end
end
6 changes: 6 additions & 0 deletions spec/requests/topics/archive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
expect(topic.reload.state).to eq("archived")
end

it "displays a success message" do
put archive_topic_url(topic)

expect(flash[:notice]).to eq("Topic was successfully archived.")
end

context "when topic has documents" do
let(:topic) { create(:topic, :with_documents) }

Expand Down
8 changes: 7 additions & 1 deletion spec/requests/topics/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
expect(topic.state).to eq("active")
end

context "when user is ad admin" do
it "displays a success message" do
post topics_url, params: { topic: topic_params }

expect(flash[:notice]).to eq("Topic was successfully created.")
end

context "when user is an admin" do
let(:new_provider) { create(:provider) }

before { user.update(is_admin: true) }
Expand Down
55 changes: 37 additions & 18 deletions spec/requests/topics/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,59 @@
describe "Topics", type: :request do
describe "DELETE /topics/:id" do
let(:user) { create(:user, :admin) }
let(:topic) { create(:topic) }
let!(:topic) { create(:topic) }
let(:turbo_stream_headers) { { Accept: "text/vnd.turbo-stream.html" } }

before { sign_in(user) }

it "deletes a Topic" do
delete topic_url(topic)
context "when first clickin on the Delete button" do
it "renders turbo stream to confirm deletion" do
expect { delete topic_url(topic), headers: turbo_stream_headers }
.not_to change(Topic, :count)

expect(response).to redirect_to(topics_url)
expect(Topic.count).to be_zero
expect(response).to have_http_status(:ok)
expect(response.media_type).to eq "text/vnd.turbo-stream.html"
end
end

context "when user is not an admin" do
let(:user) { create(:user) }

it "does not delete a Topic" do
delete topic_url(topic)
context "when deletion has been confirmed by the admin" do
it "deletes a Topic" do
delete topic_url(topic, confirmed: true)

expect(response).to redirect_to(topics_url)
expect(Topic.count).to eq(1)
expect(Topic.count).to be_zero
end
end

context "when topic has documents" do
let(:topic) { create(:topic, :with_documents) }
it "displays a success message" do
delete topic_url(topic, confirmed: true)

before do
allow(DocumentsSyncJob).to receive(:perform_later)
expect(flash[:notice]).to eq("Topic was successfully destroyed.")
end

it "runs sync job for documents" do
context "when topic has documents" do
let(:topic) { create(:topic, :with_documents) }

before do
allow(DocumentsSyncJob).to receive(:perform_later)
end

it "runs sync job for documents" do
delete topic_url(topic, confirmed: true)

expect(response).to redirect_to(topics_url)
expect(DocumentsSyncJob).to have_received(:perform_later).with(hash_including(action: "delete"))
end
end
end

context "when user is not an admin" do
let(:user) { create(:user) }

it "does not delete a Topic" do
delete topic_url(topic)

expect(response).to redirect_to(topics_url)
expect(DocumentsSyncJob).to have_received(:perform_later).with(hash_including(action: "delete"))
expect(Topic.count).to eq(1)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/requests/topics/unarchive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
expect(topic.reload.state).to eq("active")
end

it "displays a success message" do
put unarchive_topic_url(topic)

expect(flash[:notice]).to eq("Topic was successfully reinstated.")
end

context "when topic has documents" do
let(:topic) { create(:topic, :with_documents, state: "archived") }

Expand Down
9 changes: 7 additions & 2 deletions spec/requests/topics/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@
describe "PUT /topics/:id" do
let(:user) { create(:user) }
let(:topic) { create(:topic) }
let(:topic_params) { { title: "new topic", description: "updated" } }

it "updates a Topic" do
topic_params = { title: "new topic", description: "updated" }

put topic_url(topic), params: { topic: topic_params }

topic.reload
expect(topic.title).to eq("new topic")
expect(topic.description).to eq("updated")
end

it "displays a success message" do
put topics_url, params: { topic: topic_params }

expect(flash[:notice]).to eq("Topic was successfully updated.")
end

context "when removing a tag" do
let!(:tag) { create(:tag, name: "Tag to remove") }

Expand Down