Class: OtusController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/otus_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

#api_autocompleteObject

GET /api/v1/otus/autocomplete



304
305
306
307
308
309
310
311
312
313
314
# File 'app/controllers/otus_controller.rb', line 304

def api_autocomplete
  @otu_metadata = ::Queries::Otu::Autocomplete.new(
    params.require(:term),
    project_id: sessions_current_project_id,
    with_taxon_name: params[:with_taxon_name],
    having_taxon_name_only: params[:having_taxon_name_only]
  ).api_autocomplete_extended


  render '/otus/api/v1/autocomplete'
end

#api_contentObject

GET /api/v1/otus/:id/inventory/content



358
359
360
361
362
363
364
365
# File 'app/controllers/otus_controller.rb', line 358

def api_content
  topic_ids = [params[:topic_id]].flatten.compact.uniq

  @public_content =  PublicContent.where(otu: @otu, project_id: sessions_current_project_id)
  @public_content = @public_content.joins(:topic).where(topic_id: topic_ids) unless topic_ids.empty?

  render '/otus/api/v1/inventory/content'
end

#api_distributionObject

TODO: Considerations .json

* Scope Genus, Family by default
*
* 404 when no CachedMap computable

.geo_json

* Always returns result, could be empty

GET /api/v1/otus/:id/inventory/distribution.json GET /api/v1/otus/:id/inventory/distribution.geojson



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'app/controllers/otus_controller.rb', line 411

def api_distribution
  respond_to do |format|
    format.json do
      @cached_map_type = params[:cached_map_type] || 'CachedMapItem::WebLevel1'
      @quicker_cached_map = @otu.quicker_cached_map(@cached_map_type)
      if @quicker_cached_map.blank?
        render json: { error: 'no map available'}, status: :not_found and return
      end
      render '/otus/api/v1/inventory/distribution'
    end
    format.geojson do
      render '/otus/api/v1/inventory/distribution'
    end
  end

end

GET /api/v1/otus/:id/inventory/dwc_gallery.json?per=1&page=2



350
351
352
353
354
355
# File 'app/controllers/otus_controller.rb', line 350

def api_dwc_gallery
  # see otus_helper

  @data = helpers.dwc_gallery_data(@otu, dwc_occurrence_id: params[:dwc_occurrence_id])
  render '/otus/api/v1/inventory/dwc_gallery'
end

#api_dwc_inventoryObject

GET /api/v1/otus/:id/inventory/dwc



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'app/controllers/otus_controller.rb', line 327

def api_dwc_inventory
  respond_to do |format|
    format.csv do
      send_data Export::CSV.generate_csv(
        DwcOccurrence.scoped_by_otu(@otu),
        exclude_columns: ['id', 'created_by_id', 'updated_by_id', 'project_id', 'updated_at']),
      type: 'text',
      filename: "dwc_#{helpers.label_for_otu(@otu).gsub(/\W/,'_')}_#{DateTime.now}.csv"
    end

    format.json do
      if params[:page].blank? && params[:per].blank?
        render json: DwcOccurrence.scoped_by_otu(@otu).to_json
      else # only apply if provided, do not fall back to default scope
        r = DwcOccurrence.scoped_by_otu(@otu).page(params[:page]).per(params[:per])
        assign_pagination(r)
        render json: r.to_json
      end
    end
  end
end

#api_indexObject

GET /api/v1/otus.csv GET /api/v1/otus



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

def api_index
  q = ::Queries::Otu::Filter.new(params.merge!(api: true)).all
    .where(project_id: sessions_current_project_id)
    .order('otus.id')

  respond_to do |format|
    format.json {
      @otus = q.page(params[:page]).per(params[:per])
      render '/otus/api/v1/index'
    }
    format.csv {
      @otus = q
      send_data Export::CSV.generate_csv(
        @otus,
        exclude_columns: %w{updated_by_id created_by_id project_id},
      ), type: 'text', filename: "otus_#{DateTime.now}.tsv"
    }
  end
end

#api_key_inventoryObject

GET /api/v1/otus/:id/inventory/keys



317
318
319
# File 'app/controllers/otus_controller.rb', line 317

def api_key_inventory
  render json: helpers.otu_key_inventory(@otu)
end

#api_nomenclature_citationsObject

GET /api/v1/otus/:id/inventory/nomenclature_citations



373
374
375
376
377
378
379
380
# File 'app/controllers/otus_controller.rb', line 373

def api_nomenclature_citations
  if @otu.taxon_name
    @data = ::Catalog::Nomenclature::Entry.new(@otu.taxon_name)
    render '/otus/api/v1/inventory/nomenclature_citations'
  else
    render json: {}, status: :unprocessable_entity
  end
end

#api_showObject

GET /api/v1/otus/:id



289
290
291
# File 'app/controllers/otus_controller.rb', line 289

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

#api_taxonomy_inventoryObject

GET /api/v1/otus/:id/inventory/taxonomy



322
323
324
# File 'app/controllers/otus_controller.rb', line 322

def api_taxonomy_inventory
  render '/otus/api/v1/inventory/taxonomy'
