Class: CollectionObjectsController

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

Instance Method Summary collapse

Methods included from DataControllerConfiguration::ProjectDataControllerConfiguration

#annotator_params

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

Instance Method Details

#autocompleteObject



150
151
152
153
154
155
156
# File 'app/controllers/collection_objects_controller.rb', line 150

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



164
165
# File 'app/controllers/collection_objects_controller.rb', line 164

def batch_load
end

#batch_paramsObject (private)



281
282
283
284
# File 'app/controllers/collection_objects_controller.rb', line 281

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

#by_identifierObject

GET /collection_objects/by_identifier/ABCD

Raises:

  • (ActiveRecord::RecordNotFound)


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

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)



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

def collection_object_params
  params.require(:collection_object).permit(
    :total, :preparation_type_id, :repository_id,
    :ranged_lot_category_id, :collecting_event_id,
    :buffered_collecting_event, :buffered_determinations,
    :buffered_other_labels, :deaccessioned_at, :deaccession_reason,
    :contained_in,
    collecting_event_attributes: [],  # needs to be filled out!
    data_attributes_attributes: [ :id, :_destroy, :controlled_vocabulary_term_id, :type, :attribute_subject_id, :attribute_subject_type, :value ]
  )
end

#containerizeObject



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

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

#createObject

POST /collection_objects POST /collection_objects.json



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/collection_objects_controller.rb', line 90

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'app/controllers/collection_objects_controller.rb', line 236

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



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/collection_objects_controller.rb', line 210

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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/controllers/collection_objects_controller.rb', line 178

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.json



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

def depictions
end

#destroyObject

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



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

def destroy
  @collection_object.destroy
  respond_to do |format|
    if @collection_object.destroyed?
      format.html {redirect_back(fallback_location: (request.referer || root_path), notice: 'CollectionObject was successfully destroyed.')}
      format.json {head :no_content}
    else
      format.html {redirect_back(fallback_location: (request.referer || root_path), notice: 'CollectionObject was not destroyed, ' + errors.messages)}
      format.json {render json: @collection_object.errors, status: :unprocessable_entity}
    end
  end
end

#downloadObject

GET /collection_objects/download



159
160
161
# File 'app/controllers/collection_objects_controller.rb', line 159

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

#dwcObject



38
39
40
41
42
43
44
45
# File 'app/controllers/collection_objects_controller.rb', line 38

def dwc
  o = nil
  ActiveRecord::Base.connection_pool.with_connection do
    o = CollectionObject.find(params[:id])
    o.get_dwc_occurrence
  end
  render json: o.dwc_occurrence_attribute_values
end

#dwc_indexObject



32
33
34
35
36
# File 'app/controllers/collection_objects_controller.rb', line 32

def dwc_index
  @objects = filtered_collection_objects.includes(:dwc_occurrence).all.pluck( ::CollectionObject.dwc_attribute_vector  )
  @klass = ::CollectionObject
  render '/dwc_occurrences/dwc_index'
end

#dwcaObject

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



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

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



85
86
# File 'app/controllers/collection_objects_controller.rb', line 85

def edit
end

#filter_paramsObject (private)



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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'app/controllers/collection_objects_controller.rb', line 300

def filter_params
  a = params.permit(
    :recent,
    Queries::CollectingEvent::Filter::ATTRIBUTES,
    :in_labels,
    :in_verbatim_locality,
    :geo_json,
    :wkt,
    :radius,
    :start_date,
    :end_date,
    :partial_overlap_dates,
    :ancestor_id, 
    :current_determinations,
    :validity,
    :user_target,
    :user_start_date,
    :user_end_date,
    :identifier,
    :identifier_start,
    :identifier_end,
    :identifier_exact,
    :namespace_id,
    :never_loaned,
    :loaned,
    :on_loan,
    :spatial_geographic_areas,
    otu_ids: [],
    keyword_ids: [],
    collecting_event_ids: [],
    geographic_area_ids: [],
    biocuration_class_ids: [],
    biological_relationship_ids: []
    
    #  keyword_ids: [],
    #  collecting_event: {
    #   :recent,
    #   keyword_ids: []
    # }
  )

  a[:user_id] = params[:user_id] if params[:user_id] && is_project_member_by_id(params[:user_id], sessions_current_project_id) # double check vs. setting project_id from API
  a
end

#filtered_collection_objectsObject (private)



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

def filtered_collection_objects
  Queries::CollectionObject::Filter.
    new(filter_params).all.where(project_id: sessions_current_project_id).
    page(params[:page]).per(params[:per] || 500).
    order('collection_objects.id')
end

#geo_jsonObject

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



65
66
67
68
# File 'app/controllers/collection_objects_controller.rb', line 65

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

#imagesObject

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



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

def images
  @images = @collection_object.images
end

#indexObject

GET /collecting_events GET /collecting_events.json



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/collection_objects_controller.rb', line 10

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 {
      @collection_objects = filtered_collection_objects
    }
  end
end

#listObject

GET /collection_objects/list



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

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

#newObject

GET /collection_objects/new



80
81
82
# File 'app/controllers/collection_objects_controller.rb', line 80

def new
  @collection_object = CollectionObject.new
end

#preview_buffered_batch_loadObject



225
226
227
228
229
230
231
232
233
234
# File 'app/controllers/collection_objects_controller.rb', line 225

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



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

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



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

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

#searchObject

GET /collection_object/search



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

def search
  if params[:id].blank?
    redirect_to collection_object_path, notice: '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



251
252
253
# File 'app/controllers/collection_objects_controller.rb', line 251

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

#set_collection_objectObject (private)



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

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



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

def show
end

#updateObject

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



106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/controllers/collection_objects_controller.rb', line 106

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?



287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/controllers/collection_objects_controller.rb', line 287

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