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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
class TimeTablesController < ChouetteController
include TimeTablesHelper
include RansackDateFilter
before_action only: [:index] { set_date_time_params("bounding_dates", Date) }
defaults :resource_class => Chouette::TimeTable
respond_to :html
respond_to :xml
respond_to :json
respond_to :js, :only => :index
belongs_to :referential
include PolicyChecker
def show
@year = params[:year] ? params[:year].to_i : Date.today.cwyear
@time_table_combination = TimeTableCombination.new
show! do
@time_table = @time_table.decorate(context: {
referential: @referential
})
end
end
def month
@date = params['date'] ? Date.parse(params['date']) : Date.today
@time_table = resource
end
def new
@autocomplete_items = ActsAsTaggableOn::Tag.all
new!
end
def create
tt_params = time_table_params
if tt_params[:calendar_id] && tt_params[:calendar_id] != ""
%i(monday tuesday wednesday thursday friday saturday sunday).map { |d| tt_params[d] = true }
calendar = Calendar.find(tt_params[:calendar_id])
tt_params[:calendar_id] = nil if tt_params.has_key?(:dates_attributes) || tt_params.has_key?(:periods_attributes)
end
created_from = duplicate_source
@time_table = created_from ? created_from.duplicate : Chouette::TimeTable.new(tt_params)
if calendar
calendar.dates.each_with_index do |date, i|
@time_table.dates << Chouette::TimeTableDate.new(date: date, position: i, in_out: true)
end
calendar.periods.each_with_index do |period, i|
@time_table.periods << Chouette::TimeTablePeriod.new(period_start: period.begin, period_end: period.end, position: i)
end
end
create! do |success, failure|
success.html do
path = @time_table.created_from ? 'referential_time_table_path' : 'edit_referential_time_table_path'
redirect_to send(path, @referential, @time_table)
end
failure.html { render :new }
end
end
def edit
edit! do
@autocomplete_items = ActsAsTaggableOn::Tag.all
end
end
def update
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
def index
request.format.kml? ? @per_page = nil : @per_page = 12
index! do |format|
format.html {
if collection.out_of_bounds?
redirect_to params.merge(:page => 1)
end
@time_tables = decorate_time_tables(@time_tables)
}
format.js {
@time_tables = decorate_time_tables(@time_tables)
}
end
end
def duplicate
@time_table = Chouette::TimeTable.find params[:id]
@time_table = @time_table.duplicate
render :new
end
def actualize
@time_table = resource
if @time_table.calendar
@time_table.actualize
flash[:notice] = t('.success')
end
redirect_to referential_time_table_path @referential, @time_table
end
def tags
# @tags = ActsAsTaggableOn::Tag.where("tags.name = ?", "%#{params[:tag]}%")
@tags = Chouette::TimeTable.tags_on(:tags)
respond_to do |format|
format.json { render :json => @tags.map{|t| {:id => t.id, :name => t.name }} }
end
end
protected
def collection
scope = select_time_tables
if params[:q] && params[:q]["tag_search"]
tags = params[:q]["tag_search"].reject {|c| c.empty?}
scope = select_time_tables.tagged_with(tags, :any => true) if tags.any?
end
scope = self.ransack_period_range(scope: scope, error_message: t('referentials.errors.validity_period'), query: :overlapping)
@q = scope.search(params[:q])
if sort_column && sort_direction
@time_tables ||= @q.result(:distinct => true).order("#{sort_column} #{sort_direction}")
else
@time_tables ||= @q.result(:distinct => true).order(:comment)
end
@time_tables = @time_tables.paginate(page: params[:page], per_page: 10)
end
def select_time_tables
if params[:route_id]
referential.time_tables.joins(vehicle_journeys: :route).where( "routes.id IN (#{params[:route_id]})")
else
referential.time_tables
end
end
def resource_url(time_table = nil)
referential_time_table_path(referential, time_table || resource)
end
def collection_url
referential_time_tables_path(referential)
end
private
def sort_column
referential.time_tables.column_names.include?(params[:sort]) ? params[:sort] : 'comment'
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc'
end
def duplicate_source
from_id = time_table_params['created_from_id']
Chouette::TimeTable.find(from_id) if from_id
end
def decorate_time_tables(time_tables)
ModelDecorator.decorate(
time_tables,
with: TimeTableDecorator,
context: {
referential: @referential
}
)
end
def time_table_params
params.require(:time_table).permit(
:objectid,
:object_version,
:creator_id,
:calendar_id,
:version, :comment, :color,
:int_day_types,
:monday,
:tuesday,
:wednesday,
:thursday,
:friday,
:saturday,
:sunday,
:start_date,
:end_date,
:created_from_id,
{ :dates_attributes => [:date, :in_out, :id, :_destroy] },
{ :periods_attributes => [:period_start, :period_end, :_destroy, :id] },
{tag_list: []},
:tag_search
)
end
end
|