From 2ff83656512ce585ace633f23a444425a4bfd5c0 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Fri, 1 Mar 2024 12:50:41 -0600 Subject: [PATCH] add logic for tutorial requirement --- app/create.rb | 4 ++++ app/tutorial.rb | 10 ++++++++++ app_helpers.rb | 1 + migrations/124_sites_tutorial_required.rb | 9 +++++++++ views/_header.erb | 4 +++- views/site.erb | 2 +- views/tutorial/html/1.erb | 1 + views/tutorial/html/8.erb | 2 +- views/tutorial/html/9.erb | 8 ++------ 9 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 migrations/124_sites_tutorial_required.rb diff --git a/app/create.rb b/app/create.rb index d2f3b1e6..00061f74 100644 --- a/app/create.rb +++ b/app/create.rb @@ -104,6 +104,10 @@ post '/create' do return {result: 'error'}.to_json end + if defined?(BlackBox.tutorial_required?) && BlackBox.tutorial_required?(@site, request) + @site.tutorial_required = true + end + if !@site.valid? flash[:error] = @site.errors.first.last.first return {result: 'error'}.to_json diff --git a/app/tutorial.rb b/app/tutorial.rb index 2ffe69bd..582308ec 100644 --- a/app/tutorial.rb +++ b/app/tutorial.rb @@ -38,5 +38,15 @@ get '/tutorial/:section/:page/?' do @title = "#{params[:section].upcase} Tutorial - #{@page}/10" + + if @page == '9' + unless csrf_safe? + signout + redirect '/' + end + current_site.tutorial_required = false + current_site.save_changes validate: false + end + erb "tutorial/layout".to_sym end diff --git a/app_helpers.rb b/app_helpers.rb index ee6b2928..df0d4991 100644 --- a/app_helpers.rb +++ b/app_helpers.rb @@ -16,6 +16,7 @@ end def require_login redirect '/' unless signed_in? && current_site + redirect '/tutorial/html/1' if current_site.tutorial_required && !(request.path =~ /tutorial/) end def signed_in? diff --git a/migrations/124_sites_tutorial_required.rb b/migrations/124_sites_tutorial_required.rb new file mode 100644 index 00000000..d9003e85 --- /dev/null +++ b/migrations/124_sites_tutorial_required.rb @@ -0,0 +1,9 @@ +Sequel.migration do + up { + DB.add_column :sites, :tutorial_required, :boolean, default: false + } + + down { + DB.drop_column :sites, :tutorial_required + } +end \ No newline at end of file diff --git a/views/_header.erb b/views/_header.erb index 32795919..f6d0c78b 100644 --- a/views/_header.erb +++ b/views/_header.erb @@ -41,7 +41,9 @@
  • <% end %> -
  • Edit
  • + <% unless current_site.tutorial_required %> +
  • Edit
  • + <% end %>
  • View
  • Stats
  • diff --git a/views/site.erb b/views/site.erb index 0b246f4a..495b8d7b 100644 --- a/views/site.erb +++ b/views/site.erb @@ -27,7 +27,7 @@
    <%= site.tips_dataset.count %> tips
    - <% if current_site == site %> + <% if current_site == site && !current_site.tutorial_required %> Edit Site <% end %> diff --git a/views/tutorial/html/1.erb b/views/tutorial/html/1.erb index a3e0cd3e..eb7ddd5b 100644 --- a/views/tutorial/html/1.erb +++ b/views/tutorial/html/1.erb @@ -31,6 +31,7 @@
    diff --git a/views/tutorial/html/9.erb b/views/tutorial/html/9.erb index 792462f3..d6928d71 100644 --- a/views/tutorial/html/9.erb +++ b/views/tutorial/html/9.erb @@ -20,7 +20,7 @@ $('#saveToSite').on('click', function() { $.ajax({ - url: '/site_files/save/index.html?csrf_token=<%= Rack::Utils.escape csrf_token %>', + url: '/site_files/upload?csrf_token=<%= Rack::Utils.escape csrf_token %>&filename=<%= Rack::Utils.escape 'index.html' %>&site_id=<%= current_site.id %>', data: sessionStorage.getItem('tutorialHtml'), processData: false, contentType: false, @@ -29,11 +29,7 @@ alert('There has been an error saving your file, please try again. If it continues to fail, make a copy of the file locally so you don\'t lose your changes!') }, success: function(response){ - if(response == 'ok') { - window.location = '/tutorial/html/10' - } else { - alert('There has been an error saving your file, please try again. If it continues to fail, make a copy of the file locally so you don\'t lose your changes!') - } + window.location = '/tutorial/html/10' } })