Class: CollectionObjectsController

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

Constant Summary

Constants included from ProjectsHelper

ProjectsHelper::CLASSIFIER, ProjectsHelper::CLASSIFIER_ANNOTATION

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

#cumulative_gb_per_year, #document_cumulative_gb_per_year, #document_gb_per_year, #gb_per_year, #image_cumulative_gb_per_year, #image_gb_per_year, #invalid_object, #project_classification, #project_link, #project_matches, #project_tag, #projects_list, #projects_search_form, #taxonworks_classification, #week_in_review_graphs

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

#add_includes_to_filter_result(collection_objects) ⇒ Object (private)

An experiment to balance query/rendering times vs. extend[] requests Likely suggests we need some fundamental changes.

Parameters:

  • CollectionObject::Filter.new()

    instance



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'app/controllers/collection_objects_controller.rb', line 491

def add_includes_to_filter_result(collection_objects)
  a = %i(identifiers dwc_occurrence repository current_repository)

  x = []
  a.each do |e|
    if helpers.extend_response_with(e.to_s)
      x.push e
    end
  end

  if x.any?
    collection_objects = collection_objects.includes(*x)
  end

  if helpers.extend_response_with('collecting_event')
    collection_objects = collection_objects.includes(collecting_event: [:identifiers])
  end

  if helpers.extend_response_with('taxon_determinations')
    collection_objects = collection_objects.includes(taxon_determinations: [:otu, roles: [:person]])
  end

  collection_objects
end

#after_destroy_pathObject (private)



415
416
417
418
419
420
421
422
423
424
425
# File 'app/controllers/collection_objects_controller.rb', line 415

def after_destroy_path
  if request.referer =~ /tasks\/collection_objects\/browse/
    if o = @collection_object.next_by_identifier
      browse_collection_objects_path(collection_object_id: o.id)
    else
      browse_collection_objects_path
    end
  else
    collection_objects_path
  end
end

#api_autocompleteObject



378
379
380
381
382
# File 'app/controllers/collection_objects_controller.rb', line 378

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

#api_dwcObject

GET /api/v1/collection_objects/123/dwc



385
386
387
388
389
390
# File 'app/controllers/collection_objects_controller.rb', line 385

def api_dwc
  ActiveRecord::Base.connection_pool.with_connection do
    @collection_object.get_dwc_occurrence
    render json: @collection_object.dwc_occurrence_attributes
  end
end

#api_indexObject

GET /api/v1/collection_objects



365
366
367
368
369
370
371
# File 'app/controllers/collection_objects_controller.rb', line 365

def api_index
  @collection_objects = ::Queries::CollectionObject::Filter.new(params.merge!(api: true)).all
    .where(project_id: sessions_current_project_id)
    .order('collection_objects.id')
    .page(params[:page]).per(params[:per])
  render '/collection_objects/api/v1/index'
end

#api_showObject

GET /api/v1/collection_objects/:id



374
375
376
# File 'app/controllers/collection_objects_controller.rb', line 374

def api_show
  render '/collection_objects/api/v1/show'
end

#autocompleteObject



356
357
358
359
360
361
362
# File 'app/controllers/collection_objects_controller.rb', line 356

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

#batch_loadObject

GET collection_objects/batch_load



265
266
# File 'app/controllers/collection_objects_controller.rb', line 265

def batch_load
end

#batch_paramsObject (private)



469
470
471
472
# File 'app/controllers/collection_objects_controller.rb', line 469

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

#batch_updateObject

PATCH /collection_object/batch_update.json?collection_object_query=<>&collection_object={}



402
403
404
405
406
407
408
409
410
411
# File 'app/controllers/collection_objects_controller.rb', line 402

def batch_update
  if c = CollectionObject.batch_update(
      preview: params[:preview],
      collection_object: collection_object_params.merge(by: sessions_current_user_id),
      collection_object_query: params[:collection_object_query])
    render json: c.to_json, status: :ok
  else
    render json: {}, status: :unprocessable_entity
  end
end

#batch_update_dwc_occurrenceObject

PATCH /collection_object/batch_update_dwc_occurrence.json?<collection object query params>



393
394
395
396
397
398
399
# File 'app/controllers/collection_objects_controller.rb', line 393

def batch_update_dwc_occurrence
  if c = CollectionObject.batch_update_dwc_occurrence(params)
    render json: c.to_json, status: :ok
  else
    render json: {}, status: :unprocessable_entity
  end
end

#biocuration_classificationsObject



72
73
74
75
# File 'app/controllers/collection_objects_controller.rb', line 72

def biocuration_classifications
  @biocuration_classifications = @collection_object.biocuration_classifications
  render '/biocuration_classifications/index'
