From f4ce2f02aa8cdda4cc7817aa52cb2dfc7d550447 Mon Sep 17 00:00:00 2001 From: Alex Abramov Date: Sat, 28 Dec 2024 20:06:50 +0700 Subject: [PATCH] add removing flash-messages, update podcasts --- app/controllers/podcasts_controller.rb | 8 +++++--- ...hello_controller.js => removals_controller.js} | 8 +++++++- app/views/layouts/_flash.html.erb | 5 +++++ app/views/layouts/application.html.erb | 9 +++------ spec/features/podcasts/edit_update_spec.rb | 15 +++++++++------ spec/features/podcasts/new_create_spec.rb | 2 +- 6 files changed, 30 insertions(+), 17 deletions(-) rename app/javascript/controllers/{hello_controller.js => removals_controller.js} (52%) mode change 100755 => 100644 create mode 100644 app/views/layouts/_flash.html.erb diff --git a/app/controllers/podcasts_controller.rb b/app/controllers/podcasts_controller.rb index e16b9b3..2652571 100644 --- a/app/controllers/podcasts_controller.rb +++ b/app/controllers/podcasts_controller.rb @@ -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: [ @@ -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." } diff --git a/app/javascript/controllers/hello_controller.js b/app/javascript/controllers/removals_controller.js old mode 100755 new mode 100644 similarity index 52% rename from app/javascript/controllers/hello_controller.js rename to app/javascript/controllers/removals_controller.js index 5975c07..d8e603f --- a/app/javascript/controllers/hello_controller.js +++ b/app/javascript/controllers/removals_controller.js @@ -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() } } diff --git a/app/views/layouts/_flash.html.erb b/app/views/layouts/_flash.html.erb new file mode 100644 index 0000000..b696bbb --- /dev/null +++ b/app/views/layouts/_flash.html.erb @@ -0,0 +1,5 @@ +<% flash.each do |flash_type, message| %> +
+ <%= message %> +
+<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9bf4e03..81a4ff3 100755 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -22,12 +22,9 @@
<%= render "layouts/header" %> - <% if notice %> -

<%= notice %>

- <% end %> - <% if alert %> -

<%= alert %>

- <% end %> +
+ <%= render "layouts/flash" %> +
<%= yield %> diff --git a/spec/features/podcasts/edit_update_spec.rb b/spec/features/podcasts/edit_update_spec.rb index f800ea0..090344c 100644 --- a/spec/features/podcasts/edit_update_spec.rb +++ b/spec/features/podcasts/edit_update_spec.rb @@ -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 @@ -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 diff --git a/spec/features/podcasts/new_create_spec.rb b/spec/features/podcasts/new_create_spec.rb index 8217338..08382bd 100644 --- a/spec/features/podcasts/new_create_spec.rb +++ b/spec/features/podcasts/new_create_spec.rb @@ -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