diff --git a/app/controllers/metacode_sets_controller.rb b/app/controllers/metacode_sets_controller.rb index 4ac60a60..2f81c7f4 100644 --- a/app/controllers/metacode_sets_controller.rb +++ b/app/controllers/metacode_sets_controller.rb @@ -5,84 +5,64 @@ class MetacodeSetsController < ApplicationController before_action :require_admin, except: :index # GET /metacode_sets - # GET /metacode_sets.json def index @metacode_sets = MetacodeSet.order('name').all - - respond_to do |format| - format.json { render json: metacode_sets_json } - end + render json: metacode_sets_json end # POST /metacode_sets - # POST /metacode_sets.json def create @user = current_user @metacode_set = MetacodeSet.new(metacode_set_params) @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| - InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id) - end - format.json do - render json: @metacode_set, status: :created, location: metacode_sets_url - end - else - format.json { render json: @metacode_set.errors, status: :unprocessable_entity } + 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) end + render json: @metacode_set, status: :created + else + render json: @metacode_set.errors, status: :unprocessable_entity end end # PUT /metacode_sets/1 - # PUT /metacode_sets/1.json def update @metacode_set = MetacodeSet.find(params[:id]) + if @metacode_set.update_attributes(metacode_set_params) - respond_to do |format| - 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(',') - # 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 - - # 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) - end - - format.json { head :no_content } - else - format.json { render json: @metacode_set.errors, status: :unprocessable_entity } + # 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 + + # 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) + end + + head :no_content + else + render json: @metacode_set.errors, status: :unprocessable_entity end end # DELETE /metacode_sets/1 - # DELETE /metacode_sets/1.json def destroy @metacode_set = MetacodeSet.find(params[:id]) - # delete everything that tracks what's in the set @metacode_set.in_metacode_sets.each(&:destroy) - @metacode_set.destroy - - respond_to do |format| - format.json { head :no_content } - end + head :no_content end private diff --git a/app/controllers/metacodes_controller.rb b/app/controllers/metacodes_controller.rb index faf32332..2c379849 100644 --- a/app/controllers/metacodes_controller.rb +++ b/app/controllers/metacodes_controller.rb @@ -2,76 +2,39 @@ class MetacodesController < ApplicationController before_action :require_admin, except: %i[index show] - before_action :set_metacode, only: %i[edit update] + before_action :set_metacode, only: %i[update] # GET /metacodes - # GET /metacodes.json def index @metacodes = Metacode.order('name').all - - respond_to do |format| - format.html do - return unless require_admin - render :index - end - format.json { render json: @metacodes } - end + render json: @metacodes end - # GET /metacodes/1.json - # GET /metacodes/Action.json - # GET /metacodes/action.json + # GET /metacodes/1 + # GET /metacodes/Action + # GET /metacodes/action def show @metacode = Metacode.where('DOWNCASE(name) = ?', downcase(params[:name])).first if params[:name] set_metacode unless @metacode - - respond_to do |format| - format.json { render json: @metacode } - end - end - - # GET /metacodes/new - # GET /metacodes/new.json - def new - @metacode = Metacode.new - - respond_to do |format| - format.html - format.json { render json: @metacode } - end - end - - # GET /metacodes/1/edit - def edit + render json: @metacode end # POST /metacodes - # POST /metacodes.json def create @metacode = Metacode.new(metacode_params) - - respond_to do |format| - if @metacode.save - format.html { redirect_to metacodes_url, notice: 'Metacode was successfully created.' } - format.json { render json: @metacode, status: :created, location: metacodes_url } - else - format.html { render :new } - format.json { render json: @metacode.errors, status: :unprocessable_entity } - end + if @metacode.save + render json: @metacode, status: :created + else + render json: @metacode.errors, status: :unprocessable_entity end end # PUT /metacodes/1 - # PUT /metacodes/1.json def update - respond_to do |format| - if @metacode.update(metacode_params) - format.html { redirect_to metacodes_url, notice: 'Metacode was successfully updated.' } - format.json { head :no_content } - else - format.html { render :edit } - format.json { render json: @metacode.errors, status: :unprocessable_entity } - end + if @metacode.update(metacode_params) + head :no_content + else + render json: @metacode.errors, status: :unprocessable_entity end end diff --git a/config/routes.rb b/config/routes.rb index d0aafc6a..c95286bd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,7 +15,6 @@ Metamaps::Application.routes.draw do get 'starred' get 'mapper/:id', action: 'mapper' end - get :explore, to: redirect('/') resources :maps, except: %i[index edit] do member do @@ -70,7 +69,7 @@ Metamaps::Application.routes.draw do resources :metacode_sets, except: [:show] - resources :metacodes, except: [:destroy] + resources :metacodes, except: [:new, :edit, :destroy] get 'metacodes/:name', to: 'metacodes#show' namespace :search do