omegahm/beerclub

AdministratorsController
A

Similar code found in 5 other locations

class AdministratorsController < ApplicationController
  before_filter :require_administrator
  before_action :set_administrator, only: [:edit, :update, :destroy]

  # GET /administrators
Found in AdministratorsController and 5 other locations - About 2 hrs to fix
app/controllers/administrator_sessions_controller.rb on lines 8..15
app/controllers/meta_controller.rb on lines 20..27
app/controllers/products_controller.rb on lines 21..28
app/controllers/settings_controller.rb on lines 19..26
app/controllers/users_controller.rb on lines 24..31

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Refactorings

Further Reading

There are no issues that match your filters.

class AdministratorsController < ApplicationController
  before_filter :require_administrator
  before_action :set_administrator, only: [:edit, :update, :destroy]

  # GET /administrators
  def index
    @administrators = Administrator.select(:id, :username)
  end

  # GET /administrators/new
  def new
    @administrator = Administrator.new
  end

  # GET /administrators/1/edit
  def edit
  end

  # POST /administrators
  def create
    @administrator = Administrator.new(administrator_params)

    if @administrator.save
      redirect_to administrators_path, notice: 'Administratoren blev oprettet.'
    else
      render action: 'new'
    end
  end

  # PATCH/PUT /administrators/1
  def update
    if @administrator.update(administrator_params)
      redirect_to administrators_path, notice: 'Administratoren blev opdateret.'
    else
      render action: 'edit'
    end
  end

  # DELETE /administrators/1
  def destroy
    @administrator.destroy
    redirect_to administrators_url
  end

  def error
    fail 'Error'
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_administrator
    @administrator = Administrator.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def administrator_params
    params.require(:administrator).permit(:username, :email, :password, :password_confirmation)
  end
end

Size

Lines of code
41

Join us for Code Climate
Summit on July 19 in NYC.

Tickets on sale now.