Skip to content

Commit

Permalink
Add Flash Messages
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavSokov committed Jul 18, 2024
1 parent 6fd85ef commit 4fcc2c7
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 9 deletions.
9 changes: 9 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
var flashMessages = document.querySelectorAll('.flash');
flashMessages.forEach(function(flashMessage) {
flashMessage.style.display = 'none';
});
}, 5000);
});

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
20 changes: 18 additions & 2 deletions app/controllers/actual_db_schema/migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions app/controllers/actual_db_schema/phantom_migrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file.
6 changes: 5 additions & 1 deletion app/views/actual_db_schema/migrations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<html>
<head>
<title>Migrations</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
Expand Down Expand Up @@ -62,5 +63,8 @@
<p>No migrations found.</p>
<% end %>
</div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
</body>
</html>
6 changes: 5 additions & 1 deletion app/views/actual_db_schema/migrations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<html>
<head>
<title>Migration Details</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
Expand Down Expand Up @@ -50,5 +51,8 @@
style: ('display: none;' if migration[:status] == "up" || migration[:phantom]) %>
</div>
</div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
</body>
</html>
6 changes: 5 additions & 1 deletion app/views/actual_db_schema/phantom_migrations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<html>
<head>
<title>Phantom Migrations</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>
</head>
<body>
<div>
Expand Down Expand Up @@ -51,5 +52,8 @@
<p>No phantom migrations found.</p>
<% end %>
</div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
</body>
</html>
7 changes: 6 additions & 1 deletion app/views/actual_db_schema/phantom_migrations/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<html>
<head>
<title>Phantom Migration Details</title>
<%= stylesheet_link_tag 'actual_db_schema/styles', media: 'all' %>
<%= stylesheet_link_tag 'styles', media: 'all' %>
<%= javascript_include_tag 'application' %>

</head>
<body>
<div>
Expand Down Expand Up @@ -41,5 +43,8 @@
<%= button_to '⎌ Rollback', rollback_phantom_migration_path(id: params[:id], database: params[:database]), method: :post, class: 'button' %>
</div>
</div>
<% flash.each do |key, message| %>
<div class="flash <%= key %>"><%= message %></div>
<% end %>
</body>
</html>
2 changes: 1 addition & 1 deletion lib/actual_db_schema/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def active_record_setup
end
end
end
assert_select ".flash", text: "Migration 20130906111511 was successfully rolled back."
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/dummy_app/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
test:
adapter: sqlite3
database: tmp/primary.sqlite3
migrations_paths: "/home/vlad/projects/actual_db_schema/test/dummy_app/db/migrate"

0 comments on commit 4fcc2c7

Please sign in to comment.