Class: CollectingEventsController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/collecting_events_controller.rb

Instance Method Summary collapse

Methods included from DataControllerConfiguration::ProjectDataControllerConfiguration

#annotator_params

Methods included from RedirectHelper

#destroy_redirect

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#invalid_object, #project_link, #project_matches, #project_tag, #projects_list, #projects_search_form

Methods included from Api::Intercept

#intercept_api

Methods included from TokenAuthentication

#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate

Instance Method Details

#api_autocompleteObject



252
253
254
255
256
# File 'app/controllers/collecting_events_controller.rb', line 252

def api_autocomplete
  render json: {} and return if params[:term].blank?
  @collecting_events = Queries::CollectingEvent::Autocomplete.new(params[:term], project_id: sessions_current_project_id).autocomplete
  render '/collecting_events/api/v1/autocomplete'
end

#api_indexObject



239
240
241
242
243
244
245
# File 'app/controllers/collecting_events_controller.rb', line 239

def api_index
  @collecting_events = Queries::CollectingEvent::Filter.new(api_params).all
    .where(project_id: sessions_current_project_id)
    .order('collecting_events.id')
    .page(params[:page]).per(params[:per])
  render 'collecting_events/api/v1/index'
end

#api_paramsObject (private)



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'app/controllers/collecting_events_controller.rb', line 330

def api_params
  params.permit(
    Queries::CollectingEvent::Filter::ATTRIBUTES,
    :collection_objects,
    :collector_id,
    :collector_ids_or,
    :end_date, # used in date range
    :geo_json,
    :geographic_area_id,
    :identifier,
    :identifier_end,
    :identifier_exact,
    :identifier_start,
    :identifiers,
    :in_labels,
    :in_verbatim_locality,
    :md5_verbatim_label,
    :partial_overlap_dates,
    :otu_id,
    :radius,
    :recent,
    :spatial_geographic_areas,
    :start_date, # used in date range
    :wkt,
    collector_id: [],
    geographic_area_id: [],
    keyword_id_and: [],
    keyword_id_or: [],
    otu_id: [],
  )
end

#api_showObject



247
248
249
250
# File 'app/controllers/collecting_events_controller.rb', line 247

def api_show
  @collecting_event = CollectingEvent.where(project_id: sessions_current_project_id).find(params[:id])
  render '/collecting_events/api/v1/show'
end

#attributesObject



105
106
107
108
109
110
# File 'app/controllers/collecting_events_controller.rb', line 105

def attributes
  render json: ::CollectingEvent.columns.select{
    |a| Queries::CollectingEvent::Filter::ATTRIBUTES.include?(
      a.name)
  }.collect{|b| {'name' => b.name, 'type' => b.type } }
end

#autocompleteObject



121
122
123
# File 'app/controllers/collecting_events_controller.rb', line 121

def autocomplete
  @collecting_events = Queries::CollectingEvent::Autocomplete.new(params[:term], project_id: sessions_current_project_id).autocomplete
end

#autocomplete_collecting_event_verbatim_localityObject

GET /collecting_events/autocomplete_collecting_event_verbatim_locality?term=asdf see rails-jquery-autocomplete



127
128
129
130
131
# File 'app/controllers/collecting_events_controller.rb', line 127

def autocomplete_collecting_event_verbatim_locality
  term = params[:term]
  values = CollectingEvent.where(project_id: sessions_current_project_id).where('verbatim_locality ILIKE ?', term + '%').select(:verbatim_locality, 'length(verbatim_locality)').distinct.limit(20).order('length(verbatim_locality)').order('verbatim_locality ASC').all
  render json: values.map { |v| { label: v.verbatim_locality, value: v.verbatim_locality} }
end

#batch_loadObject

GET collecting_events/batch_load



152
153
# File 'app/controllers/collecting_events_controller.rb', line 152

def batch_load
end

#batch_paramsObject (private)



