class V1::CollectionsController < ApplicationController include ::SociableController include ::DownloadableController include ::FollowableController include ::TaggableController include ::DeletedObjectsController include ::HighlightsController include ::Paginator include ::SubjectableController include ::StageableController before_action :authenticate_user!, only: [:create, :update, :destroy, :tagging, :untagging] before_action :set_collection, only: [:show, :update, :destroy, :add_object, :delete_object, :subjecting, :unsubjecting, :add_stages, :remove_stages] before_action :set_new_collection, only: :index before_action :authorize!, except: [:create, :tagging, :untagging, :follow, :unfollow, :download] # GET /v1/collections # GET /v1/collections.json def index collections = paginate policy_scope(Collection) render json: collections end # GET /v1/collections/1 # GET /v1/collections/1.json def show render json: @collection end # POST /v1/collection # POST /v1/collection.json def create collection = Collection.new(collection_params) collection.owner = current_user if collection.owner.nil? authorize collection if collection.save collection_associations(collection) render json: collection, status: :created else render json: collection.errors, status: :unprocessable_entity end end # PUT/PATCH /v1/users/1 # PUT/PATCH /v1/users/1.json def update if @collection.update(collection_params) collection_associations(@collection) render json: @collection, status: :ok else render json: @collection.errors, status: :unprocessable_entity end end # DELETE /v1/collections/1 # DELETE /v1/collections/1.json def destroy @collection.destroy render status: :ok end # POST /v1/collections/1/items def add_object return render nothing: true, status: :unprocessable_entity if extra_params.blank? || extra_params[:items].blank? @collection.add_items(extra_params[:items]) render json: @collection, status: :ok end # DELETE /v1/collections/1/items def delete_object return render nothing: true, status: :unprocessable_entity if extra_params.blank? || extra_params[:items].blank? @collection.delete_items(extra_params[:items]) render json: @collection, status: :ok end private def deleted_resource; Collection; end def highlights_resource; Collection; end # social concerns methods def followable; set_collection; end def taggable; set_collection; end def sociable; set_collection; end def downloadable; set_collection; end def subjectable; set_collection; end def stageable; set_collection; end def set_collection @collection ||= Collection.find(params[:id]) end def set_new_collection @collection ||= Collection.new end # Never trust parameters from the scary internet, only allow the white list through. def collection_params params.require(:collection).permit(:name, :description, :owner_id, :owner_type, :privacy, tags: []) end def extra_params return {} if params[:collection].nil? params[:collection].permit(subjects: [], educational_stages: [], items: [:id, :type, :position], tags: [:name]) end def collection_associations(collection) if extra_params[:tags] == [] current_user.untag(collection, with: @collection.tags.map { |t| t['name'] }) elsif !extra_params[:tags].nil? current_user.tag(collection, with: extra_params[:tags].map { |t| t['name'] }) end collection.add_subjects(ids: extra_params[:subjects]) unless extra_params[:subjects].nil? collection.add_educational_stages(ids: extra_params[:educational_stages]) unless extra_params[:educational_stages].nil? collection.add_items(extra_params[:items]) unless extra_params[:items].nil? end def authorize! authorize @collection end end