From e76735031420fef1cc60db9176e7ecd8ba27f43e Mon Sep 17 00:00:00 2001 From: joaoGabriel55 Date: Wed, 25 May 2022 14:04:24 -0300 Subject: [PATCH] Fixed bug when creating admin tags Minor fix Implemented specs for tags_controller and tag model Code review --- app/controllers/admin/tags_controller.rb | 17 +++++++++++++---- spec/system/admin/tags_spec.rb | 11 +++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/tags_controller.rb b/app/controllers/admin/tags_controller.rb index 6ab6d1260..0dd52039e 100644 --- a/app/controllers/admin/tags_controller.rb +++ b/app/controllers/admin/tags_controller.rb @@ -1,17 +1,23 @@ class Admin::TagsController < Admin::BaseController before_action :find_tag, only: [:update, :destroy] + before_action :tags, only: [:index, :create] respond_to :html, :js def index - @tags = Tag.category.page(params[:page]) - @tag = Tag.category.new + @tag = Tag.category.new end def create - Tag.find_or_create_by!(name: tag_params["name"]).update!(kind: "category") + @tag = Tag.find_or_initialize_by(tag_params) - redirect_to admin_tags_path + save_and_update = @tag.save && @tag.update!(kind: "category") + + if save_and_update + redirect_to admin_tags_path + else + render :index + end end def destroy @@ -20,6 +26,9 @@ class Admin::TagsController < Admin::BaseController end private + def tags + @tags ||= Tag.category.page(params[:page]) + end def tag_params params.require(:tag).permit(:name) diff --git a/spec/system/admin/tags_spec.rb b/spec/system/admin/tags_spec.rb index d2bd745fe..2868a2726 100644 --- a/spec/system/admin/tags_spec.rb +++ b/spec/system/admin/tags_spec.rb @@ -105,4 +105,15 @@ describe "Admin tags", :admin do expect(page).to have_content "Soon a category" end + + scenario "Create shows validation error when tag name is empty" do + visit admin_tags_path + + within("form.new_tag") do + fill_in "tag_name", with: "" + click_button "Create topic" + end + + expect(page).to have_content "can't be blank" + end end