YaleSTC/shifts

View on GitHub
app/controllers/stickies_controller.rb

Summary

Maintainability
A
0 mins
Test Coverage
class StickiesController < NoticesController
  layout 'shifts'

  def index
    flash.keep
        redirect_to(notices_path)
  end

  def show
    flash.keep
    redirect_to(notices_path)
  end

  def new
    @sticky = Sticky.new
        @current_shift_location = current_user.current_shift.location if current_user.current_shift
        @display = params[:on_report_view] == "true" ? "none" : "inline"
    layout_check
  end

  def create
    @sticky = Sticky.new(params[:sticky])
        set_author_dept_and_times
        current_user.current_shift ? @in_shift = true : @in_shift = false
        if @in_shift
          @current_shift = current_user.current_shift
        end
    begin
      Sticky.transaction do
        @sticky.save(validate: false)
        set_sources(@sticky)
        @sticky.save!
        end
        rescue Exception
      respond_to do |format|
        format.html { render action: "new" }
        format.js  #create.js.erb
      end
    else
      flash[:notice] = 'Sticky was successfully created.'
      respond_to do |format|
        format.html { redirect_to stickies_path }
        format.js  #create.js.erb
      end
    end
  end

    def update
    @sticky = Sticky.where(id: params[:id]).first || Sticky.new
    @sticky.update_attributes(params[:sticky])
        set_author_dept_and_times
    begin
      Sticky.transaction do
        @sticky.save(validate: false)
        set_sources(@sticky)
        @sticky.save!
      end
    rescue Exception
        respond_to do |format|
          format.html { render action: "new" }
        end
      else
        respond_to do |format|
        format.html {
          flash[:notice] = 'Sticky was successfully created.'
          redirect_to action: "index"
        }
      end
    end
  end

    private
    def set_author_dept_and_times
        @sticky.author = current_user
        @sticky.department = current_department
        @sticky.start = Time.now
        if params[:end_time_choice] == "indefinite"
            @sticky.end = nil
            @sticky.indefinite = true
        elsif params[:sticky][:end] == "day"
            @sticky.end = 1.day.from_now
        elsif params[:sticky][:end] == "week"
            @sticky.end = 1.week.from_now
        elsif params[:sticky][:end] == "month"
            @sticky.end = 1.month.from_now
        end
    end
end