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
|
class WorkbenchesController < BreadcrumbController
before_action :query_params, only: [:show]
defaults resource_class: Workbench
respond_to :html, only: [:show]
def show
scope = resource.all_referentials
scope = ransack_associated_lines(scope)
scope = ransack_periode(scope)
scope = ransack_status(scope)
# Ignore archived_at_not_null/archived_at_null managed by ransack_status scope
# We clone params[:q] so we can delete fake ransack filter arguments before calling search method,
# which will allow us to preserve params[:q] for sorting
ransack_params = params[:q].merge(archived_at_not_null: nil, archived_at_null: nil).clone
ransack_params.delete('associated_lines_id_eq')
@q = scope.ransack(ransack_params)
@wbench_refs = sort_result(@q.result).paginate(page: params[:page], per_page: 30)
@wbench_refs = ModelDecorator.decorate(
@wbench_refs,
with: ReferentialDecorator
)
show! do
build_breadcrumb :show
end
end
def delete_referentials
referentials = resource.referentials.where(id: params[:referentials])
referentials.each do |referential|
ReferentialDestroyWorker.perform_async(referential.id)
referential.update_attribute(:ready, false)
end
flash[:notice] = t('notice.referentials.deleted')
redirect_to resource
end
private
def resource
@workbench = Workbench.find params[:id]
end
def sort_result collection
col = (Workbench.find(params[:id]).referentials.column_names + %w{lines validity_period}).include?(params[:sort]) ? params[:sort] : 'name'
dir = %w[asc desc].include?(params[:direction]) ? params[:direction] : 'asc'
if ['lines', 'validity_period'].include?(col)
collection.send("order_by_#{col}", dir)
else
collection.order("#{col} #{dir}")
end
end
def query_params
if params[:q].present?
params[:q].delete_if { |query, value| value.blank? }
else
params[:q] = { "archived_at_not_null"=>"1", "archived_at_null"=>"1" }
end
end
# Fake ransack filter
def ransack_associated_lines scope
if params[:q] && params[:q]['associated_lines_id_eq']
scope = scope.include_metadatas_lines([params[:q]['associated_lines_id_eq']])
end
scope
end
# Fake ransack filter
def ransack_periode scope
return scope unless params[:q] && params[:q]['validity_period']
periode = params[:q]['validity_period']
return scope if periode['end_lteq(1i)'].blank? || periode['begin_gteq(1i)'].blank?
begin_range = Date.civil(periode["begin_gteq(1i)"].to_i, periode["begin_gteq(2i)"].to_i, periode["begin_gteq(3i)"].to_i)
end_range = Date.civil(periode["end_lteq(1i)"].to_i, periode["end_lteq(2i)"].to_i, periode["end_lteq(3i)"].to_i)
if begin_range > end_range
flash.now[:error] = t('referentials.errors.validity_period')
else
scope = scope.in_periode(begin_range..end_range)
@begin_range = begin_range
@end_range = end_range
end
scope
end
# Fake (again) ransack filter
def ransack_status scope
return scope unless params[:q]
archived_at_not_null = params[:q]['archived_at_not_null'] == '1'
archived_at_null = params[:q]['archived_at_null'] == '1'
if !archived_at_not_null and !archived_at_null
return scope.none
end
if archived_at_not_null and archived_at_null
return scope
end
if archived_at_null
return scope.where(archived_at: nil)
end
if archived_at_not_null
return scope.where("archived_at is not null")
end
scope
end
end
|