-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsamples_controller.rb
163 lines (133 loc) · 4.34 KB
/
samples_controller.rb
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
# frozen_string_literal: true
module Projects
# Controller actions for Samples
class SamplesController < Projects::ApplicationController # rubocop:disable Metrics/ClassLength
include Metadata
include ListActions
include Storable
before_action :sample, only: %i[show edit update view_history_version]
before_action :current_page
before_action :query, only: %i[index search select]
def index
@timestamp = DateTime.current
@pagy, @samples = pagy_searchkick(@query.results(:searchkick_pagy), limit: params[:limit] || 20)
@has_samples = @project.samples.size.positive?
end
def search
redirect_to namespace_project_samples_path(request.request_parameters.slice(:limit, :page))
end
def show
authorize! @sample.project, to: :read_sample?
@tab = params[:tab]
if @tab == 'metadata'
@sample_metadata = @sample.metadata_with_provenance
elsif @tab == 'history'
@log_data = @sample.log_data_without_changes
else
@sample_attachments = @sample.attachments
end
end
def view_history_version
authorize! @sample.project, to: :view_history?
@log_data = @sample.log_data_with_changes(params[:version])
respond_to do |format|
format.turbo_stream do
render status: :ok
end
end
end
def new
authorize! @project, to: :create_sample?
@sample = Sample.new
end
def edit
authorize! @sample.project, to: :update_sample?
end
def create
@sample = ::Samples::CreateService.new(current_user, @project, sample_params).execute
if @sample.persisted?
flash[:success] = t('.success')
redirect_to namespace_project_sample_path(id: @sample.id)
else
render :new, status: :unprocessable_entity
end
end
def update
respond_to do |format|
if ::Samples::UpdateService.new(@sample, current_user, sample_params).execute
flash[:success] = t('.success')
format.html { redirect_to namespace_project_sample_path(id: @sample.id) }
else
format.html { render :edit, status: :unprocessable_entity }
end
end
end
def select
authorize! @project, to: :sample_listing?
@sample_ids = []
respond_to do |format|
format.turbo_stream do
if params[:select].present?
@sample_ids = @query.results(:searchkick)
.where(updated_at: ..params[:timestamp].to_datetime)
.select(:id).pluck(:id)
end
end
end
end
private
def sample
@sample = Sample.find_by(id: params[:id] || params[:sample_id], project_id: project.id) || not_found
end
def sample_params
params.require(:sample).permit(:name, :description)
end
def context_crumbs
super
@context_crumbs +=
[{
name: I18n.t('projects.samples.index.title'),
path: namespace_project_samples_path
}]
return unless action_name == 'show' && [email protected]?
@context_crumbs +=
[{
name: @sample.puid,
path: namespace_project_sample_path(id: @sample.id)
}]
end
def layout_fixed
super
return unless action_name == 'index'
@fixed = false
end
def current_page
@current_page = t(:'projects.sidebar.samples')
end
def set_metadata_fields
fields_for_namespace(
namespace: @project.namespace,
show_fields: @search_params && @search_params[:metadata].to_i == 1
)
end
def search_key
:"#{controller_name}_#{@project.id}_search_params"
end
def query
authorize! @project, to: :sample_listing?
@search_params = search_params
set_metadata_fields
@query = Sample::Query.new(@search_params.except(:metadata).merge({ project_ids: [@project.id] }))
end
def search_params
updated_params = update_store(search_key,
params[:q].present? ? params[:q].to_unsafe_h : {}).with_indifferent_access
if !updated_params.key?(:sort) ||
(updated_params[:metadata].to_i.zero? && updated_params[:sort]&.match?(/metadata_/))
updated_params[:sort] = 'updated_at desc'
update_store(search_key, updated_params)
end
updated_params
end
end
end