end

#by_identifierObject

GET /collection_objects/by_identifier/ABCD TODO: remove for filter

Raises:

  • (ActiveRecord::RecordNotFound)


180
181
182
183
184
185
186
# File 'app/controllers/collection_objects_controller.rb', line 180

def by_identifier
  @identifier = params.require(:identifier)
  @request_project_id = sessions_current_project_id
  @collection_objects = CollectionObject.with_identifier(@identifier).where(project_id: @request_project_id).all

  raise ActiveRecord::RecordNotFound if @collection_objects.empty?
end

#collection_object_paramsObject (private)



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'app/controllers/collection_objects_controller.rb', line 432

def collection_object_params
  params.require(:collection_object).permit(
    :total, :preparation_type_id, :repository_id, :current_repository_id,
    :ranged_lot_category_id, :collecting_event_id,
    :buffered_collecting_event, :buffered_determinations,
    :buffered_other_labels, :accessioned_at, :deaccessioned_at, :deaccession_reason,
    :contained_in,
    :taxon_determination_id,
    collecting_event_attributes: [],  # needs to be filled out!
    data_attributes_attributes: [ :id, :_destroy, :controlled_vocabulary_term_id, :type, :value ],
    tags_attributes: [:id, :_destroy, :keyword_id],
    depictions_attributes: [:id, :_destroy, :svg_clip, :svg_view_box, :position, :caption, :figure_label, :image_id],
    identifiers_attributes: [
      :id,
      :_destroy,
      :identifier,
      :namespace_id,
      :type,
      labels_attributes: [
        :text,
        :type,
        :text_method,
        :total
      ]
    ],
    taxon_determinations_attributes: [
      :id, :_destroy, :otu_id, :year_made, :month_made, :day_made, :position,
      roles_attributes: [:id, :_destroy, :type, :organization_id, :person_id, :position, person_attributes: [:last_name, :first_name, :suffix, :prefix]],
      otu_attributes: [:id, :_destroy, :name, :taxon_name_id]
    ],
    biocuration_classifications_attributes: [
      :id, :_destroy, :biocuration_class_id
    ]

  )
end

#containerizeObject



296
297
298
# File 'app/controllers/collection_objects_controller.rb', line 296

def containerize
  @container_item = ContainerItem.new(contained_object: @collection_object)
end

#createObject

POST /collection_objects POST /collection_objects.json



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/controllers/collection_objects_controller.rb', line 199

def create
  @collection_object = CollectionObject.new(collection_object_params)

  respond_to do |format|
    if @collection_object.save
      format.html { redirect_to url_for(@collection_object.metamorphosize), notice: 'Collection object was successfully created.' }
      format.json { render action: 'show', status: :created, location: @collection_object.metamorphosize }
    else
      format.html { render action: 'new' }
      format.json { render json: @collection_object.errors, status: :unprocessable_entity }
    end
  end
end

#create_buffered_batch_loadObject



337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'app/controllers/collection_objects_controller.rb', line 337

def create_buffered_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :Buffered_collection_objects_md5)
    @result = BatchLoad::Import::CollectionObjects::BufferedInterpreter.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} items were created."
      render 'collection_objects/batch_load/buffered/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_castor_batch_loadObject



311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'app/controllers/collection_objects_controller.rb', line 311

def create_castor_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :Castor_collection_objects_md5)
    @result = BatchLoad::Import::CollectionObjects::CastorInterpreter.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} items were created."
      render 'collection_objects/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_simple_batch_loadObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/controllers/collection_objects_controller.rb', line 279

def create_simple_batch_load
  if params[:file] && digested_cookie_exists?(
      params[:file].tempfile,
      :batch_collection_objects_md5)
    @result = BatchLoad::Import::CollectionObjects.new(**batch_params.merge(user_map))
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} collection object-related object-sets were created."
      render 'collection_objects/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

#depictionsObject

GET /collection_objects/depictions/1 GET /collection_objects/depictions/1.html This is



147
148
# File 'app/controllers/collection_objects_controller.rb', line 147

def depictions
end

#destroyObject

DELETE /collection_objects/1 DELETE /collection_objects/1.json



230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/controllers/collection_objects_controller.rb', line 230

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

#downloadObject

GET /collection_objects/download



260
261
262
# File 'app/controllers/collection_objects_controller.rb', line 260

def download
  send_data Export::CSV.generate_csv(CollectionObject.where(project_id: sessions_current_project_id), header_converters: []), type: 'text', filename: "collection_objects_#{DateTime.now}.tsv"
end

#dwcObject

