Skip to content
Snippets Groups Projects
Commit d08a7ef4 authored by Giovanne Marcelo's avatar Giovanne Marcelo
Browse files

fixing taggable controller

parent 74a195f2
No related branches found
No related tags found
No related merge requests found
...@@ -3,25 +3,26 @@ module TaggableController ...@@ -3,25 +3,26 @@ module TaggableController
included do included do
before_action :authenticate_user!, only: [:tagging, :untagging] before_action :authenticate_user!, only: [:tagging, :untagging]
before_action :set_user_tags, only: [:tagging, :untagging]
end end
# POST /v1/learning_objects/1/tagging # POST /v1/learning_objects/1/tagging
# POST /v1/learning_objects/1/tagging.json # POST /v1/learning_objects/1/tagging.json
def tagging def tagging
if current_user.tag(taggable, with: taggable.owner_tags_on(current_user, :tags) << tag_params[:name], on: :tags) if current_user.tag(taggable, with: @user_tags << tag_params[:name], on: :tags)
render json: @taggable, status: :created render json: taggable, status: :created
else else
render json: @taggable.errors, status: :unprocessable_entity render json: taggable.errors, status: :unprocessable_entity
end end
end end
# DELETE /v1/learning_objects/1/untagging # DELETE /v1/learning_objects/1/untagging
# DELETE /v1/learning_objects/1/untagging.json # DELETE /v1/learning_objects/1/untagging.json
def untagging def untagging
if current_user.tag(taggable, with: taggable.owner_tags_on(current_user, :tags).delete(tag_params[:name]), on: :tags) if current_user.tag(taggable, with: @user_tags.delete(params[:name]), on: :tags)
render json: @taggable, status: :ok render json: taggable, status: :ok
else else
render json: @taggable.errors, status: :unprocessable_entity render json: taggable.errors, status: :unprocessable_entity
end end
end end
...@@ -34,4 +35,8 @@ module TaggableController ...@@ -34,4 +35,8 @@ module TaggableController
def tag_params def tag_params
params.require(:tag).permit(:name) params.require(:tag).permit(:name)
end end
def set_user_tags
@user_tags = taggable.owner_tags_on(current_user, :tags)
end
end end
...@@ -60,7 +60,7 @@ class V1::CollectionsControllerTest < ActionController::TestCase ...@@ -60,7 +60,7 @@ class V1::CollectionsControllerTest < ActionController::TestCase
auth_request users(:john) auth_request users(:john)
delete :untagging, id: collections(:ufpr).id, tag: { name: 'Test' } delete :untagging, id: collections(:ufpr).id, tag: { name: 'Test' }
assertok_response :unprocessable_entity assert_response :unprocessable_entity
end end
test 'should user untagging a tag that does not exist' do test 'should user untagging a tag that does not exist' do
...@@ -70,7 +70,7 @@ class V1::CollectionsControllerTest < ActionController::TestCase ...@@ -70,7 +70,7 @@ class V1::CollectionsControllerTest < ActionController::TestCase
assert_response :created assert_response :created
delete :untagging, id: collections(:ufpr).id, tag: { name: 'Test2' } delete :untagging, id: collections(:ufpr).id, tag: { name: 'Test2' }
assertok_response :unprocessable_entity assert_response :unprocessable_entity
end end
private private
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment