aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/admin_holidays_controller.rb
blob: 9177ebd44c1e23793546768fcfda3e8fe735c830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class AdminHolidaysController < AdminController

    def index
        get_all_holidays
    end

    def new
        @holiday = Holiday.new
        if request.xhr?
            render :partial => 'new_form', :locals => { :holiday => @holiday }
        else
            render :action => 'new'
        end
    end

    def create
        @holiday = Holiday.new(holiday_params)
        if @holiday.save
            notice = "Holiday successfully created."
            redirect_to admin_holidays_path, :notice => notice
        else
            render :new
        end
    end

    def edit
        @holiday = Holiday.find(params[:id])
        if request.xhr?
            render :partial => 'edit_form'
        else
            render :action => 'edit'
        end
    end

    def update
        @holiday = Holiday.find(params[:id])
        if @holiday.update_attributes(holiday_params)
            flash[:notice] = 'Holiday successfully updated.'
            redirect_to admin_holidays_path
        else
            render :edit
        end
    end

    def destroy
        @holiday = Holiday.find(params[:id])
        @holiday.destroy
        notice = "Holiday successfully destroyed"
        redirect_to admin_holidays_path, :notice => notice
    end

    private

    def get_all_holidays
        @holidays_by_year = Holiday.all.group_by { |holiday| holiday.day.year }
        @years = @holidays_by_year.keys.sort.reverse
    end

    def holiday_params(key = :holiday)
        if params[key]
            params[key].slice(:description, 'day(1i)', 'day(2i)', 'day(3i)')
        else
            {}
        end
    end

end