Class: TaxonNamesController

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

GET /api/v1/taxon_names



223
224
225
226
227
228
229
# File 'app/controllers/taxon_names_controller.rb', line 223

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

#api_paramsObject (private)



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
361
362
363
364
365
366
367
368
# File 'app/controllers/taxon_names_controller.rb', line 332

def api_params
  params.permit(
    :ancestors,
    :author,
    :authors,
    :citations,
    :descendants,
    :descendants_max_depth,
    :etymology,
    :exact,
    :leaves,
    :name,
    :nomenclature_code,
    :nomenclature_group, # !! different than autocomplete
    :otus,
    :not_specified,
#     :page, # TODO: yes or no?
#     :per,
    :taxon_name_type,
    :type_metadata,
    :updated_since,
    :validity,
    :year,
    keyword_id_and: [],
    keyword_id_or: [],
    parent_id: [],
    taxon_name_classification: [],
    taxon_name_id: [],
    taxon_name_relationship: [],
    taxon_name_relationship_type: [],
    type: []
  ).to_h.symbolize_keys.merge(project_id: sessions_current_project_id)

  # TODO: see config in collection objects controller
  # 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

#api_parseObject



236
237
238
239
240
241
242
243
244
# File 'app/controllers/taxon_names_controller.rb', line 236

def api_parse
  @combination = Combination.where(project_id: sessions_current_project_id).find(params[:combination_id]) if params[:combination_id]
  @result = TaxonWorks::Vendor::Biodiversity::Result.new(
    query_string: params.require(:query_string),
    project_id: sessions_current_project_id,
    code: :iczn # !! TODO: generalize
  ).result
  render '/taxon_names/api/v1/parse'
end

#api_showObject

GET /api/v1/taxon_names/:id



232
233
234
# File 'app/controllers/taxon_names_controller.rb', line 232

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

#autocompleteObject



90
91
92
93
94
95
96
# File 'app/controllers/taxon_names_controller.rb', line 90

def autocomplete
  render json: {} and return if params[:term].blank?
  @taxon_names = Queries::TaxonName::Autocomplete.new(
    params[:term],
    **autocomplete_params
  ).autocomplete
end

#autocomplete_paramsObject (private)



253
254
255
256
257
258
# File 'app/controllers/taxon_names_controller.rb', line 253

def autocomplete_params
  params.permit(
    :valid, :exact, :no_leaves,
    type: [], parent_id: [], nomenclature_group: []
  ).to_h.symbolize_keys.merge(project_id: sessions_current_project_id)
end

#batch_loadObject



109
110
# File 'app/controllers/taxon_names_controller.rb', line 109

def batch_load
end

#batch_paramsObject (private)



279
280
281
282
283
284
285
286
287
288
289
# File 'app/controllers/taxon_names_controller.rb', line 279

def batch_params
  params.permit(
    :file,
    :parent_taxon_name_id,
    :nomenclature_code,
    :also_create_otu,
    :import_level).merge(
      user_id: sessions_current_user_id,
      project_id: sessions_current_project_id
    ).to_h.symbolize_keys
end

#createObject

POST /taxon_names POST /taxon_names.json



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

def create
  @taxon_name = TaxonName.new(taxon_name_params)
  respond_to do |format|
    if @taxon_name.save
      format.html { redirect_to url_for(@taxon_name.metamorphosize),
                    notice: "Taxon name '#{@taxon_name.name}' was successfully created." }
      format.json { render :show, status: :created, location: @taxon_name.metamorphosize }
    else
      format.html { render action: :new }
      format.json { render json: @taxon_name.errors, status: :unprocessable_entity }
    end
  end
end

#create_castor_batch_loadObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/controllers/taxon_names_controller.rb', line 194

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



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

def create_simple_batch_load
  if params[:file] && digested_cookie_exists?(params[:file].tempfile, :simple_taxon_names_md5)
    @result =  BatchLoad::Import::TaxonifiToTaxonworks.new(**batch_params)
    if @result.create
      flash[:notice] = "Successfully proccessed file, #{@result.total_records_created} taxon names were created."
      render 'taxon_names/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 /taxon_names/1 DELETE /taxon_names/1.json



69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/taxon_names_controller.rb', line 69

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

#downloadObject

GET /taxon_names/download



103
104
105
106
107
# File 'app/controllers/taxon_names_controller.rb', line 103

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

#editObject

GET /taxon_names/1/edit



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

def edit
  @taxon_name.source = Source.new if !@taxon_name.source
end

#filter_paramsObject (private)



291
292
293
294
295
296
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
329
330
# File 'app/controllers/taxon_names_controller.rb', line 291

