diff --git a/app/assets/javascripts/application/collections.coffee b/app/assets/javascripts/application/collections.coffee index 979ceea3f1b9b6ba34751ba8cafc2866d64232fd..51855518dc564e05cf8a551eb7182a42c22f951f 100644 --- a/app/assets/javascripts/application/collections.coffee +++ b/app/assets/javascripts/application/collections.coffee @@ -22,9 +22,9 @@ $ -> d $(document).on 'open_collections_modal', (evt, params) -> - url = '/collections/list?type=' + params.type - url += '&id=' + encodeURIComponent(params.collection) if params.collection != undefined - url += '&learning_objects_ids=' + encodeURIComponent(JSON.stringify(params.learning_object)) if params.learning_object != undefined + id = if (params.collection == undefined) then "all" else encodeURIComponent(params.collection) + url = '/collections/' + id + '/list?type=' + params.type + url += '&learning_objects_ids=' + encodeURIComponent(params.learning_object) if params.learning_object != undefined $.get url, (d) -> $('#collections-modal').remove() $('body').append d @@ -64,13 +64,15 @@ $ -> return false if (data.learning_objects_ids.length < 1) || (data.collections_ids.length < 1) || (permitted_types.indexOf(data.type) < 0) - id = if (data.collection_id == undefined) then data.collections_ids else data.collection_id - - url = '/collections/' + encodeURIComponent(id) + '/' + data.type + id = if (data.collections_ids == undefined) then data.collection_id else data.collections_ids + url = '/collections/' + encodeURIComponent(id) + '/learning_objects/' + encodeURIComponent(data.learning_objects_ids.join()) $.ajax {method: "POST", url: url, data: data } - - location.reload(true) if !(refreshable_type.indexOf(data.type) < 0) - $('#collections-modal').modal('hide') + .done () -> + if (data.type == "move") + url = '/collections/' + encodeURIComponent(data.collection_id) + '/learning_objects/' + encodeURIComponent(data.learning_objects_ids.join()) + $.ajax {method: "DELETE", url: url, data: data } + location.reload(true) if !(refreshable_type.indexOf(data.type) < 0) + $('#collections-modal').modal('hide') return # manipulate collections @@ -116,8 +118,8 @@ $ -> return o.value if permitted_types[index] == 'remove' - url = '/collections/' + encodeURIComponent(collection) + '/remove' - $.ajax {method: 'DELETE', url: url, data: { learning_objects_ids: learning_objects } } if confirm('Você tem certeza?') + url = '/collections/' + encodeURIComponent(collection) + '/learning_objects/' + encodeURIComponent(learning_objects.join()) + $.ajax { method: 'DELETE', url: url } if confirm('Você tem certeza?') location.reload(true) else $(document).trigger('open_collections_modal', [collection: collection, type: permitted_types[index], learning_object: learning_objects ]) if !!(~index) diff --git a/app/controllers/collections_controller.rb b/app/controllers/collections_controller.rb index 6e48608711376f05c0b30baed537332b4cc0b0fc..7e68db0ddadd388764323a46deb243cd157b5bf6 100644 --- a/app/controllers/collections_controller.rb +++ b/app/controllers/collections_controller.rb @@ -1,6 +1,7 @@ class CollectionsController < ApplicationController - before_action :set_collection, only: [:show, :update, :destroy, :like, :list, :add_learning_object, :remove_learning_object, :move_learning_objects, :copy_learning_objects, :change_privacy] - before_action :authenticate_user!, only: [:update, :destroy, :like, :list, :add_learning_object, :remove_learning_object, :move_learning_objects, :copy_learning_objects, :change_privacy] + before_action :set_collection, only: [:show, :update, :destroy, :like, :change_privacy] + before_action :set_collections, only: [:list, :add_learning_object, :remove_learning_object] + before_action :authenticate_user!, only: [:update, :destroy, :like, :list, :add_learning_object, :remove_learning_object, :change_privacy] # GET /collections # GET /collections.json @@ -74,8 +75,13 @@ class CollectionsController < ApplicationController end def list + @collection = @collections.first + + # list all + @collection = nil if @collection == 'all' + @collections = collection_repository.all(Collections::UserContext.new(current_user)) - @collections.select!{|c| c.id != @collection.id} unless @collection.blank? + @collections.select! { |c| c.id != @collection.id } unless @collection.blank? @type = params[:type] unless params[:type].blank? @@ -84,37 +90,35 @@ class CollectionsController < ApplicationController # POST /collections/1/learning_object def add_learning_object - response = learning_objects_to_collections(@learning_objects, @collection) + @collections.each do |collection| + if collection.owner?(current_user) + @learning_objects.each do |learning_object| + collection.add learning_object + end - if request.xhr? - render json: {status: response} + collection_repository.save_learning_objects(collection) + end end - end - - # DELETE /collections/1/learning_object - def remove_learning_object - response = remove_learning_objects_from_collection(@learning_objects, @collection) if request.xhr? - render json: {status: response} + render json: {status: true} end end - def copy_learning_objects - response = learning_objects_to_collections(@learning_objects, @target_collections) + # DELETE /collections/1/learning_object + def remove_learning_object + @collections.each do |collection| + if collection.owner?(current_user) + @learning_objects.each do |learning_object| + collection.remove learning_object + end - if request.xhr? - render json: {status: response} + collection_repository.save_learning_objects(collection) + end end - end - - def move_learning_objects - response = learning_objects_to_collections(@learning_objects, @target_collections) - - remove_learning_objects_from_collection(@learning_objects, @collection) if response if request.xhr? - render json: {status: response} + render json: {status: true} end end @@ -133,25 +137,7 @@ class CollectionsController < ApplicationController end def learning_objects_to_collections(learning_objects, collections) - collections.each do |collection| - if collection.owner?(current_user) - learning_objects.each do |learning_object| - collection.add learning_object - end - - collection_repository.save_learning_objects(collection) - end - end - end - - def remove_learning_objects_from_collection(learning_objects, collection) - learning_objects = [learning_objects] if learning_objects.class == String - - learning_objects.each do |learning_object| - collection.remove learning_object - end - collection_repository.save_learning_objects(collection) end def check_collection_privacy!(collection) @@ -161,12 +147,24 @@ class CollectionsController < ApplicationController end def set_collection - @collection = collection_repository.find params[:id] unless params[:id].blank? + @collection = collection_repository.find params[:id] + end + + def set_collections + if params[:id] == "all" || params[:id].blank? + @collections = ['all'] + else + @collections = (params[:id].class == String) ? [collection_repository.find(params[:id])] : params[:id].map{|id| collection_repository.find id} + end - @target_collections = params[:collections_ids].map{|id| collection_repository.find id} unless params[:collections_ids].blank? + unless params[:learning_objects_ids].blank? + @learning_objects = [] - params[:learning_objects_ids] = JSON.parse(params[:learning_objects_ids]) if params[:learning_objects_ids].class == String - @learning_objects = params[:learning_objects_ids].map{|id| learning_object_repository.find id || false} unless params[:learning_objects_ids].blank? + params[:learning_objects_ids].split(',').each do |id| + object = learning_object_repository.find id + @learning_objects << object unless object.blank? + end + end end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/config/routes.rb b/config/routes.rb index 2a696ece014ef52d51b750a002ed883dae7c16a9..16a664c821c983a78dc861a1bf65a7ec4d63dcdb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -65,21 +65,17 @@ Rails.application.routes.draw do end end - # collection list - get '/collections/list' => "collections#list", as: 'collection_list' resources :collections do - member do + # collection list + get :list + # change privacy post :change_privacy # add/remove a learning object for some collection - post '/add', as: :add_learning_object, action: :add_learning_object - delete '/remove', as: :remove_learning_object, action: :remove_learning_object - - # copy / move learning objects from a collection to another - post '/copy', as: :copy_learning_objects, action: :copy_learning_objects - post '/move', as: :move_learning_objects, action: :move_learning_objects + post '/learning_objects/:learning_objects_ids', as: :add_learning_object, action: :add_learning_object + delete '/learning_objects/:learning_objects_ids', as: :remove_learning_object, action: :remove_learning_object end end