From 1a989bef07834ad46ea789a0775e406cdcbc4f3f Mon Sep 17 00:00:00 2001 From: Andy Zhao <17884966+Zhao-Andy@users.noreply.github.com> Date: Thu, 26 Mar 2020 14:48:00 -0400 Subject: [PATCH] [deploy] Add response templates to user settings (#6823) * Add response templates controller, routes, and view * Add response templates settings page * Remove dead code * Use proper variable oops * Use proper indentation * Use before action for checking current_user * Remove index action temporarily * Use url helpers for redirects * Use form_with over form_tag and form_for * Persist previous content if edit fails * Use exists for performance * Remove index spec and add CRUD specs * Memoize @response_template * Use workaround for sad paths b/c render doesn't work * Refactor and extract permitted_attributes into var * Use present over exists to preload into memory * Use last over first * Lint for codeclimate * Use two redirects for happy and sad create path * Remove proper index route oops * Codeclimate comma sigh --- app/assets/stylesheets/settings.scss | 40 +++++---- .../response_templates_controller.rb | 64 ++++++++++++++ app/controllers/users_controller.rb | 7 ++ .../response_templates/index.json.jbuilder | 8 ++ app/views/users/_response_templates.html.erb | 39 +++++++++ config/routes.rb | 1 + spec/requests/response_templates_spec.rb | 85 +++++++++++++++++++ 7 files changed, 226 insertions(+), 18 deletions(-) create mode 100644 app/controllers/response_templates_controller.rb create mode 100644 app/views/response_templates/index.json.jbuilder create mode 100644 app/views/users/_response_templates.html.erb create mode 100644 spec/requests/response_templates_spec.rb diff --git a/app/assets/stylesheets/settings.scss b/app/assets/stylesheets/settings.scss index 2d2033087..faaad7da5 100755 --- a/app/assets/stylesheets/settings.scss +++ b/app/assets/stylesheets/settings.scss @@ -529,8 +529,13 @@ $input-width: 650px; } .github-repo-row-name { font-weight: bold; - button { - transition: all 0.6s ease; + form { + display: block; + float: right; + } + button, + a[role='button'] { + transition: all 0.4s ease; cursor: pointer; font-size: 1.2em; font-weight: bold; @@ -545,6 +550,9 @@ $input-width: 650px; &:hover, &:focus { background: $green; + &.danger-button { + background: $red; + } } &:disabled { cursor: wait; @@ -552,23 +560,19 @@ $input-width: 650px; center center; background-size: 75px; } + a { + color: $black; + } + } + &.response-template-row-name { + button, + a[role='button'] { + width: 80px; + margin-left: 2.5px; + margin-right: 2.5px; + text-align: center; + } } - } - .github-repo-fork { - margin-left: 5px; - font-weight: 400; - background: $purple; - color: $bold-blue; - border-radius: 3px; - padding: 1px 6px; - font-size: 0.9em; - } - .github-repo-featured-indicator { - display: inline-block; - float: right; - background: $green; - color: white; - border: 0px; } } } diff --git a/app/controllers/response_templates_controller.rb b/app/controllers/response_templates_controller.rb new file mode 100644 index 000000000..6434c924e --- /dev/null +++ b/app/controllers/response_templates_controller.rb @@ -0,0 +1,64 @@ +class ResponseTemplatesController < ApplicationController + after_action :verify_authorized + + def create + authorize ResponseTemplate + response_template.user_id = current_user.id + response_template.content_type = "body_markdown" + response_template.type_of = "personal_comment" + + if response_template.save + flash[:settings_notice] = "Your response template \"#{response_template.title}\" was created." + redirect_to user_settings_path(tab: "response-templates", id: response_template.id) + else + flash[:error] = "Response template error: #{response_template.errors.full_messages.to_sentence}" + attributes = permitted_attributes(ResponseTemplate) + redirect_to user_settings_path( + tab: "response-templates", + id: response_template.id, + previous_title: attributes[:title], + previous_content: attributes[:content], + ) + end + end + + def destroy + authorize response_template + + if response_template.destroy + flash[:settings_notice] = "Your response template \"#{response_template.title}\" was deleted." + else + flash[:error] = response_template.errors.full_messages.to_sentence # this will probably never fail + end + + redirect_to user_settings_path(tab: "response-templates") + end + + def update + authorize response_template + + attributes = permitted_attributes(ResponseTemplate) + if response_template.update(attributes) + flash[:settings_notice] = "Your response template \"#{response_template.title}\" was updated." + redirect_to user_settings_path(tab: "response-templates", id: response_template.id) + else + flash[:error] = "Response template error: #{response_template.errors.full_messages.to_sentence}" + redirect_to user_settings_path( + tab: "response-templates", + id: response_template.id, + previous_title: attributes[:title], + previous_content: attributes[:content], + ) + end + end + + private + + def response_template + @response_template ||= if params[:id].present? + ResponseTemplate.find(params[:id]) + else + ResponseTemplate.new(permitted_attributes(ResponseTemplate)) + end + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4b9c72e8b..429de8024 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -241,6 +241,8 @@ class UsersController < ApplicationController handle_pro_membership_tab when "account" handle_account_tab + when "response-templates" + handle_response_templates_tab else not_found unless @tab_list.map { |t| t.downcase.tr(" ", "-") }.include? @tab end @@ -307,6 +309,11 @@ class UsersController < ApplicationController HEREDOC end + def handle_response_templates_tab + @response_templates = current_user.response_templates + @response_template = ResponseTemplate.find_or_initialize_by(id: params[:id], user: current_user) + end + def set_user @user = current_user not_found unless @user diff --git a/app/views/response_templates/index.json.jbuilder b/app/views/response_templates/index.json.jbuilder new file mode 100644 index 000000000..6dd11e71f --- /dev/null +++ b/app/views/response_templates/index.json.jbuilder @@ -0,0 +1,8 @@ +json.array!(@response_templates) do |response_template| + json.id response_template.id + json.type_of response_template.type_of + json.user_id response_template.user_id + json.title response_template.title + json.content response_template.content + json.content_truncated truncate(response_template.content, length: 200) +end diff --git a/app/views/users/_response_templates.html.erb b/app/views/users/_response_templates.html.erb new file mode 100644 index 000000000..d38a15e7a --- /dev/null +++ b/app/views/users/_response_templates.html.erb @@ -0,0 +1,39 @@ +
Response templates are snippets that you can re-use for quick and accurate comments.
+ +<% if @response_templates.present? %> +