| 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
 | class CalendarsController < ChouetteController
  include PolicyChecker
  include TimeTablesHelper
  defaults resource_class: Calendar
  before_action :ransack_contains_date, only: [:index]
  respond_to :html
  respond_to :json, only: :show
  respond_to :js, only: :index
  belongs_to :workgroup
  def index
    index! do
      @calendars = decorate_calendars(@calendars)
    end
  end
  def show
    show! do
      @year = params[:year] ? params[:year].to_i : Date.today.cwyear
      @calendar = @calendar.decorate(context: {
        workgroup: workgroup
      })
    end
  end
  def month
    @date = params['date'] ? Date.parse(params['date']) : Date.today
    @calendar = resource
  end
  def create
    create! do |success, failure|
      if has_feature?('application_days_on_calendars')
        success.html do
          redirect_to([:edit, @workgroup, @calendar])
        end
      end
    end
  end
  def update
    if params[:calendar]
      super
    else
      state  = JSON.parse request.raw_post
      resource.state_update state
      respond_to do |format|
        format.json { render json: state, status: state['errors'] ? :unprocessable_entity : :ok }
      end
    end
  end
  private
  def decorate_calendars(calendars)
    CalendarDecorator.decorate(
      calendars,
      context: {
        workgroup: workgroup
      }
    )
  end
  def calendar_params
    permitted_params = [:id, :name, :short_name, :shared, periods_attributes: [:id, :begin, :end, :_destroy], date_values_attributes: [:id, :value, :_destroy]]
    permitted_params << :shared if policy(Calendar).share?
    params.require(:calendar).permit(*permitted_params)
  end
  def sort_column
    Calendar.column_names.include?(params[:sort]) ? params[:sort] : 'short_name'
  end
  def sort_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'
  end
  protected
  alias_method :workgroup, :parent
  helper_method :workgroup
  def resource
    @calendar ||= workgroup.calendars.where('(organisation_id = ? OR shared = ?)', current_organisation.id, true).find_by_id(params[:id])
  end
  def build_resource
    super.tap do |calendar|
      calendar.workgroup = workgroup
      calendar.organisation = current_organisation
    end
  end
  def collection
    @calendars ||= begin
      scope = workgroup.calendars.where('(organisation_id = ? OR shared = ?)', current_organisation.id, true)
      scope = shared_scope(scope)
      @q = scope.ransack(params[:q])
      calendars = @q.result
      calendars = calendars.order(sort_column + ' ' + sort_direction) if sort_column && sort_direction
      calendars = calendars.paginate(page: params[:page])
    end
  end
  def ransack_contains_date
    date =[]
    if params[:q] && !params[:q]['contains_date(1i)'].empty?
      ['contains_date(1i)', 'contains_date(2i)', 'contains_date(3i)'].each do |key|
        date << params[:q][key].to_i
        params[:q].delete(key)
      end
      params[:q]['contains_date'] = Date.new(*date) rescue nil
    end
  end
  def shared_scope scope
    return scope unless params[:q]
    if params[:q][:shared_true] == params[:q][:shared_false]
      params[:q].delete(:shared_true)
      params[:q].delete(:shared_false)
    end
    scope
  end
end
 |