* 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
64 lines
2.2 KiB
Ruby
64 lines
2.2 KiB
Ruby
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
|