Skip to content

Commit

Permalink
add removing flash-messages, update podcasts
Browse files Browse the repository at this point in the history
  • Loading branch information
o-200 committed Dec 28, 2024
1 parent b82cd2c commit f4ce2f0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
8 changes: 5 additions & 3 deletions app/controllers/podcasts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ def create

respond_to do |format|
if @podcast.save
flash[:notice] = "Your podcast was successfully created."
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("podcast_frame", partial: "podcasts/podcast", locals: { podcast: @podcast }),
turbo_stream.update("notice", "Your podcast was successfully created.")
turbo_stream.append("flash", partial: "layouts/flash", locals: { message: notice })
]
end
format.html { redirect_to @podcast, notice: "Your podcast was successfully created." }
format.html { redirect_to @podcast, notice: notice }
else
format.turbo_stream do
render turbo_stream: [
Expand All @@ -48,10 +49,11 @@ def update

respond_to do |format|
if @podcast.update(podcast_params)
flash[:notice] = "Your podcast was successfully updated."
format.turbo_stream do
render turbo_stream: [
turbo_stream.replace("podcast_frame", partial: "podcasts/podcast", locals: { podcast: @podcast }),
turbo_stream.update("notice", "Your podcast was successfully updated.")
turbo_stream.append("flash", partial: "layouts/flash", locals: { message: notice })
]
end
format.html { redirect_to @podcast, notice: "Your podcast was successfully updated." }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
connect() {
this.element.textContent = "Hello World!"
setTimeout(() => {
this.remove()
}, 3000)
}

remove() {
this.element.remove()
}
}
5 changes: 5 additions & 0 deletions app/views/layouts/_flash.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% flash.each do |flash_type, message| %>
<div class="flash__message alert alert-info" data-controller="removals" data-action="animationend->removals#remove">
<%= message %>
</div>
<% end %>
9 changes: 3 additions & 6 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@
<div class="container">
<%= render "layouts/header" %>

<% if notice %>
<p class="alert alert-info"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert alert-warning"><%= alert %></p>
<% end %>
<div id="flash" class="flash">
<%= render "layouts/flash" %>
</div>

<div class="mb-3">
<%= yield %>
Expand Down
15 changes: 9 additions & 6 deletions spec/features/podcasts/edit_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
scenario "the current podcast details in the form" do
expect(page).to have_content("Редактировать Podcast: #{podcast.title}")

expect(find_field('podcast[title]').value).to eq(podcast.title)
expect(find_field('podcast[title]').value).to eq(podcast.title)
expect(find_field('podcast[description]').value).to eq(podcast.description)
end

Expand All @@ -30,13 +30,16 @@

# context 'user updates the podcast' do
scenario "successfully updates the podcast title and description" do
fill_in 'podcast[title]', with: "Updated Podcast Title"
fill_in 'podcast[description]', with: "Updated description."
title = "Updated Podcast Title"
description = "Updated description."

fill_in 'podcast[title]', with: title
fill_in 'podcast[description]', with: description
click_button 'Submit Podcast'

# expect(page).to have_content("Podcast was successfully updated.")
expect(page).to have_content("Updated Podcast Title")
expect(page).to have_content("Updated description.")
expect(page).to have_content("Your podcast was successfully updated.")
expect(page).to have_content(title)
expect(page).to have_content(description)
end

scenario "see error" do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/podcasts/new_create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
click_button 'Submit Podcast'

expect(current_path).to eq(podcast_path(Podcast.last))
# expect(page).to have_content("Your podcast was successfully created.")
expect(page).to have_content("Your podcast was successfully created.")
expect(page).to have_content(podcast.title)
expect(page).to have_content(podcast.description)
end
Expand Down

0 comments on commit f4ce2f0

Please sign in to comment.