GET /collection_objects/123/dwc



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/collection_objects_controller.rb', line 100

def dwc
  o = nil
  ActiveRecord::Base.connection_pool.with_connection do
    o = CollectionObject.find(params[:id])
    if params[:rebuild] == 'true'
      # get does not rebuild, but does set if it doesn't exist
      o.set_dwc_occurrence
    else
      o.get_dwc_occurrence
    end

    # Default to *exclude* some fields that include large text, like geospatial
    mode = params[:mode] || :view
    render json: o.dwc_occurrence_attribute_values(mode)
  end
end

#dwc_indexObject

Render DWC fields only



88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/collection_objects_controller.rb', line 88

def dwc_index
  objects = ::Queries::CollectionObject::Filter.new(params).all.order('collection_objects.id').includes(:dwc_occurrence).page(params[:page]).per(params[:per]).all
  assign_pagination(objects)

  # Default to *exclude* some big fields, like geo-spatial wkt
  mode = params[:mode] || :view
  @objects = objects.pluck(*::CollectionObject.dwc_attribute_vector(mode))
  @headers = ::CollectionObject.dwc_attribute_vector_names(mode)
  render '/dwc_occurrences/dwc_index'
end

#dwc_verboseObject

GET /collection_objects/123/dwc_verbose



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/collection_objects_controller.rb', line 118

def dwc_verbose
  o = nil
  ActiveRecord::Base.connection_pool.with_connection do
    o = CollectionObject.find(params[:id])

    if params[:rebuild] == 'true'
      # get does not rebuild
      o.set_dwc_occurrence
    else
      o.get_dwc_occurrence
    end
  end
  render json: o.dwc_occurrence_attributes
end

#dwcaObject

DEPRECATED GET /collection_objects/dwca/123 # SHOULD BE dwc



82
83
84
85
# File 'app/controllers/collection_objects_controller.rb', line 82

def dwca
  @dwc_occurrence = CollectionObject.includes(:dwc_occurrence).find(params[:id]).get_dwc_occurrence # find or compute for
  render json: @dwc_occurrence.to_json
end

#editObject

GET /collection_objects/1/edit



194
195
# File 'app/controllers/collection_objects_controller.rb', line 194

def edit
end

#geo_jsonObject

TODO: render in view GET /collection_objects/1/geo_json GET /collection_objects/1/geo_json.json



173
174
175
176
# File 'app/controllers/collection_objects_controller.rb', line 173

def geo_json
  ce = @collection_object.collecting_event
  @geo_json = ce.nil? ? nil : ce.to_geo_json_feature
end

#imagesObject

GET /collection_objects/1/inventory/images.html GET /collection_objects/1/images.json



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/collection_objects_controller.rb', line 155

def images
  @images = ::Queries::Image::Filter.new(
    collection_object_id: [ params.require(:id)],
    collection_object_scope: [:all]
  )

  respond_to do |format|
    format.html { @images = @images.all }
    format.json do  # rendered as Depictions for now
      @depictions = @iamges.derived_depictions
      render '/depictions/index' and return
    end
  end
end

#indexObject

GET /collecting_events GET /collecting_events.json



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/collection_objects_controller.rb', line 13

def index
  respond_to do |format|
    format.html do
      @recent_objects = CollectionObject.recent_from_project_id(sessions_current_project_id)
        .order(updated_at: :desc)
        .includes(:identifiers, :taxon_determinations)
        .limit(10)
      render '/shared/data/all/index'
    end
    format.json do
      collection_objects = ::Queries::CollectionObject::Filter.new(params).all

      @collection_objects = add_includes_to_filter_result(collection_objects)

      @collection_objects = @collection_objects
        .page(params[:page])
        .per(params[:per])
    end
  end
end

#index_metadataObject

/collection_objects/index_metadata/.json



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

def 
  render json: ( {
    repository: Repository,
    current_respository: Repository,
    collecting_event: CollectingEvent,
    taxon_determinations: TaxonDetermination })
    .merge( dwc_occurrence:  DwcOccurrence.target_columns.inject({}){|hsh,p| hsh[p] = nil; hsh}.delete_if{|k,v| k =~ /(_id|_type)\z/} )
    .merge( CollectionObject.core_attributes.inject({}){|hsh,p| hsh[p] = nil; hsh})
    .merge(
      identifiers: nil,
      object_tag: nil,
      object_label: nil,
    ).delete_if{|k,v| k =~ /(_id|_type)\z/}
end

#listObject

GET /collection_objects/list



244
245
246
247
248
# File 'app/controllers/collection_objects_controller.rb', line 244

def list
  @collection_objects = CollectionObject.with_project_id(sessions_current_project_id)
    .order(:id)
    .page(params[:page]) #.per(10) #.per(3)
