From 20e670ddb403ff5b6f3b50665f7f3e0e84410e44 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Wed, 28 Feb 2018 17:01:43 -0800 Subject: [PATCH] Steal the feedback form from PURL --- app/assets/javascripts/feedback_form.js | 136 ++++++++++++++++++ app/assets/stylesheets/feedback-form.scss | 16 +++ app/controllers/feedback_forms_controller.rb | 32 +++++ app/helpers/application_helper.rb | 4 + app/mailers/feedback_mailer.rb | 26 ++++ app/views/feedback_forms/new.html.erb | 1 + .../feedback_mailer/submit_feedback.text.erb | 10 ++ app/views/layouts/application.html.erb | 8 ++ .../shared/feedback_forms/_form.html.erb | 41 ++++++ .../feedback_forms/_reporting_from.html.erb | 2 + config/routes.rb | 45 +----- config/settings.yml | 1 + 12 files changed, 279 insertions(+), 43 deletions(-) create mode 100644 app/assets/javascripts/feedback_form.js create mode 100644 app/assets/stylesheets/feedback-form.scss create mode 100644 app/controllers/feedback_forms_controller.rb create mode 100644 app/mailers/feedback_mailer.rb create mode 100644 app/views/feedback_forms/new.html.erb create mode 100644 app/views/feedback_mailer/submit_feedback.text.erb create mode 100644 app/views/shared/feedback_forms/_form.html.erb create mode 100644 app/views/shared/feedback_forms/_reporting_from.html.erb diff --git a/app/assets/javascripts/feedback_form.js b/app/assets/javascripts/feedback_form.js new file mode 100644 index 0000000..030e5bf --- /dev/null +++ b/app/assets/javascripts/feedback_form.js @@ -0,0 +1,136 @@ +$(document).on("turbolinks:load", function(){ + + //Instantiates plugin for feedback form + + $("#feedback-form").feedbackForm(); +}) + + +;(function ( $, window, document, undefined ) { + /* + jQuery plugin that handles some of the feedback form functionality + + Usage: $(selector).feedbackForm(); + + No available options + + This plugin : + - changes feedback form link to button + - submits an ajax request for the feedback form + - displays alert on response from feedback form + */ + + var pluginName = "feedbackForm"; + + function Plugin( element, options ) { + this.element = element; + var $el, $form; + + this.options = $.extend( {}, options) ; + this._name = pluginName; + this.init(); + } + + function submitListener(){ + // Serialize and submit form if not on action url + $form.each(function(i, form){ + if (location !== form.action){ + $('#user_agent').val(navigator.userAgent); + $('#viewport').val('width:' + window.innerWidth + ' height:' + innerHeight); + $(form).on('submit', function(){ + var valuesToSubmit = $(this).serialize(); + $.ajax({ + url: form.action, + data: valuesToSubmit, + type: 'post' + }).success(function(response){ + if (isSuccess(response)){ + $($el).collapse('hide'); + $($form)[0].reset(); + } + renderFlashMessages(response); + }); + return false; + }); + + } + }); + } + + function isSuccess(response){ + switch(response[0][0]){ + case 'success': + return true; + default: + return false; + } + } + + function renderFlashMessages(response){ + $.each(response, function(i,val){ + var flashHtml = "
" + val[1] + "
"; + + // Show the flash message + $('div.flash_messages').html(flashHtml); + }); + } + + function replaceLink(form, link) { + var attrs = {}; + $.each(link[0].attributes, function(idx, attr) { + attrs[attr.nodeName] = attr.value; + }); + attrs.class = 'cancel-link btn btn-link'; + + // Replace the cancel link with a button + link.replaceWith(function() { + return $(' + <%= link_to "Cancel", :back, class:"cancel-link", data: {toggle:"collapse", target:"#feedback-form"} %> + + + + <% end %> + diff --git a/app/views/shared/feedback_forms/_reporting_from.html.erb b/app/views/shared/feedback_forms/_reporting_from.html.erb new file mode 100644 index 0000000..dc9c2e0 --- /dev/null +++ b/app/views/shared/feedback_forms/_reporting_from.html.erb @@ -0,0 +1,2 @@ +Reporting from: <%= request.referer %> +<%= hidden_field_tag :url, request.referer, class:"reporting-from-field" %> diff --git a/config/routes.rb b/config/routes.rb index 07281d2..5c25f8c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,47 +21,6 @@ resources :reserves - - # Sample resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Sample resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Sample resource route with more complex sub-resources - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', :on => :collection - # end - # end - - # Sample resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end - - # You can have the root of your site routed with "root" - # just remember to delete public/index.html. - # root :to => 'welcome#index' - - # See how all your routes lay out with "rake routes" - - # This is a legacy wild controller route that's not recommended for RESTful applications. - # Note: This route will make all actions in every controller accessible via GET requests. - # match ':controller(/:action(/:id))(.:format)' + resource :feedback_form, path: 'feedback', only: [:new, :create] + get 'feedback' => 'feedback_forms#new' end diff --git a/config/settings.yml b/config/settings.yml index 747dc2a..913de93 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -5,6 +5,7 @@ email: from: "no-reply@reserves.stanford.edu" reports: "searchworks-reports@lists.stanford.edu" allforms: "course-reserves-allforms@lists.stanford.edu" + feedback: email_mapping: "ART-RESV": "artlibrary@stanford.edu"