end

#api_type_material_inventoryObject

GET /api/v1/otus/:id/inventory/type_material



368
369
370
# File 'app/controllers/otus_controller.rb', line 368

def api_type_material_inventory
  render '/otus/api/v1/inventory/type_material'
end

#autocompleteObject



293
294
295
296
297
298
299
300
301
# File 'app/controllers/otus_controller.rb', line 293

def autocomplete
  @otus = ::Queries::Otu::Autocomplete.new(
    params.require(:term),
    exact: 'true',
    project_id: sessions_current_project_id,
    with_taxon_name: params[:with_taxon_name],
    having_taxon_name_only: params[:having_taxon_name_only],
  ).autocomplete
end

#batch_loadObject



128
129
130
# File 'app/controllers/otus_controller.rb', line 128

def batch_load
  # see app/views/otus/batch_load.html.erb
end

#batch_paramsObject (private)



446
447
448
449
450
451
452
453
454
455
456
# File 'app/controllers/otus_controller.rb', line 446

def batch_params
  params.permit(
    :name, :file, :import_level,
    :create_new_otu, :source_id, :type_select, :create_new_predicate,
    files: [])
    .merge(
      user_id: sessions_current_user_id,
      project_id: sessions_current_project_id)
    .to_h
    .symbolize_keys
end

#batch_updateObject

PATCH /otus/batch_update.json?otus_query=<>&otu=taxon_name_id=123}



254
255
256
257
258
259
260
261
262
263
264
# File 'app/controllers/otus_controller.rb', line 254

def batch_update
  if r = Otu.batch_update(
      preview: params[:preview],
      otu: otu_params.merge(by: sessions_current_user_id),
      otu_query: params[:otu_query],
  )
    render json: r.to_json, status: :ok
  else
    render json: {}, status: :unprocessable_entity
  end
end

GET /otus/1/navigation.json



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

def breadcrumbs
  render json: :not_found and return if @otu.nil?
end

#by_nameObject

GET api/v1/otus/by_name/:name?token=:token&project_id=:id



243
244
245
246
# File 'app/controllers/otus_controller.rb', line 243

def by_name
  @otu_name = params.require(:name)
  @otu_ids = ::Queries::Otu::Autocomplete.new(@otu_name, project_id: params.require(:project_id)).all.pluck(:id)
end

#collection_objectsObject

GET /otus/1/collection_objects



115
116
117
# File 'app/controllers/otus_controller.rb', line 115

def collection_objects
  @collection_objects = Otu.where(project_id: sessions_current_project_id).find(params[:id]).collection_objects.pluck(:id)
end

#coordinateObject

GET /otus/1/coordinate.json



58
59
60
61
# File 'app/controllers/otus_controller.rb', line 58

def coordinate
  @otus = Otu.coordinate_otus(@otu.id)
  render :index
end

#createObject

POST /otus POST /otus.json



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/otus_controller.rb', line 70

def create
  @otu = Otu.new(otu_params)

  respond_to do |format|
    if @otu.save
      format.html { redirect_to @otu,
                    notice: "Otu '#{@otu.name}' was successfully created." }
      format.json { render action: :show, status: :created, location: @otu }
    else
      format.html { render action: 'new' }
      format.json { render json: @otu.errors, status: :unprocessable_entity }
    end
  end
end

#create_data_attributes_batch_loadObject

TODO: AUTOGENERATED STUB, check and update



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

def create_data_attributes_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :data_attributes_batch_load_otus_md5)
    @result = BatchLoad::Import::Otus::DataAttributesInterpreter.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} TODO RECORD TYPES were created."
      render 'otus/batch_load/data_attributes/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_identifiers_batch_loadObject



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

def create_identifiers_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :batch_load_otus_identifiers_md5)
    @result = BatchLoad::Import::Otus::IdentifiersInterpreter.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully processed file, #{@result.total_records_created} otus were created."
      render('otus/batch_load/identifiers/create')
      return
    else
      flash[:alert] = 'Batch import failed.'
    end
    render(:batch_load)
  end
end

#create_simple_batch_file_loadObject



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

def create_simple_batch_file_load
  if params[:files] && digested_cookie_exists?(params[:files][0].tempfile, :batch_file_load_simple_md5)
    @result = BatchFileLoad::Import::Otus::SimpleInterpreter.new(**batch_params)

    if @result.create
      flash[:notice] = "Successfully processed #{@result.total_files_processed} file(s), #{@result.total_records_created} otus were created."
      render 'otus/batch_file_load/simple/create'
      return
    else
      flash[:alert] = 'Batch import failed.'
      render :batch_load
    end
  end
end

#create_simple_batch_loadObject



143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/otus_controller.rb', line 143

def create_simple_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :batch_otus_md5)
    @result = BatchLoad::Import::Otus.new(**batch_params.merge(user_map))
    if @result.create
      flash[:notice] = "Successfully processed file, #{@result.total_records_created} otus were created."
      render('otus/batch_load/simple/create') and return
    else
      flash[:alert] = 'Batch import failed.'
    end
    render(:batch_load)
  end