end

#metadata_badgeObject



150
151
# File 'app/controllers/collection_objects_controller.rb', line 150

def 
end

#metadata_index(models = {}) ⇒ Object

TODO: probably some deep clean TODO: Move



52
53
54
55
56
57
58
59
60
# File 'app/controllers/collection_objects_controller.rb', line 52

def (models = {})
  h = {}
  models.each do |l, m|
    h.merge!(
      l => m.core_attributes.inject({}){|hsh,p| hsh[p] = nil; hsh}
    )
  end
  h
end


77
78
# File 'app/controllers/collection_objects_controller.rb', line 77

def navigation
end

#newObject

GET /collection_objects/new



189
190
191
# File 'app/controllers/collection_objects_controller.rb', line 189

def new
  @collection_object = CollectionObject.new
end

#previewObject

/collection_objects/preview?<filter params>



140
141
142
# File 'app/controllers/collection_objects_controller.rb', line 140

def preview
  @collection_objects = ::Queries::CollectionObject::Filter.new(params).all.order('collection_objects.id').includes(:dwc_occurrence).page(params[:page]).per(params[:per] || 500)
end

#preview_buffered_batch_loadObject



326
327
328
329
330
331
332
333
334
335
# File 'app/controllers/collection_objects_controller.rb', line 326

def preview_buffered_batch_load
  if params[:file]
    @result = BatchLoad::Import::CollectionObjects::BufferedInterpreter.new(**batch_params)
    digest_cookie(params[:file].tempfile, :Buffered_collection_objects_md5)
    render 'collection_objects/batch_load/buffered/preview'
  else
    flash[:notice] = 'No file provided!'
    redirect_to action: :batch_load
  end
end

#preview_castor_batch_loadObject



300
301
302
303
304
305
306
307
308
309
# File 'app/controllers/collection_objects_controller.rb', line 300

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

#preview_simple_batch_loadObject



268
269
270
271
272
273
274
275
276
277
# File 'app/controllers/collection_objects_controller.rb', line 268

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

#reportObject

Intent is DWC fields + quick summary fields for reports !! As currently implemented rebuilds DWC all



135
136
137
# File 'app/controllers/collection_objects_controller.rb', line 135

def report
  @collection_objects = ::Queries::CollectionObject::Filter.new(params).all.order('collection_objects.id').includes(:dwc_occurrence).page(params[:page]).per(params[:per] || 500)
end

#searchObject

GET /collection_object/search



251
252
253
254
255
256
257
# File 'app/controllers/collection_objects_controller.rb', line 251

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

#select_optionsObject



352
353
354
# File 'app/controllers/collection_objects_controller.rb', line 352

def select_options
  @collection_objects = CollectionObject.select_optimized(sessions_current_user_id, sessions_current_project_id, params[:target])
end

#set_collection_objectObject (private)



427
428
429
430
# File 'app/controllers/collection_objects_controller.rb', line 427

def set_collection_object
  @collection_object = CollectionObject.with_project_id(sessions_current_project_id).find(params[:id])
  @recent_object = @collection_object
end

#showObject

GET /collection_objects/1 GET /collection_objects/1.json



64
65
# File 'app/controllers/collection_objects_controller.rb', line 64

def show
end

#timelineObject

GET /collection_objects/1/timeline.json



68
69
70
# File 'app/controllers/collection_objects_controller.rb', line 68

def timeline
  @data = ::Catalog::CollectionObject.data_for(@collection_object)
end

#updateObject

PATCH/PUT /collection_objects/1 PATCH/PUT /collection_objects/1.json



215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/controllers/collection_objects_controller.rb', line 215

def update
  respond_to do |format|
    if @collection_object.update(collection_object_params)
      @collection_object = @collection_object.metamorphosize
      format.html { redirect_to url_for(@collection_object), notice: 'Collection object was successfully updated.' }
      format.json { render :show, status: :ok, location: @collection_object }
    else
      format.html { render action: 'edit' }
      format.json { render json: @collection_object.errors, status: :unprocessable_entity }
    end
  end
end

#user_mapObject (private)

TODO: not used?



475
476
477
478
479
480
481
482
483
484
485
486
# File 'app/controllers/collection_objects_controller.rb', line 475

def user_map
  {
    user_header_map: {
      'otu'         => 'otu_name',
      'start_day'   => 'start_date_day',
      'start_month' => 'start_date_month',
      'start_year'  => 'start_date_year',
      'end_day'     => 'end_date_day',
      'end_month'   => 'end_date_month',
      'end_year'    => 'end_date_year'}
  }
end