Class: DwcOccurrencesController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::ProjectDataControllerConfiguration
Defined in:
app/controllers/dwc_occurrences_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, #cumulative_projects_created_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, #sound_cumulative_gb_per_year, #sound_gb_per_year, #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_area_autocompleteObject

GET /api/v1/dwc_occurrences/area_autocomplete?term=Can&target=country

[View source]

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/dwc_occurrences_controller.rb', line 38

def api_area_autocomplete

  target = params[:target] || 'country'

  if !['country', 'stateProvince', 'county'].include?(target) || params[:term].blank?
    render json: {}, status: :unprocessable_entity and return
  end

  names = DwcOccurrence.select(target.to_sym)
    .where(Arel.sql("\"dwc_occurrences\".\"#{target}\" ILIKE '%#{params[:term]}%'"))
    .order(target.to_sym)
    .distinct
    .limit(20)
    .pluck(target.to_sym)
    .sort_by{|a| a.length}

  render json: names 
end

#api_indexObject

[View source]

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/dwc_occurrences_controller.rb', line 17

def api_index
  q = Queries::DwcOccurrence::Filter.new(params.merge!(api: true)).all
    .where(project_id: sessions_current_project_id)
    .page(params[:page]).per(params[:per])

  respond_to do |format|
    format.json {
      @dwc_occurrences = q
      render '/dwc_occurrences/api/v1/index'
    }
    format.csv {
      @dwc_occurrences = q.limit(100000)
      send_data Export::CSV.generate_csv(
        @dwc_occurrences,
        exclude_columns: %w{updated_by_id created_by_id project_id, rebuild_set},
      ), type: 'text', filename: "dwc_occurrences_#{DateTime.now}.tsv"
    }
  end
end

#attributesObject

TODO: extract this all, unify with Concerns::IsData model, and re-use in DwcOccurrence

[View source]

93
94
95
96
97
98
# File 'app/controllers/dwc_occurrences_controller.rb', line 93

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

#collector_id_metadataObject

[View source]

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

def 
  render json: helpers.
end

#downloadObject

GET /dwc_occurence/download

[View source]

81
82
83
84
# File 'app/controllers/dwc_occurrences_controller.rb', line 81

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

#indexObject

.json only

[View source]

10
11
12
13
14
15
# File 'app/controllers/dwc_occurrences_controller.rb', line 10

def index
  @dwc_occurrences = Queries::DwcOccurrence::Filter.new(params).all
    .where(project_id: sessions_current_project_id)
    .page(params[:page])
    .per(params[:per] || 1)
end

#metadataObject

[View source]

57
58
59
# File 'app/controllers/dwc_occurrences_controller.rb', line 57

def 
  @dwc_occurrences = DwcOccurrence.where(project_id: sessions_current_project_id)
end

#predicatesObject

[View source]

61
62
# File 'app/controllers/dwc_occurrences_controller.rb', line 61

def predicates
end

#set_objectObject (protected)

[View source]

102
103
104
105
106
107
108
# File 'app/controllers/dwc_occurrences_controller.rb', line 102

def set_object
  @object = GlobalID::Locator.locate(params[:object_global_id])

  if @object.nil? || (@object.project_id != sessions_current_project_id)
    render status: :not_found and return
  end
end

#statusObject

[View source]

64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/dwc_occurrences_controller.rb', line 64

def status
  if @object
    render json: {
      object: params[:object_global_id],
      id: @object.dwc_occurrence&.id,
      updated_at:  @object.dwc_occurrence&.updated_at
    }
  else
    render json: {}, status: :unprocessable_entity
  end
end

#sweepObject

POST /dwc_occurrence/sweep (admin)

[View source]

87
88
89
90
# File 'app/controllers/dwc_occurrences_controller.rb', line 87

def sweep
  DwcOccurrence.sweep
  redirect_to administration_data_health_task_path and return
end