2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
class MetacodeSetsController < ApplicationController
|
2018-03-08 22:12:24 +00:00
|
|
|
include MetacodesHelper
|
2018-03-08 23:32:56 +00:00
|
|
|
before_action :require_admin, except: :index
|
2015-09-10 14:12:50 +00:00
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
# GET /metacode_sets
|
|
|
|
def index
|
2016-07-26 00:14:23 +00:00
|
|
|
@metacode_sets = MetacodeSet.order('name').all
|
2018-03-09 20:54:30 +00:00
|
|
|
render json: metacode_sets_json
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# POST /metacode_sets
|
|
|
|
def create
|
|
|
|
@user = current_user
|
2015-09-19 12:05:36 +00:00
|
|
|
@metacode_set = MetacodeSet.new(metacode_set_params)
|
2014-05-17 18:57:03 +00:00
|
|
|
@metacode_set.user_id = @user.id
|
2018-03-09 20:54:30 +00:00
|
|
|
if @metacode_set.save
|
|
|
|
# create the InMetacodeSet for all the metacodes that were selected for the set
|
|
|
|
@metacodes = params[:metacodes][:value].split(',')
|
|
|
|
@metacodes.each do |m|
|
|
|
|
InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id)
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
2018-03-09 20:54:30 +00:00
|
|
|
render json: @metacode_set, status: :created
|
|
|
|
else
|
|
|
|
render json: @metacode_set.errors, status: :unprocessable_entity
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /metacode_sets/1
|
|
|
|
def update
|
|
|
|
@metacode_set = MetacodeSet.find(params[:id])
|
2018-03-09 20:54:30 +00:00
|
|
|
if @metacode_set.update_attributes(metacode_set_params)
|
|
|
|
|
|
|
|
# build an array of the IDs of the metacodes currently in the set
|
|
|
|
current_metacodes = @metacode_set.metacodes.map { |m| m.id.to_s }
|
|
|
|
# get the list of desired metacodes for the set from the user input and build an array out of it
|
|
|
|
new_metacodes = params[:metacodes][:value].split(',')
|
|
|
|
|
|
|
|
# remove the metacodes that were in it, but now aren't
|
|
|
|
removed_metacodes = current_metacodes - new_metacodes
|
|
|
|
removed_metacodes.each do |m|
|
|
|
|
inmetacodeset = InMetacodeSet.find_by(metacode_id: m, metacode_set_id: @metacode_set.id)
|
|
|
|
inmetacodeset.destroy
|
|
|
|
end
|
2014-05-17 18:57:03 +00:00
|
|
|
|
2018-03-09 20:54:30 +00:00
|
|
|
# add the new metacodes
|
|
|
|
added_metacodes = new_metacodes - current_metacodes
|
|
|
|
added_metacodes.each do |m|
|
|
|
|
InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id)
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
2018-03-09 20:54:30 +00:00
|
|
|
|
|
|
|
head :no_content
|
|
|
|
else
|
|
|
|
render json: @metacode_set.errors, status: :unprocessable_entity
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /metacode_sets/1
|
|
|
|
def destroy
|
|
|
|
@metacode_set = MetacodeSet.find(params[:id])
|
2016-07-26 00:14:23 +00:00
|
|
|
# delete everything that tracks what's in the set
|
|
|
|
@metacode_set.in_metacode_sets.each(&:destroy)
|
2014-05-17 18:57:03 +00:00
|
|
|
@metacode_set.destroy
|
2018-03-09 20:54:30 +00:00
|
|
|
head :no_content
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
2015-09-19 08:26:34 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def metacode_set_params
|
|
|
|
params.require(:metacode_set).permit(:desc, :mapperContributed, :name)
|
|
|
|
end
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|