fiddle with metacodes controller

This commit is contained in:
Devin Howard 2016-09-24 11:00:53 +08:00
parent 0ace202ace
commit 5fab6de48a
2 changed files with 15 additions and 9 deletions

View file

@ -12,3 +12,9 @@ Rails:
Metrics/LineLength: Metrics/LineLength:
Max: 100 Max: 100
Metrics/AbcSize:
Max: 16
Style/Documentation:
Enabled: false

View file

@ -1,5 +1,7 @@
# frozen_string_literal: true
class MetacodesController < ApplicationController class MetacodesController < ApplicationController
before_action :require_admin, except: [:index, :show] before_action :require_admin, except: [:index, :show]
before_action :set_metacode, only: [:edit, :update]
# GET /metacodes # GET /metacodes
# GET /metacodes.json # GET /metacodes.json
@ -8,10 +10,7 @@ class MetacodesController < ApplicationController
respond_to do |format| respond_to do |format|
format.html do format.html do
unless authenticated? && user.admin return unless require_admin
redirect_to root_url, notice: 'You need to be an admin for that.'
return false
end
render :index render :index
end end
format.json { render json: @metacodes } format.json { render json: @metacodes }
@ -23,7 +22,7 @@ class MetacodesController < ApplicationController
# GET /metacodes/action.json # GET /metacodes/action.json
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]
@metacode = Metacode.find(params[:id]) unless @metacode set_metacode unless @metacode
respond_to do |format| respond_to do |format|
format.json { render json: @metacode } format.json { render json: @metacode }
@ -36,14 +35,13 @@ class MetacodesController < ApplicationController
@metacode = Metacode.new @metacode = Metacode.new
respond_to do |format| respond_to do |format|
format.html # new.html.erb format.html
format.json { render json: @metacode } format.json { render json: @metacode }
end end
end end
# GET /metacodes/1/edit # GET /metacodes/1/edit
def edit def edit
@metacode = Metacode.find(params[:id])
end end
# POST /metacodes # POST /metacodes
@ -65,8 +63,6 @@ class MetacodesController < ApplicationController
# PUT /metacodes/1 # PUT /metacodes/1
# PUT /metacodes/1.json # PUT /metacodes/1.json
def update def update
@metacode = Metacode.find(params[:id])
respond_to do |format| 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.' } format.html { redirect_to metacodes_url, notice: 'Metacode was successfully updated.' }
@ -84,4 +80,8 @@ class MetacodesController < ApplicationController
def metacode_params def metacode_params
params.require(:metacode).permit(:id, :name, :aws_icon, :manual_icon, :color) params.require(:metacode).permit(:id, :name, :aws_icon, :manual_icon, :color)
end end
def set_metacode
@metacode = Metacode.find(params[:id])
end
end end