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

add tag controller and tests

parent ebaea684
No related branches found
No related tags found
No related merge requests found
......@@ -3,20 +3,19 @@ module TaggableController
included do
before_action :authenticate_user!, only: [:tagging, :untagging]
before_action :set_user_tags, only: [:tagging, :untagging]
end
# POST /v1/learning_objects/1/tagging
# POST /v1/learning_objects/1/tagging.json
def tagging
current_user.tag(taggable, with: @user_tags.map(&:name) << tag_params[:name], on: :tags)
current_user.tag(taggable, with: [tag_params[:name]])
render json: taggable, status: :created
end
# DELETE /v1/learning_objects/1/untagging
# DELETE /v1/learning_objects/1/untagging.json
def untagging
current_user.tag(taggable, with: @user_tags.map(&:name).select { |tag| tag != tag_params[:name] }, on: :tags)
current_user.untag(taggable, tag_params[:name])
render json: taggable, status: :ok
end
......@@ -30,7 +29,4 @@ module TaggableController
params.require(:tag).permit(:name)
end
def set_user_tags
@user_tags = taggable.owner_tags_on(current_user, :tags)
end
end
......@@ -65,7 +65,7 @@ class V1::LearningObjectsController < ApplicationController
# Never trust parameters from the scary internet, only allow the white list through.
def learning_object_params
params[:learning_object].permit(:author, :name, :object_type_id, :description, :school_level, :language, :link, topics: [])
params[:learning_object].permit(:author, :name, :object_type_id, :description, :school_level, :language, :link, :tags)
end
def authorize!
......
class CollectionSerializer < ActiveModel::Serializer
attributes :id, :name, :created_at, :updated_at, :description, :privacy, :score, :tags
attributes :id, :name, :created_at, :updated_at, :description, :privacy, :score
has_many :tags
end
class LearningObjectSerializer < ActiveModel::Serializer
attributes :id, :name, :created_at, :updated_at, :description, :author, :score, :tags
attributes :id, :name, :created_at, :updated_at, :description, :author, :score
has_many :tags
end
......@@ -39,16 +39,13 @@ class V1::CollectionsControllerTest < ActionController::TestCase
post :tagging, id: collections(:ufpr).id, tag: { name: 'Test' }
assert_response :created
assert_equal ['Test'], collections(:ufpr).all_tags_list
end
test 'should user untagging a collection' do
auth_request users(:jack)
post :tagging, id: collections(:ufpr).id, tag: { name: 'Test' }
assert_response :created
auth_request users(:john)
tag = tags(:tag_one).name
delete :untagging, id: collections(:ufpr).id, tag: { name: 'Test' }
delete :untagging, id: collections(:ufpr).id, tag: { name: tag }
assert_response :ok
end
......
tagging_one:
tag: tag_one
user: john
taggable: ufpr (Collection)
tagging_two:
tag: tag_one
user: jack
taggable: one (LearningObject)
\ No newline at end of file
tag_one:
name: 'Português'
......@@ -8,6 +8,10 @@ class TagTest < ActiveSupport::TestCase
lo = learning_objects(:one)
assert john.tag(lo, with: ['Tag'])
# check lo_one tags
assert_count 2, lo.tags
assert_count 2, john.taggings
end
test 'an user can untag a learning object' do
......@@ -15,6 +19,10 @@ class TagTest < ActiveSupport::TestCase
lo = learning_objects(:one)
assert john.untag(lo, 'Tag')
# check lo_one tags
assert_count 1, lo.tags
assert_count 1, john.taggings
end
test 'an user can tag a collection' do
......@@ -22,6 +30,10 @@ class TagTest < ActiveSupport::TestCase
collection = collections(:ufpr)
assert john.tag(collection, with: ['Tag'])
# check ufpr tags
assert_count 2, collection.tags
assert_count 2, john.taggings
end
test 'an user can untag a collection' do
......@@ -29,6 +41,10 @@ class TagTest < ActiveSupport::TestCase
collection = collections(:ufpr)
assert john.untag(collection, 'Tag')
# check ufpr tags
assert_count 1, collection.tags
assert_count 1, john.taggings
end
......
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