Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

How i can rewrite or refactoring my code in controller. I have the same sql query ( @plan_gp_users) in all def.

    class PlanGpsController < ApplicationController

     def index
      @search = PlanGp.joins(:user).search(params[:q])
      @plan_gps = @search.result.order("created_at DESC").page(params[:page]).per(15)
      @plan_gp_users = User.where("users.ab_id = :abs_id  AND users.id != :user_id AND is_admin IS NULL AND role != 'head'", {:abs_id => current_user.ab_id,:user_id =>current_user.id})

      respond_to do |format|
      format.js
      format.html # index.html.erb
      format.json { render json: @plan_gps }
      end

     end


    def show

     @plan_gp = PlanGp.find(params[:id])
     @plan_gp_users = User.where("users.ab_id = :abs_id  AND users.id != :user_id AND is_admin IS NULL AND role != 'head'", {:abs_id => current_user.ab_id,:user_id =>current_user.id})

    respond_to do |format|

      format.js

      format.html # show.html.erb

      format.json { render json: @plan_gp }

    end

  end



  # GET /plan_gps/new

  # GET /plan_gps/new.json

  def new



    @plan_gp = PlanGp.new

    # 3.times { render @plan_gp.user_id }

    # .joins("LEFT JOIN plan_gps ON plan_gps.user_id = users.id and strftime('%Y-%m','now') = strftime('%Y-%m',plan_gps.month)") AND plan_gps.user_id is null 



    @plan_gp_users = User.where("users.ab_id = :abs_id  AND users.id != :user_id AND is_admin IS NULL AND role != 'head'", {:abs_id => current_user.ab_id,:user_id =>current_user.id})

     # raise @plan_gp_users.to_sql

    respond_to do |format|

      format.js

      format.html # new.html.erb

      format.json { render json: @plan_gp }

    end

  end



  # GET /plan_gps/1/edit

  def edit

    @plan_gp = PlanGp.find(params[:id])

    @plan_gp_users = User.where("users.ab_id = :abs_id  AND users.id != :user_id AND is_admin IS NULL AND role != 'head'", {:abs_id => current_user.ab_id,:user_id =>current_user.id})

  end



  # POST /plan_gps

  # POST /plan_gps.json

  def create

    @plan_gp = PlanGp.new(params[:plan_gp])

    @plan_gp_users = User.where("users.ab_id = :abs_id  AND users.id != :user_id AND is_admin IS NULL AND role != 'head'", {:abs_id => current_user.ab_id,:user_id =>current_user.id})



      User.where("id IN (:user_ids) AND role != :role", {:user_ids => params[:plan_gp]["user_id"],:role =>'head'}).select("id").map do|m| 

        @plan_gp = PlanGp.new(params[:plan_gp])

        @plan_gp.user_id = m.id

        @plan_gp.abs_id = current_user.ab_id

        if @plan_gp.save 

          @plan_gp_save = true

        else 

          @plan_gp_save = false

        end

      end







    @plan_gp.abs_id = current_user.ab_id



    respond_to do |format|

      if @plan_gp_save

        format.js

        format.html { redirect_to plan_gps_url }

        format.json { render json: @plan_gp, status: :created, location: @plan_gp } 

      else

        format.js

        format.html { redirect_to plan_gps_url }

        format.json { render json: @plan_gp.errors, status: :unprocessable_entity }      

      end

    end

  end



  # PUT /plan_gps/1

  # PUT /plan_gps/1.json

  def update

    @plan_gp = PlanGp.find(params[:id])

    @plan_gp_users = User.where("users.ab_id = :abs_id  AND users.id != :user_id AND is_admin IS NULL AND role != 'head'", {:abs_id => current_user.ab_id,:user_id =>current_user.id})







    respond_to do |format|

      if @plan_gp.update_attributes(params[:plan_gp])

        format.js

        format.html { redirect_to @plan_gp, notice: 'Plan gp was successfully updated.' }

        format.json { head :no_content }

      else

        format.js

        format.html { render action: "edit" }

        format.json { render json: @plan_gp.errors, status: :unprocessable_entity }

      end

    end

  end



  # DELETE /plan_gps/1

  # DELETE /plan_gps/1.json

  def destroy

    @plan_gp = PlanGp.find(params[:id])

    @plan_gp.destroy



    respond_to do |format|

      format.js

      format.html { redirect_to plan_gps_url }

      format.json { head :no_content }

    end

  end

end
share|improve this question
add comment (requires an account with 50 reputation)

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.