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 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
respond_to do |format|
if @metacode_set.save if @metacode_set.save
# create the InMetacodeSet for all the metacodes that were selected for the set # create the InMetacodeSet for all the metacodes that were selected for the set
@metacodes = params[:metacodes][:value].split(',') @metacodes = params[:metacodes][:value].split(',')
@metacodes.each do |m| @metacodes.each do |m|
InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id) InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id)
end end
format.json do render json: @metacode_set, status: :created
render json: @metacode_set, status: :created, location: metacode_sets_url
end
else else
format.json { render json: @metacode_set.errors, status: :unprocessable_entity } render json: @metacode_set.errors, status: :unprocessable_entity
end
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])
respond_to do |format|
if @metacode_set.update_attributes(metacode_set_params) if @metacode_set.update_attributes(metacode_set_params)
# build an array of the IDs of the metacodes currently in the set # 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) InMetacodeSet.create(metacode_id: m, metacode_set_id: @metacode_set.id)
end end
format.json { head :no_content } head :no_content
else else
format.json { render json: @metacode_set.errors, status: :unprocessable_entity } render json: @metacode_set.errors, status: :unprocessable_entity
end
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)
respond_to do |format|
if @metacode.save if @metacode.save
format.html { redirect_to metacodes_url, notice: 'Metacode was successfully created.' } render json: @metacode, status: :created
format.json { render json: @metacode, status: :created, location: metacodes_url }
else else
format.html { render :new } render json: @metacode.errors, status: :unprocessable_entity
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)
format.html { redirect_to metacodes_url, notice: 'Metacode was successfully updated.' } head :no_content
format.json { head :no_content }
else else
format.html { render :edit } render json: @metacode.errors, status: :unprocessable_entity
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