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,44 +5,31 @@ 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
render json: @metacode_set, status: :created
else
format.json { render json: @metacode_set.errors, status: :unprocessable_entity }
end
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])
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
@ -63,26 +50,19 @@ class MetacodeSetsController < ApplicationController
InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id)
end
format.json { head :no_content }
head :no_content
else
format.json { render json: @metacode_set.errors, status: :unprocessable_entity }
end
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

View file

@ -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 }
render json: @metacode, status: :created
else
format.html { render :new }
format.json { render json: @metacode.errors, status: :unprocessable_entity }
end
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 }
head :no_content
else
format.html { render :edit }
format.json { render json: @metacode.errors, status: :unprocessable_entity }
end
render json: @metacode.errors, status: :unprocessable_entity
end
end

View file

@ -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