def filter_params
  params.permit(
    :ancestors,
    :author,
    :authors,
    :citations,
    :descendants,
    :descendants_max_depth,
    :etymology,
    :exact,
    :leaves,
    :name,
    :nomenclature_code,
    :nomenclature_group, # !! different than autocomplete
    :not_specified,
    :otus,
    :page,
    :per,
    :taxon_name_author_ids_or,
    :taxon_name_type,
    :type_metadata,
    :updated_since,
    :user_date_end,
    :user_date_start,
    :user_id,
    :user_target,
    :validity,
    :year,
    keyword_id_and: [],
    keyword_id_or: [],
    parent_id: [],
    taxon_name_author_ids: [],
    taxon_name_classification: [],
    taxon_name_id: [],
    taxon_name_relationship: [],
    taxon_name_relationship_type: [],
    type: []
    # user_id: []
  ).to_h.symbolize_keys.merge(project_id: sessions_current_project_id)
end

#indexObject

GET /taxon_names GET /taxon_names.json



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

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

#listObject



98
99
100
# File 'app/controllers/taxon_names_controller.rb', line 98

def list
  @taxon_names = TaxonName.with_project_id(sessions_current_project_id).order(:id).page(params[:page])
end

#newObject

GET /taxon_names/new



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

def new
  @taxon_name = Protonym.new(source: Source.new)
end

#original_combinationObject

GET /taxon_names/1/original_combination



219
220
# File 'app/controllers/taxon_names_controller.rb', line 219

def original_combination
end

#parseObject



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

def parse
  @combination = Combination.where(project_id: sessions_current_project_id).find(params[:combination_id]) if params[:combination_id]
  @result = TaxonWorks::Vendor::Biodiversity::Result.new(
    query_string: params.require(:query_string),
    project_id: sessions_current_project_id,
    code: :iczn # !! TODO: generalize
  ).result
end

#predicted_rankObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/taxon_names_controller.rb', line 116

def predicted_rank
  if params[:parent_id]
    p = TaxonName.find_by(id: params[:parent_id])
    if p.nil?
      render json: {predicted_rank: ''}.to_json
    else
      render json: {predicted_rank: p.predicted_child_rank(params[:name]).to_s}.to_json
    end
  else
    render json: {predicted_rank: ''}.to_json
  end
end

#preview_castor_batch_loadObject



183
184
185
186
187
188
189
190
191
192
# File 'app/controllers/taxon_names_controller.rb', line 183

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

#preview_simple_batch_loadObject



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

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

#randomObject



129
130
131
132
133
# File 'app/controllers/taxon_names_controller.rb', line 129

def random
  redirect_to browse_nomenclature_task_path(
    taxon_name_id: TaxonName.where(project_id: sessions_current_project_id).order('random()').limit(1).pluck(:id).first
  )
end

#rank_tableObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/taxon_names_controller.rb', line 135

def rank_table
  @q = Queries::TaxonName::Tabular.new(
    ancestor_id: params.require(:ancestor_id),
    ranks: params.require(:ranks),
    fieldsets: params[:fieldsets],
    limit: params[:limit],
    validity: params[:validity],
    combinations: params[:combinations],
    project_id: sessions_current_project_id,
    rank_data: params[:rank_data]
  )
end

#ranksObject



112
113
114
# File 'app/controllers/taxon_names_controller.rb', line 112

def ranks
  render json: RANKS_JSON.to_json
end

#searchObject



82
83
84
85
86
87
88
# File 'app/controllers/taxon_names_controller.rb', line 82

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

#select_optionsObject

GET /taxon_names/select_options



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

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

#set_taxon_nameObject (private)



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

def set_taxon_name
  @taxon_name = TaxonName.with_project_id(sessions_current_project_id).includes(:creator, :updater).find(params[:id])
  @recent_object = @taxon_name
end

#showObject

GET /taxon_names/1 GET /taxon_names/1.json



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

def show
end

#taxon_name_paramsObject (private)



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'app/controllers/taxon_names_controller.rb', line 260

def taxon_name_params
  params.require(:taxon_name).permit(
    :name,
    :parent_id,
    :year_of_publication,
    :etymology,
    :verbatim_author, :verbatim_name, :rank_class, :type, :masculine_name,
    :feminine_name, :neuter_name, :also_create_otu,
    roles_attributes: [
      :id, :_destroy, :type, :person_id, :position,
      person_attributes: [
        :last_name, :first_name, :suffix, :prefix
      ]
    ],
    origin_citation_attributes: [:id, :_destroy, :source_id, :pages],
    taxon_name_classifications_attributes: [:id, :_destroy, :type]
  )
end

#updateObject

PATCH/PUT /taxon_names/1 PATCH/PUT /taxon_names/1.json



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/taxon_names_controller.rb', line 54

def update
  respond_to do |format|
    if @taxon_name.update(taxon_name_params)
      @taxon_name.reload
      format.html { redirect_to url_for(@taxon_name.metamorphosize), notice: 'Taxon name was successfully updated.' }
      format.json { render :show, status: :ok, location: @taxon_name.metamorphosize }
    else
      format.html { render action: :edit }
      format.json { render json: @taxon_name.errors, status: :unprocessable_entity }
    end
  end
end