From 4fcc2c76eb04fdbc32c5355abcbbfef5abf264a9 Mon Sep 17 00:00:00 2001 From: Vladislav Sokov Date: Thu, 18 Jul 2024 18:18:17 +0300 Subject: [PATCH] Add Flash Messages --- app/assets/javascripts/application.js | 9 +++++++++ .../{actual_db_schema => }/styles.css | 16 +++++++++++++++ .../actual_db_schema/migrations_controller.rb | 20 +++++++++++++++++-- .../phantom_migrations_controller.rb | 20 +++++++++++++++++-- app/javascripts/packs/application.js | 0 .../migrations/index.html.erb | 6 +++++- .../actual_db_schema/migrations/show.html.erb | 6 +++++- .../phantom_migrations/index.html.erb | 6 +++++- .../phantom_migrations/show.html.erb | 7 ++++++- lib/actual_db_schema/engine.rb | 2 +- .../migrations_controller_test.rb | 1 + .../phantom_migrations_controller_test.rb | 1 + test/dummy_app/config/database.yml | 5 +++++ 13 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 app/assets/javascripts/application.js rename app/assets/stylesheets/{actual_db_schema => }/styles.css (87%) create mode 100644 app/javascripts/packs/application.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js new file mode 100644 index 0000000..d1e5685 --- /dev/null +++ b/app/assets/javascripts/application.js @@ -0,0 +1,9 @@ +document.addEventListener('DOMContentLoaded', function() { + setTimeout(function() { + var flashMessages = document.querySelectorAll('.flash'); + flashMessages.forEach(function(flashMessage) { + flashMessage.style.display = 'none'; + }); + }, 5000); +}); + diff --git a/app/assets/stylesheets/actual_db_schema/styles.css b/app/assets/stylesheets/styles.css similarity index 87% rename from app/assets/stylesheets/actual_db_schema/styles.css rename to app/assets/stylesheets/styles.css index 162fce5..01338b3 100644 --- a/app/assets/stylesheets/actual_db_schema/styles.css +++ b/app/assets/stylesheets/styles.css @@ -90,3 +90,19 @@ pre { overflow: hidden; text-overflow: ellipsis; } + +.flash { + padding: 10px; + margin-bottom: 10px; + border-radius: 5px; +} + +.flash.notice { + background-color: #d4edda; + color: #155724; +} + +.flash.alert { + background-color: #f8d7da; + color: #721c24; +} diff --git a/app/controllers/actual_db_schema/migrations_controller.rb b/app/controllers/actual_db_schema/migrations_controller.rb index 979bcc9..d223419 100644 --- a/app/controllers/actual_db_schema/migrations_controller.rb +++ b/app/controllers/actual_db_schema/migrations_controller.rb @@ -10,17 +10,33 @@ def show end def rollback - ActualDbSchema::Migration.instance.rollback(params[:id], params[:database]) + handle_rollback(params[:id], params[:database]) redirect_to migrations_path end def migrate - ActualDbSchema::Migration.instance.migrate(params[:id], params[:database]) + handle_migrate(params[:id], params[:database]) redirect_to migrations_path end private + def handle_rollback(id, database) + ActualDbSchema::Migration.instance.rollback(id, database) + flash[:notice] = "Migration #{id} was successfully rolled back." + rescue ActiveRecord::IrreversibleMigration + flash[:alert] = "Migration #{id} cannot be rolled back because it is irreversible." + rescue StandardError => e + flash[:alert] = "An error occurred while trying to roll back the migration #{id}: #{e.message}" + end + + def handle_migrate(id, database) + ActualDbSchema::Migration.instance.migrate(id, database) + flash[:notice] = "Migration #{id} was successfully migrated." + rescue StandardError => e + flash[:alert] = "An error occurred while migrating #{id}: #{e.message}" + end + helper_method def migrations @migrations ||= ActualDbSchema::Migration.instance.all end diff --git a/app/controllers/actual_db_schema/phantom_migrations_controller.rb b/app/controllers/actual_db_schema/phantom_migrations_controller.rb index 8356e8a..d7024b5 100644 --- a/app/controllers/actual_db_schema/phantom_migrations_controller.rb +++ b/app/controllers/actual_db_schema/phantom_migrations_controller.rb @@ -10,17 +10,33 @@ def show end def rollback - ActualDbSchema::Migration.instance.rollback(params[:id], params[:database]) + handle_rollback(params[:id], params[:database]) redirect_to phantom_migrations_path end def rollback_all - ActualDbSchema::Migration.instance.rollback_all + handle_rollback_all redirect_to phantom_migrations_path end private + def handle_rollback(id, database) + ActualDbSchema::Migration.instance.rollback(id, database) + flash[:notice] = "Migration #{id} was successfully rolled back." + rescue ActiveRecord::IrreversibleMigration + flash[:alert] = "Migration #{id} cannot be rolled back because it is irreversible." + rescue StandardError => e + flash[:alert] = "An error occurred while trying to roll back the migration #{id}: #{e.message}" + end + + def handle_rollback_all + ActualDbSchema::Migration.instance.rollback_all + flash[:notice] = "Migrations was successfully rolled back." + rescue StandardError => e + flash[:alert] = "An error occurred while trying to roll back migrations: #{e.message}" + end + helper_method def phantom_migrations @phantom_migrations ||= ActualDbSchema::Migration.instance.all_phantom end diff --git a/app/javascripts/packs/application.js b/app/javascripts/packs/application.js new file mode 100644 index 0000000..e69de29 diff --git a/app/views/actual_db_schema/migrations/index.html.erb b/app/views/actual_db_schema/migrations/index.html.erb index 896f0a5..aa0addd 100644 --- a/app/views/actual_db_schema/migrations/index.html.erb +++ b/app/views/actual_db_schema/migrations/index.html.erb @@ -2,7 +2,8 @@ Migrations - <%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %> + <%= stylesheet_link_tag 'styles', media: 'all' %> + <%= javascript_include_tag 'application' %>
@@ -62,5 +63,8 @@

No migrations found.

<% end %>
+ <% flash.each do |key, message| %> +
<%= message %>
+ <% end %> diff --git a/app/views/actual_db_schema/migrations/show.html.erb b/app/views/actual_db_schema/migrations/show.html.erb index e8d122f..b382fb1 100644 --- a/app/views/actual_db_schema/migrations/show.html.erb +++ b/app/views/actual_db_schema/migrations/show.html.erb @@ -2,7 +2,8 @@ Migration Details - <%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %> + <%= stylesheet_link_tag 'styles', media: 'all' %> + <%= javascript_include_tag 'application' %>
@@ -50,5 +51,8 @@ style: ('display: none;' if migration[:status] == "up" || migration[:phantom]) %>
+ <% flash.each do |key, message| %> +
<%= message %>
+ <% end %> diff --git a/app/views/actual_db_schema/phantom_migrations/index.html.erb b/app/views/actual_db_schema/phantom_migrations/index.html.erb index d849ee8..09e9457 100644 --- a/app/views/actual_db_schema/phantom_migrations/index.html.erb +++ b/app/views/actual_db_schema/phantom_migrations/index.html.erb @@ -2,7 +2,8 @@ Phantom Migrations - <%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %> + <%= stylesheet_link_tag 'styles', media: 'all' %> + <%= javascript_include_tag 'application' %>
@@ -51,5 +52,8 @@

No phantom migrations found.

<% end %>
+ <% flash.each do |key, message| %> +
<%= message %>
+ <% end %> diff --git a/app/views/actual_db_schema/phantom_migrations/show.html.erb b/app/views/actual_db_schema/phantom_migrations/show.html.erb index 96681cf..2511d94 100644 --- a/app/views/actual_db_schema/phantom_migrations/show.html.erb +++ b/app/views/actual_db_schema/phantom_migrations/show.html.erb @@ -2,7 +2,9 @@ Phantom Migration Details - <%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %> + <%= stylesheet_link_tag 'styles', media: 'all' %> + <%= javascript_include_tag 'application' %> +
@@ -41,5 +43,8 @@ <%= button_to '⎌ Rollback', rollback_phantom_migration_path(id: params[:id], database: params[:database]), method: :post, class: 'button' %>
+ <% flash.each do |key, message| %> +
<%= message %>
+ <% end %> diff --git a/lib/actual_db_schema/engine.rb b/lib/actual_db_schema/engine.rb index a245b27..8e9121a 100644 --- a/lib/actual_db_schema/engine.rb +++ b/lib/actual_db_schema/engine.rb @@ -11,7 +11,7 @@ class Engine < ::Rails::Engine mount ActualDbSchema::Engine => "/rails" end - app.config.assets.precompile += %w[actual_db_schema/styles.css] + app.config.assets.precompile += %w[styles.css application.js] end end end diff --git a/test/controllers/actual_db_schema/migrations_controller_test.rb b/test/controllers/actual_db_schema/migrations_controller_test.rb index f810e4f..e2afd87 100644 --- a/test/controllers/actual_db_schema/migrations_controller_test.rb +++ b/test/controllers/actual_db_schema/migrations_controller_test.rb @@ -113,6 +113,7 @@ def active_record_setup end end end + assert_select ".flash", text: "Migration 20130906111511 was successfully rolled back." end end end diff --git a/test/controllers/actual_db_schema/phantom_migrations_controller_test.rb b/test/controllers/actual_db_schema/phantom_migrations_controller_test.rb index 2cb7738..fda24bc 100644 --- a/test/controllers/actual_db_schema/phantom_migrations_controller_test.rb +++ b/test/controllers/actual_db_schema/phantom_migrations_controller_test.rb @@ -133,6 +133,7 @@ def active_record_setup end end end + assert_select ".flash", text: "Migration 20130906111511 was successfully rolled back." end test "POST #rollback_all changes all phantom migrations status to down and hide migration with down status" do diff --git a/test/dummy_app/config/database.yml b/test/dummy_app/config/database.yml index e69de29..b7c49e3 100644 --- a/test/dummy_app/config/database.yml +++ b/test/dummy_app/config/database.yml @@ -0,0 +1,5 @@ +--- +test: + adapter: sqlite3 + database: tmp/primary.sqlite3 + migrations_paths: "/home/vlad/projects/actual_db_schema/test/dummy_app/db/migrate"