strip down admin endpoints to just json

This commit is contained in:
Connor Turland 2018-03-09 15:54:30 -05:00
parent 245bb88112
commit 0bbe838483
3 changed files with 45 additions and 103 deletions

View file

@ -5,84 +5,64 @@ class MetacodeSetsController < ApplicationController
before_action :require_admin, except: :index before_action :require_admin, except: :index
# GET /metacode_sets # GET /metacode_sets
# GET /metacode_sets.json
def index def index
@metacode_sets = MetacodeSet.order('name').all @metacode_sets = MetacodeSet.order('name').all
render json: metacode_sets_json
respond_to do |format|
format.json { render json: metacode_sets_json }
end
end end
# POST /metacode_sets # POST /metacode_sets
# POST /metacode_sets.json
def create def create
@user = current_user @user = current_user
@metacode_set = MetacodeSet.new(metacode_set_params) @metacode_set = MetacodeSet.new(metacode_set_params)
@metacode_set.user_id = @user.id @metacode_set.user_id = @user.id
if @metacode_set.save
respond_to do |format| # create the InMetacodeSet for all the metacodes that were selected for the set
if @metacode_set.save @metacodes = params[:metacodes][:value].split(',')
# create the InMetacodeSet for all the metacodes that were selected for the set @metacodes.each do |m|
@metacodes = params[:metacodes][:value].split(',') InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id)
@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 }
end end
render json: @metacode_set, status: :created
else
render json: @metacode_set.errors, status: :unprocessable_entity
end end
end end
# PUT /metacode_sets/1 # PUT /metacode_sets/1
# PUT /metacode_sets/1.json
def update def update
@metacode_set = MetacodeSet.find(params[:id]) @metacode_set = MetacodeSet.find(params[:id])
if @metacode_set.update_attributes(metacode_set_params)
respond_to do |format| # build an array of the IDs of the metacodes currently in the set
if @metacode_set.update_attributes(metacode_set_params) 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 # remove the metacodes that were in it, but now aren't
current_metacodes = @metacode_set.metacodes.map { |m| m.id.to_s } removed_metacodes = current_metacodes - new_metacodes
# get the list of desired metacodes for the set from the user input and build an array out of it removed_metacodes.each do |m|
new_metacodes = params[:metacodes][:value].split(',') inmetacodeset = InMetacodeSet.find_by(metacode_id: m, metacode_set_id: @metacode_set.id)
inmetacodeset.destroy
# 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 }
end 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
end end
# DELETE /metacode_sets/1 # DELETE /metacode_sets/1
# DELETE /metacode_sets/1.json
def destroy def destroy
@metacode_set = MetacodeSet.find(params[:id]) @metacode_set = MetacodeSet.find(params[:id])
# delete everything that tracks what's in the set # delete everything that tracks what's in the set
@metacode_set.in_metacode_sets.each(&:destroy) @metacode_set.in_metacode_sets.each(&:destroy)
@metacode_set.destroy @metacode_set.destroy
head :no_content
respond_to do |format|
format.json { head :no_content }
end
end end
private private

View file

@ -2,76 +2,39 @@
class MetacodesController < ApplicationController class MetacodesController < ApplicationController
before_action :require_admin, except: %i[index show] 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
# GET /metacodes.json
def index def index
@metacodes = Metacode.order('name').all @metacodes = Metacode.order('name').all
render json: @metacodes
respond_to do |format|
format.html do
return unless require_admin
render :index
end
format.json { render json: @metacodes }
end
end end
# GET /metacodes/1.json # GET /metacodes/1
# GET /metacodes/Action.json # GET /metacodes/Action
# GET /metacodes/action.json # GET /metacodes/action
def show def show
@metacode = Metacode.where('DOWNCASE(name) = ?', downcase(params[:name])).first if params[:name] @metacode = Metacode.where('DOWNCASE(name) = ?', downcase(params[:name])).first if params[:name]
set_metacode unless @metacode set_metacode unless @metacode
render json: @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
end end
# POST /metacodes # POST /metacodes
# POST /metacodes.json
def create def create
@metacode = Metacode.new(metacode_params) @metacode = Metacode.new(metacode_params)
if @metacode.save
respond_to do |format| render json: @metacode, status: :created
if @metacode.save else
format.html { redirect_to metacodes_url, notice: 'Metacode was successfully created.' } render json: @metacode.errors, status: :unprocessable_entity
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
end end
end end
# PUT /metacodes/1 # PUT /metacodes/1
# PUT /metacodes/1.json
def update def update
respond_to do |format| if @metacode.update(metacode_params)
if @metacode.update(metacode_params) head :no_content
format.html { redirect_to metacodes_url, notice: 'Metacode was successfully updated.' } else
format.json { head :no_content } render json: @metacode.errors, status: :unprocessable_entity
else
format.html { render :edit }
format.json { render json: @metacode.errors, status: :unprocessable_entity }
end
end end
end end

View file

@ -15,7 +15,6 @@ Metamaps::Application.routes.draw do
get 'starred' get 'starred'
get 'mapper/:id', action: 'mapper' get 'mapper/:id', action: 'mapper'
end end
get :explore, to: redirect('/')
resources :maps, except: %i[index edit] do resources :maps, except: %i[index edit] do
member do member do
@ -70,7 +69,7 @@ Metamaps::Application.routes.draw do
resources :metacode_sets, except: [:show] resources :metacode_sets, except: [:show]
resources :metacodes, except: [:destroy] resources :metacodes, except: [:new, :edit, :destroy]
get 'metacodes/:name', to: 'metacodes#show' get 'metacodes/:name', to: 'metacodes#show'
namespace :search do namespace :search do