287
288
289
290
291
292
293
294
295
# File 'app/controllers/collecting_events_controller.rb', line 287

def batch_params
  params.permit(
    :ce_namespace,
    :ce_geographic_area_id,
    :file,
    :import_level).merge(
      user_id: sessions_current_user_id,
      project_id: sessions_current_project_id).to_h.symbolize_keys
end

#cardObject



93
94
95
# File 'app/controllers/collecting_events_controller.rb', line 93

def card
  @target = params[:target]
end

#cloneObject

POST /collecting_events/1/clone.json



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/collecting_events_controller.rb', line 51

def clone
  @collecting_event = @collecting_event.clone
  respond_to do |format|
    if @collecting_event.persisted?
      format.html { redirect_to new_collecting_event_task_path(@collecting_event), notice: 'Clone successful, editing new record.' }
      format.json { render :show }
    else
      format.html { redirect_to new_collecting_event_task_path(@collecting_event), notice: 'Failed to clone the collecting event..' }
      format.json {render json: @collecting_event.errors, status: :unprocessable_entity}
    end
  end
end

#collecting_event_paramsObject (private)



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'app/controllers/collecting_events_controller.rb', line 268

def collecting_event_params
  params.require(:collecting_event).permit(
    :verbatim_label, :print_label, :print_label_number_to_print, :document_label,
    :verbatim_locality, :verbatim_date, :verbatim_longitude, :verbatim_latitude,
    :verbatim_geolocation_uncertainty, :verbatim_trip_identifier, :verbatim_collectors,
    :verbatim_method, :geographic_area_id, :minimum_elevation, :maximum_elevation,
    :elevation_precision, :time_start_hour, :time_start_minute, :time_start_second,
    :time_end_hour, :time_end_minute, :time_end_second, :start_date_day,
    :start_date_month, :start_date_year, :end_date_day, :end_date_month,
    :group, :member, :formation, :lithology, :max_ma, :min_ma,
    :end_date_year, :verbatim_habitat, :field_notes, :verbatim_datum,
    :verbatim_elevation,
    roles_attributes: [:id, :_destroy, :type, :person_id, :position,
                       person_attributes: [:last_name, :first_name, :suffix, :prefix]],
  identifiers_attributes: [:id, :namespace_id, :identifier, :type, :_destroy],
  data_attributes_attributes: [ :id, :_destroy, :controlled_vocabulary_term_id, :type, :attribute_subject_id, :attribute_subject_type, :value ]
  )
end

#createObject

POST /collecting_events POST /collecting_events.json



37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/collecting_events_controller.rb', line 37

def create
  @collecting_event = CollectingEvent.new(collecting_event_params)
  respond_to do |format|
    if @collecting_event.save
      format.html { redirect_to @collecting_event, notice: 'Collecting event was successfully created.' }
      format.json { render action: 'show', status: :created, location: @collecting_event }
    else
      format.html { render action: 'new' }
      format.json { render json: @collecting_event.errors, status: :unprocessable_entity }
    end
  end
end

#create_castor_batch_loadObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/collecting_events_controller.rb', line 192

def create_castor_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :Castor_collecting_events_md5)
    @result = BatchLoad::Import::CollectingEvents::CastorInterpreter.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} collecting events were created."
      render 'collecting_events/batch_load/castor/create' and return
    else
      flash[:alert] = 'Batch import failed.'
    end
  else
    flash[:alert] = 'File to batch upload must be supplied.'
  end
  render :batch_load
end

#create_gpx_batch_loadObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/controllers/collecting_events_controller.rb', line 219

def create_gpx_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :gpx_batch_load_collecting_events_md5)
    @result = BatchLoad::Import::CollectingEvents::GpxInterpreter.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} collecting events w/georeferences were created."
      render 'collecting_events/batch_load/gpx/create' and return
    else
      flash[:alert] = 'Batch import failed.'
    end
  else
    flash[:alert] = 'File to batch upload must be supplied.'
  end
  render :batch_load