end

#destroyObject

DELETE /otus/1 DELETE /otus/1.json



101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/controllers/otus_controller.rb', line 101

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

#distributionObject

TODO: Redirect to json if too big? GET /otus/:id/inventory/distribution.json GET /otus/:id/inventory/distribution.geojson



385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'app/controllers/otus_controller.rb', line 385

def distribution
  respond_to do |format|
    format.json do
      @cached_map_type = params[:cached_map_type] || 'CachedMapItem::WebLevel1'
      @quicker_cached_map = @otu.quicker_cached_map(@cached_map_type)

      render json: { error: 'no map available'}, status: :not_found unless @quicker_cached_map.present? and return
    end

    format.geojson do
    end
  end
end

#downloadObject

GET /otus/download



236
237
238
239
240
# File 'app/controllers/otus_controller.rb', line 236

def download
  send_data Export::CSV.generate_csv(Otu.where(project_id: sessions_current_project_id)),
    type: 'text',
    filename: "otus_#{DateTime.now}.tsv"
end

#editObject

GET /otus/1/edit



41
42
# File 'app/controllers/otus_controller.rb', line 41

def edit
end

#indexObject

GET /otus GET /otus.json



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/otus_controller.rb', line 14

def index
  respond_to do |format|
    format.html do
      @recent_objects = Otu.recent_from_project_id(sessions_current_project_id).order(updated_at: :desc).limit(10)
      render '/shared/data/all/index'
    end
    format.json {
      @otus = ::Queries::Otu::Filter.new(params).all
        .page(params[:page])
        .per(params[:per])
        .eager_load(:taxon_name)
        .order(:cached, 'otus.name')
    }
  end
end

#listObject



44
45
46
# File 'app/controllers/otus_controller.rb', line 44

def list
  @otus = Otu.with_project_id(sessions_current_project_id).page(params[:page]).per(params[:per])
end

GET /otus/1/navigation.json



54
55
# File 'app/controllers/otus_controller.rb', line 54

def navigation
end

#newObject

GET /otus/new



36
37
38
# File 'app/controllers/otus_controller.rb', line 36

def new
  @otu = Otu.new
end

#otu_paramsObject (private)

end end



442
443
444
# File 'app/controllers/otus_controller.rb', line 442

def otu_params
  params.require(:otu).permit(:name, :taxon_name_id, :exact)
end

#preview_data_attributes_batch_loadObject

TODO: AUTOGENERATED STUB, check and update



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

def preview_data_attributes_batch_load
  if params[:file]
    @result = BatchLoad::Import::Otus::DataAttributesInterpreter.new(**batch_params)
    digest_cookie(params[:file].tempfile, :data_attributes_batch_load_otus_md5)
    render 'otus/batch_load/data_attributes/preview'
  else
    flash[:notice] = 'No file provided!'
    redirect_to action: :batch_load
  end
end

#preview_identifiers_batch_loadObject



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

def preview_identifiers_batch_load
  if params[:file]
    @result = BatchLoad::Import::Otus::IdentifiersInterpreter.new(**batch_params)
    digest_cookie(params[:file].tempfile, :batch_load_otus_identifiers_md5)
    render('otus/batch_load/identifiers/preview')
  else
    flash[:notice] = 'No file provided!'
    redirect_to action: :batch_load
  end
end

#preview_simple_batch_file_loadObject



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

def preview_simple_batch_file_load
  if params[:files]
    @result = BatchFileLoad::Import::Otus::SimpleInterpreter.new(**batch_params)
    digest_cookie(params[:files][0].tempfile, :batch_file_load_simple_md5)
    render 'otus/batch_file_load/simple/preview'
  else
    flash[:notice] = 'No file(s) provided!'
    redirect_to action: :batch_load
  end
end

#preview_simple_batch_loadObject



132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/otus_controller.rb', line 132

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

#searchObject



119
120
121
122
123
124
125
126
# File 'app/controllers/otus_controller.rb', line 119

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

#select_optionsObject

GET /otus/select_options?target=TaxonDetermination



249
250
251
# File 'app/controllers/otus_controller.rb', line 249

def select_options
  @otus = Otu.select_optimized(sessions_current_user_id, sessions_current_project_id, params.require(:target))
end

#set_otuObject (private)



430
431
432
433
# File 'app/controllers/otus_controller.rb', line 430

def set_otu
  @otu = Otu.where(project_id: sessions_current_project_id).eager_load(:taxon_name).find(params[:id])
  @recent_object = @otu
end

#showObject

GET /otus/1 GET /otus/1.json



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

def show
end

#timelineObject

GET /otus/1/timeline.json



49
50
51
# File 'app/controllers/otus_controller.rb', line 49

def timeline
  @catalog = Catalog::Timeline.new(targets: [@otu])
end

#updateObject

PATCH/PUT /otus/1 PATCH/PUT /otus/1.json



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

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

#user_mapObject (private)



458
459
460
# File 'app/controllers/otus_controller.rb', line 458

def user_map
  {user_header_map: {'otu' => 'otu_name'}}
end