2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-06 16:15:18 +00:00
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
class MetacodeSetsController < ApplicationController
|
2016-02-28 09:48:18 +00:00
|
|
|
before_action :require_admin
|
2015-09-10 14:12:50 +00:00
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
# GET /metacode_sets
|
|
|
|
# GET /metacode_sets.json
|
|
|
|
def index
|
2016-07-26 00:14:23 +00:00
|
|
|
@metacode_sets = MetacodeSet.order('name').all
|
2014-05-17 18:57:03 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html # index.html.erb
|
|
|
|
format.json { render json: @metacode_sets }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
### SHOW IS NOT CURRENTLY IN USE
|
|
|
|
# GET /metacode_sets/1
|
|
|
|
# GET /metacode_sets/1.json
|
2016-07-26 00:14:23 +00:00
|
|
|
# def show
|
|
|
|
# @metacode_set = MetacodeSet.find(params[:id])
|
|
|
|
#
|
|
|
|
# respond_to do |format|
|
|
|
|
# format.html # show.html.erb
|
|
|
|
# format.json { render json: @metacode_set }
|
|
|
|
# end
|
|
|
|
# end
|
2014-05-17 18:57:03 +00:00
|
|
|
|
|
|
|
# GET /metacode_sets/new
|
|
|
|
# GET /metacode_sets/new.json
|
|
|
|
def new
|
|
|
|
@metacode_set = MetacodeSet.new
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html # new.html.erb
|
|
|
|
format.json { render json: @metacode_set }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# GET /metacode_sets/1/edit
|
|
|
|
def edit
|
|
|
|
@metacode_set = MetacodeSet.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /metacode_sets
|
|
|
|
# POST /metacode_sets.json
|
|
|
|
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
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
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|
|
2016-07-26 00:14:23 +00:00
|
|
|
InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id)
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
2017-01-24 00:30:13 +00:00
|
|
|
format.html do
|
|
|
|
redirect_to metacode_sets_url,
|
|
|
|
notice: 'Metacode set was successfully created.'
|
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render json: @metacode_set, status: :created, location: metacode_sets_url
|
|
|
|
end
|
2014-05-17 18:57:03 +00:00
|
|
|
else
|
2016-07-26 00:14:23 +00:00
|
|
|
format.html { render action: 'new' }
|
2014-05-17 18:57:03 +00:00
|
|
|
format.json { render json: @metacode_set.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# PUT /metacode_sets/1
|
|
|
|
# PUT /metacode_sets/1.json
|
|
|
|
def update
|
|
|
|
@metacode_set = MetacodeSet.find(params[:id])
|
|
|
|
|
|
|
|
respond_to do |format|
|
2015-09-19 12:05:36 +00:00
|
|
|
if @metacode_set.update_attributes(metacode_set_params)
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
# build an array of the IDs of the metacodes currently in the set
|
2017-01-24 00:30:13 +00:00
|
|
|
current_metacodes = @metacode_set.metacodes.map { |m| m.id.to_s }
|
2014-05-17 18:57:03 +00:00
|
|
|
# get the list of desired metacodes for the set from the user input and build an array out of it
|
2017-01-24 00:30:13 +00:00
|
|
|
new_metacodes = params[:metacodes][:value].split(',')
|
2016-07-26 00:14:23 +00:00
|
|
|
|
|
|
|
# remove the metacodes that were in it, but now aren't
|
2017-01-24 00:30:13 +00:00
|
|
|
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
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
# add the new metacodes
|
2017-01-24 00:30:13 +00:00
|
|
|
added_metacodes = new_metacodes - current_metacodes
|
|
|
|
added_metacodes.each do |m|
|
2016-07-26 00:14:23 +00:00
|
|
|
InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id)
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
format.html { redirect_to metacode_sets_url, notice: 'Metacode set was successfully updated.' }
|
|
|
|
format.json { head :no_content }
|
|
|
|
else
|
2016-07-26 00:14:23 +00:00
|
|
|
format.html { render action: 'edit' }
|
2014-05-17 18:57:03 +00:00
|
|
|
format.json { render json: @metacode_set.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE /metacode_sets/1
|
|
|
|
# DELETE /metacode_sets/1.json
|
|
|
|
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
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to metacode_sets_url }
|
|
|
|
format.json { head :no_content }
|
|
|
|
end
|
|
|
|
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
|