Fixed bug when creating admin tags

Minor fix

Implemented specs for tags_controller and tag model

Code review
This commit is contained in:
joaoGabriel55
2022-05-25 14:04:24 -03:00
parent 4f1f7eaa40
commit e767350314
2 changed files with 24 additions and 4 deletions

View File

@@ -1,17 +1,23 @@
class Admin::TagsController < Admin::BaseController class Admin::TagsController < Admin::BaseController
before_action :find_tag, only: [:update, :destroy] before_action :find_tag, only: [:update, :destroy]
before_action :tags, only: [:index, :create]
respond_to :html, :js respond_to :html, :js
def index def index
@tags = Tag.category.page(params[:page]) @tag = Tag.category.new
@tag = Tag.category.new
end end
def create 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 end
def destroy def destroy
@@ -20,6 +26,9 @@ class Admin::TagsController < Admin::BaseController
end end
private private
def tags
@tags ||= Tag.category.page(params[:page])
end
def tag_params def tag_params
params.require(:tag).permit(:name) params.require(:tag).permit(:name)

View File

@@ -105,4 +105,15 @@ describe "Admin tags", :admin do
expect(page).to have_content "Soon a category" expect(page).to have_content "Soon a category"
end 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 end