end

#create_simple_batch_loadObject



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/controllers/collecting_events_controller.rb', line 166

def create_simple_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :batch_collecting_events_md5)
    @result = BatchLoad::Import::CollectingEvent.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} collecting events were created."
      render 'collecting_events/batch_load/simple/create' and return
    else
      flash[:alert] = 'Batch import failed.'
    end
  else
    flash[:alert] = 'File to batch upload must be supplied.'
  end
  render :batch_load
end

#destroyObject

DELETE /collecting_events/1 DELETE /collecting_events/1.json



80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/collecting_events_controller.rb', line 80

def destroy
  @collecting_event.destroy
  respond_to do |format|
    if @collecting_event.destroyed?
      format.html { destroy_redirect @collecting_event, notice: 'CollectingEvent was successfully destroyed.' }
      format.json { head :no_content }
    else
      format.html { destroy_redirect @collecting_event, notice: 'CollectingEvent was not destroyed: ' + @collecting_event.errors.full_messages.join('; ') }
      format.json { render json: @collecting_event.errors, status: :unprocessable_entity }
    end
  end
end

#downloadObject

GET /collecting_events/download



134
135
136
137
138
# File 'app/controllers/collecting_events_controller.rb', line 134

def download
  send_data(Export::Download.generate_csv(CollectingEvent.where(project_id: sessions_current_project_id)),
            type: 'text',
            filename: "collecting_events_#{DateTime.now}.csv")
end

#editObject

GET /collecting_events/1/edit



32
33
# File 'app/controllers/collecting_events_controller.rb', line 32

def edit
end

#filter_paramsObject (private)



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'app/controllers/collecting_events_controller.rb', line 297

def filter_params
  params.permit(
    Queries::CollectingEvent::Filter::ATTRIBUTES,
    :collection_objects,
    :collector_id,
    :collector_ids_or,
    :end_date,   # used in date range
    :geo_json,
    :geographic_area_id,
    :in_labels,
    :in_verbatim_locality,
    :identifier,
    :identifier_end,
    :identifier_exact,
    :identifier_start,
    :identifiers,
    :md5_verbatim_label,
    :otu_id,
    :partial_overlap_dates,
    :radius,
    :recent,
    :spatial_geographic_areas,
    :start_date, # used in date range
    :wkt,
    collecting_event_wildcards: [],
    collector_id: [],
    geographic_area_id: [],
    keyword_id_and: [],
    keyword_id_or: [],
    otu_id: [],
  )
end

#indexObject

GET /collecting_events GET /collecting_events.json



9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/collecting_events_controller.rb', line 9

def index
  respond_to do |format|
    format.html do
      @recent_objects = CollectingEvent.where(project_id: sessions_current_project_id).order(updated_at: :desc).limit(10)
      render '/shared/data/all/index'
    end
    format.json {
      @collecting_events = Queries::CollectingEvent::Filter.new(filter_params).all.where(project_id: sessions_current_project_id).page(params[:page]).per(params[:per] || 500)
    }
  end
end

#listObject



101
102
103
# File 'app/controllers/collecting_events_controller.rb', line 101

def list
  @collecting_events = CollectingEvent.where(project_id: sessions_current_project_id).order(:id).page(params[:page])
end


258
259
# File 'app/controllers/collecting_events_controller.rb', line 258

def navigation
end

#newObject

GET /collecting_events/new



27
28
29
# File 'app/controllers/collecting_events_controller.rb', line 27

def new
  @collecting_event = CollectingEvent.new
end

#parse_verbatim_labelObject

parse verbatim label, return date and coordinates



141
142
143
144
145
146
147
148
149
# File 'app/controllers/collecting_events_controller.rb', line 141

