Class: AssertedDistributionsController

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

#api_indexObject



138
139
140
141
142
143
144
145
146
# File 'app/controllers/asserted_distributions_controller.rb', line 138

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

#api_paramsObject (private)



179
180
181
# File 'app/controllers/asserted_distributions_controller.rb', line 179

def api_params
  params.permit(:otu_id, :geographic_area_id, :recent, :geo_json)
end

#api_showObject



148
149
150
# File 'app/controllers/asserted_distributions_controller.rb', line 148

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

#asserted_distribution_paramsObject (private)



159
160
161
162
163
164
165
166
167
168
169
# File 'app/controllers/asserted_distributions_controller.rb', line 159

def asserted_distribution_params
  params.require(:asserted_distribution).permit(
    :otu_id,
    :geographic_area_id,
    :is_absent,
    otu_attributes: [:id, :_destroy, :name, :taxon_name_id],
    origin_citation_attributes: [:id, :_destroy, :source_id, :pages],
    citations_attributes: [:id, :is_original, :_destroy, :source_id, :pages, :citation_object_id, :citation_object_type],
    data_attributes_attributes: [ :id, :_destroy, :controlled_vocabulary_term_id, :type, :attribute_subject_id, :attribute_subject_type, :value ]
  )
end

#autocompleteObject



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

def autocomplete
  @asserted_distributions = Queries::AssertedDistribution::Autocomplete.new(params.require(:term), project_id: sessions_current_project_id).autocomplete
end

#batch_loadObject

GET /asserted_distributions/batch_load



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

def batch_load
end

#batch_paramsObject (private)



171
172
173
# File 'app/controllers/asserted_distributions_controller.rb', line 171

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

#createObject

POST /asserted_distributions POST /asserted_distributions.json



44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/asserted_distributions_controller.rb', line 44

def create
  @asserted_distribution = AssertedDistribution.new(asserted_distribution_params)
  respond_to do |format|
    if @asserted_distribution.save
      format.html { redirect_to @asserted_distribution, notice: 'Asserted distribution was successfully created.' }
      format.json { render :show, status: :created, location: @asserted_distribution }
    else
      format.html { render :new }
      format.json { render json: @asserted_distribution.errors, status: :unprocessable_entity }
    end
  end
end

#create_simple_batch_loadObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/asserted_distributions_controller.rb', line 123

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



73
74
75
76
77
78
79
80
81
# File 'app/controllers/asserted_distributions_controller.rb', line 73

def destroy
  @asserted_distribution.mark_citations_for_destruction
  @asserted_distribution.destroy!

  respond_to do |format|
    format.html { redirect_to asserted_distributions_url, notice: 'Asserted distribution was successfully destroyed.' }
    format.json { head :no_content }
  end
end

#downloadObject

GET /asserted_distributions/download



101
102
103
104
105
106
# File 'app/controllers/asserted_distributions_controller.rb', line 101

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

#editObject

GET /asserted_distributions/1/edit



38
39
40
# File 'app/controllers/asserted_distributions_controller.rb', line 38

def edit
  @asserted_distribution.origin_citation.source = Source.new if !@asserted_distribution.origin_citation.source
end

#filter_paramsObject (private)



175
176
177
# File 'app/controllers/asserted_distributions_controller.rb', line 175

def filter_params
  params.permit(:otu_id, :geographic_area_id, :recent, otu_id: [], geographic_area_id: [])
end

#indexObject

GET /asserted_distributions GET /asserted_distributions.json



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

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

#listObject



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

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

#newObject

GET /asserted_distributions/new



33
34
35
# File 'app/controllers/asserted_distributions_controller.rb', line 33

def new
  @asserted_distribution = AssertedDistribution.new(origin_citation: Citation.new)
end

#preview_simple_batch_loadObject



112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/asserted_distributions_controller.rb', line 112

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

#searchObject

TODO: deprecate



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

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

#set_asserted_distributionObject (private)



154
155
156
157
# File 'app/controllers/asserted_distributions_controller.rb', line 154

def set_asserted_distribution
  @asserted_distribution = AssertedDistribution.where(project_id: sessions_current_project_id).find(params[:id])
  @recent_object = @asserted_distribution
end

#showObject

GET /asserted_distributions/1 GET /asserted_distributions/1.json



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

def show
  # @asserted_distribution = AssertedDistribution.find(params[:id])
end

#updateObject

PATCH/PUT /asserted_distributions/1 PATCH/PUT /asserted_distributions/1.json



59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/asserted_distributions_controller.rb', line 59

def update
  respond_to do |format|
    if @asserted_distribution.update(asserted_distribution_params)
      format.html { redirect_to @asserted_distribution, notice: 'Asserted distribution was successfully updated.' }
      format.json { render :show, status: :ok, location: @asserted_distribution }
    else
      format.html { render :edit }
      format.json { render json: @asserted_distribution.errors, status: :unprocessable_entity }
    end
  end
end