def parse_verbatim_label
  if params[:verbatim_label]
    render json: {date: Utilities::Dates.date_regex_from_verbatim_label(params[:verbatim_label]),
                  geo: Utilities::Geo.coordinates_regex_from_verbatim_label(params[:verbatim_label]),
                  elevation: Utilities::Elevation.elevation_regex_from_verbatim_label(params[:verbatim_label]),
                  collecting_method: Utilities::CollectingMethods.method_regex_from_verbatim_label(params[:verbatim_label]),
    }.to_json
  end
end

#preview_castor_batch_loadObject



181
182
183
184
185
186
187
188
189
190
# File 'app/controllers/collecting_events_controller.rb', line 181

def preview_castor_batch_load
  if params[:file]
    @result = BatchLoad::Import::CollectingEvents::CastorInterpreter.new(**batch_params)
    digest_cookie(params[:file].tempfile, :Castor_collecting_events_md5)
    render 'collecting_events/batch_load/castor/preview'
  else
    flash[:notice] = 'No file provided!'
    redirect_to action: :batch_load
  end
end

#preview_gpx_batch_loadObject



207
208
209
210
211
212
213
214
215
216
217
# File 'app/controllers/collecting_events_controller.rb', line 207

def preview_gpx_batch_load
  if params[:file]
    @result = BatchLoad::Import::CollectingEvents::GpxInterpreter.new(**batch_params)
    digest_cookie(params[:file].tempfile, :gpx_batch_load_collecting_events_md5)
    render 'collecting_events/batch_load/gpx/preview'
    # render '/shared/data/all/batch_load/preview'
  else
    flash[:notice] = "No file provided!"
    redirect_to action: :batch_load
  end
end

#preview_simple_batch_loadObject



155
156
157
158
159
160
161
162
163
164
# File 'app/controllers/collecting_events_controller.rb', line 155

def preview_simple_batch_load
  if params[:file]
    @result = BatchLoad::Import::CollectingEvents.new(**batch_params)
    digest_cookie(params[:file].tempfile, :batch_collecting_events_md5)
    render 'collecting_events/batch_load/simple/preview'
  else
    flash[:notice] = 'No file provided!'
    redirect_to action: :batch_load
  end
end

#searchObject

GET /collecting_events/search



113
114
115
116
117
118
119
# File 'app/controllers/collecting_events_controller.rb', line 113

def search
  if params[:id].blank?
    redirect_to collecting_event_path, alert: 'You must select an item from the list with a click or tab press before clicking show.'
  else
    redirect_to collecting_event_path(params[:id])
  end
end

#select_optionsObject

GET /collecting_events/select_options



235
236
237
# File 'app/controllers/collecting_events_controller.rb', line 235

def select_options
  @collecting_events = CollectingEvent.select_optimized(sessions_current_user_id, sessions_current_project_id)
end

#set_collecting_eventObject (private)



263
264
265
266
# File 'app/controllers/collecting_events_controller.rb', line 263

def set_collecting_event
  @collecting_event = CollectingEvent.where(project_id: sessions_current_project_id).find(params[:id])
  @recent_object = @collecting_event
end

#showObject

GET /collecting_events/1 GET /collecting_events/1.json



23
24
# File 'app/controllers/collecting_events_controller.rb', line 23

def show
end

#testObject



97
98
99
# File 'app/controllers/collecting_events_controller.rb', line 97

def test
  @geo = CollectingEvent.test
end

#updateObject

PATCH/PUT /collecting_events/1 PATCH/PUT /collecting_events/1.json



66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/collecting_events_controller.rb', line 66

def update
  respond_to do |format|
    if @collecting_event.update(collecting_event_params)
      format.html { redirect_to @collecting_event, notice: 'Collecting event was successfully updated.' }
      format.json { render :show, status: :ok, location: @collecting_event }
    else
      format.html { render action: 'edit' }
      format.json { render json: @collecting_event.errors, status: :unprocessable_entity }
    end
  end
end