Class: SourcesController

Inherits:
ApplicationController show all
Includes:
DataControllerConfiguration::SharedDataControllerConfiguration
Defined in:
app/controllers/sources_controller.rb

Instance Method Summary collapse

Methods included from DataControllerConfiguration::SharedDataControllerConfiguration

#set_is_shared_data_model

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/sources



228
229
230
231
232
233
# File 'app/controllers/sources_controller.rb', line 228

def api_index
  @sources = Queries::Source::Filter.new(api_params).all
    .order('sources.id')
    .page(params[:page]).per(params[:per])
  render '/sources/api/v1/index'
end

#api_paramsObject (private)



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
331
332
333
334
335
336
337
338
339
340
# File 'app/controllers/sources_controller.rb', line 296

def api_params
  params[:project_id] = sessions_current_project_id
  params.permit(
    :ancestor_id,
    :author,
    :author_ids_or,
    :citations,
    :citations_on_otus,
    # :documents
    :exact_author,
    :exact_title,
    :identifier,
    :identifier_end,
    :identifier_exact,
    :identifier_start,
    :in_project,
    :namespace_id,
    :nomenclature,
    :notes,
    :per,
    :project_id,
    :query_term,
    :recent,
    :roles,
    :source_type,
    :tags,
    :title,
    :user_date_end,
    :user_date_start,
    :user_id,
    :user_target,
    :with_doi,
    :year_end,
    :year_start,
    ids: [],
    author_ids: [],
    citation_object_type: [],
    keyword_id_and: [],
    keyword_id_or: [],
    topic_ids: [],
    serial_ids: [],
    empty: [],
    not_empty: []
  )
end

#api_showObject

GET /api/v1/sources/:id



236
237
238
# File 'app/controllers/sources_controller.rb', line 236

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

#attributesObject



74
75
76
77
78
79
# File 'app/controllers/sources_controller.rb', line 74

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

#autocompleteObject



144
145
146
147
148
149
150
# File 'app/controllers/sources_controller.rb', line 144

def autocomplete
  @term = params.require(:term) 
  @sources = Queries::Source::Autocomplete.new(
    @term,
    autocomplete_params
  ).autocomplete
end

#autocomplete_paramsObject (private)



246
247
248
# File 'app/controllers/sources_controller.rb', line 246

def autocomplete_params
  params.permit(:limit_to_project).merge(project_id: sessions_current_project_id).to_h.symbolize_keys
end

#batch_loadObject

GET /sources/batch_load



162
163
# File 'app/controllers/sources_controller.rb', line 162

def batch_load
end

#batch_paramsObject (private)



347
348
# File 'app/controllers/sources_controller.rb', line 347

def batch_params
end

#citation_object_typesObject

GET /sources/citation_object_types.json



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

def citation_object_types
  render json: Source.joins(:citations)
    .where(citations: {project_id: sessions_current_project_id})
    .select('citations.project_id, citations.citation_object_type')
    .distinct
    .pluck(:citation_object_type).sort
end

#cloneObject

POST /sources/1/clone.json



35
36
37
38
39
40
41
# File 'app/controllers/sources_controller.rb', line 35

def clone
  @source = @source.clone
  respond_to do |format|
    format.html { redirect_to edit_source_path(@source), notice: 'Clone successful, on new record.' }
    format.json { render :show }
  end
end

#createObject

POST /sources POST /sources.json



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

def create
  @source = new_source
  respond_to do |format|
    if @source&.save
      format.html { redirect_to url_for(@source.metamorphosize),
                    notice: "#{@source.type} successfully created." }
      format.json { render action: 'show', status: :created, location: @source.metamorphosize }
    else
      format.html { render action: 'new' }
      format.json { render json: @source&.errors, status: :unprocessable_entity }
    end
  end
end

#create_bibtex_batch_loadObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/controllers/sources_controller.rb', line 185

def create_bibtex_batch_load
  file = params.require(:file)
  redirect_to batch_load_sources_path, notice: 'no file has been selected' and return if file.blank?
  sha256 = Digest::SHA256.file(file.tempfile)
  if cookies[:batch_sources_md5] == sha256.hexdigest
    if result_hash = Source.batch_create(file.tempfile)
      # error in results?
      @count = result_hash[:count]
      @sources = result_hash[:records]
      flash[:notice] = "Successfully batch created #{@count} sources."
      render 'sources/batch_load/bibtex/bibtex_batch_create'
    else
      flash[:notice] = 'Failed to create sources.'
      redirect_to batch_load_sources_path
    end
  else
    flash[:notice] = 'Batch upload must be previewed before it can be created.'
    redirect_to batch_load_sources_path
  end
end

#csl_typesObject

GET /sources/csl_types.json



91
92
93
# File 'app/controllers/sources_controller.rb', line 91

def csl_types
  render json: ::CSL_STYLES
end

#destroyObject

DELETE /sources/1 DELETE /sources/1.json



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

def destroy
  if @source.destroy
    respond_to do |format|
      format.html { redirect_to sources_url, notice: "Destroyed source #{@source.cached}" }
      format.json { head :no_content }
    end
  else
    respond_to do |format|
      format.html { render action: :show, notice: 'failed to destroy the source, there is likely data associated with it' }
      format.json { render json: @source.errors, status: :unprocessable_entity }
    end
  end
end

#downloadObject

GET /sources/download



207
208
209
210
211
212
213
# File 'app/controllers/sources_controller.rb', line 207

def download
  send_data Export::Download.generate_csv(
    Source.joins(:project_sources)
    .where(project_sources: {project_id: sessions_current_project_id})
    .all),
  type: 'text', filename: "sources_#{DateTime.now}.csv"
end

#editObject

GET /sources/1/edit



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

def edit
  redirect_to new_source_task_path(source_id: @source.id), notice: 'Editing in new interface.'
end

#filter_paramsObject (private)



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'app/controllers/sources_controller.rb', line 250

def filter_params
  params[:project_id] = sessions_current_project_id
  params.permit(
    :author,
    :ancestor_id,
    :author_ids_or,
    :citations,
    :citations_on_otus,
    :documents,
    :exact_author,
    :exact_title,
    :identifier,
    :identifier_end,
    :identifier_exact,
    :identifier_start,
    :in_project,
    :namespace_id,
    :nomenclature,
    :notes,
    :per,
    :project_id,
    :query_term,
    :recent,
    :roles,
    :source_type,
    :tags,
    :title,
    :user_date_end,
    :user_date_start,
    :user_id,
    :user_target,
    :with_doi,
    :year_end,
    :year_start,
    author_ids: [],
    citation_object_type: [],
    ids: [],
    keyword_id_and: [],
    keyword_id_or: [],
    topic_ids: [],
    serial_ids: [],
    empty: [],
    not_empty: []
  )
end

#generateObject

GET /sources/generate.json?<filter params>



216
217
218
219
220
221
222
223
224
225
# File 'app/controllers/sources_controller.rb', line 216

def generate
  sources = Queries::Source::Filter.new(filter_params).all.page(params[:page]).per(params[:per] || 2000)
  @download = ::Export::Bibtex.download(
    sources,
    request.url,
    (params[:is_public] == 'true' ? true : false),
    params[:style_id]
  )
  render '/downloads/show.json'
end

#indexObject

GET /sources GET /sources.json



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

def index
  respond_to do |format|
    format.html do
      @recent_objects = Source.created_this_week.order(updated_at: :desc).limit(10)
      render '/shared/data/all/index'
    end
    format.json {
      @sources = Queries::Source::Filter.new(filter_params).all.order(:cached).page(params[:page]).per(params[:per] || 500)
    }
    format.bib {
      # TODO - handle count and download
      @sources = Queries::Source::Filter.new(filter_params).all.order(:cached).page(params[:page]).per(params[:per] || 2000)
    }
  end
end

#listObject



25
26
27
# File 'app/controllers/sources_controller.rb', line 25

def list
  @sources = Source.page(params[:page])
end

#newObject

GET /sources/new



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

def new
  redirect_to new_source_task_path, notice: "Redirected to new interface."
end

#new_sourceObject (private)



242
243
244
# File 'app/controllers/sources_controller.rb', line 242

def new_source
  (params[:bibtex_input].blank? ? Source.new(source_params) : Source::Bibtex.new_from_bibtex_text(params[:bibtex_input])) || nil
end

#parseObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/sources_controller.rb', line 95

def parse
  error_message = 'Unknown'

  begin
    @source = new_source
  rescue BibTeX::ParseError => e
    error_message = e.message
  end

  if @source
    render '/sources/show'
  else
    render json: { status: :failed, error: error_message }
  end
end

#preview_bibtex_batch_loadObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/controllers/sources_controller.rb', line 165

def preview_bibtex_batch_load
  file = params.require(:file)
  redirect_to batch_load_sources_path, notice: 'No file has been selected.' and return if file.blank?
  file_ok, mimetype = Utilities::Files.recognized_batch_file_type?(file.tempfile)
  if !file_ok
    redirect_to batch_load_sources_path,
      notice: "File '#{file.original_filename}' is of type '#{mimetype}', and not processable as BibTex."
  else
    @sources, message = Source.batch_preview(file.tempfile)
    if @sources.size > 0
      sha256 = Digest::SHA256.file(file.tempfile)
      cookies[:batch_sources_md5] = sha256.hexdigest
      render 'sources/batch_load/bibtex/bibtex_batch_preview'
    else
      redirect_to batch_load_sources_path,
        notice: "Error parsing BibTeX :#{message}."
    end
  end
end

#searchObject



152
153
154
155
156
157
158
159
# File 'app/controllers/sources_controller.rb', line 152

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

#select_optionsObject

GET /sources/select_options



70
71
72
# File 'app/controllers/sources_controller.rb', line 70

def select_options
  @sources = Source.select_optimized(sessions_current_user_id, sessions_current_project_id, params[:klass])
end

#set_sourceObject (private)



342
343
344
345
# File 'app/controllers/sources_controller.rb', line 342

def set_source
  @source = Source.find(params[:id])
  @recent_object = @source
end

#showObject

GET /sources/1 GET /sources/1.json



31
32
# File 'app/controllers/sources_controller.rb', line 31

def show
end

#source_paramsObject (private)



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'app/controllers/sources_controller.rb', line 350

def source_params
  params['source'][:project_sources_attributes] = [{project_id: sessions_current_project_id.to_s}]
  params.require(:source).permit(
    :serial_id, :address, :annote, :author, :booktitle, :chapter,
    :crossref, :edition, :editor, :howpublished, :institution,
    :journal, :key, :month, :note, :number, :organization, :pages,
    :publisher, :school, :series, :title, :type, :volume, :doi,
    :abstract, :copyright, :language, :stated_year, :verbatim,
    :bibtex_type, :day, :year, :isbn, :issn, :verbatim_contents,
    :verbatim_keywords, :language_id, :translator, :year_suffix, :url, :type, :style_id,
    :convert_to_bibtex,
    roles_attributes: [
      :id,
      :_destroy,
      :type,
      :person_id,
      :position,
      person_attributes: [
        :last_name, :first_name, :suffix, :prefix
      ]
    ],
    project_sources_attributes: [:project_id]
  )
end

#updateObject

PATCH/PUT /sources/1 PATCH/PUT /sources/1.json



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/sources_controller.rb', line 113

def update
  respond_to do |format|
    if @source.update(source_params)
      # We go through this dance to handle changing types from verbatim to other
      @source = @source.becomes!(@source.type.safe_constantize)
      @source.reload # necessary to reload the cached value.
      format.html { redirect_to url_for(@source.metamorphosize), notice: 'Source was successfully updated.' }
      format.json { render :show, status: :ok, location: @source.metamorphosize }
    else
      format.html { render action: 'edit' }
      format.json { render json: @source.errors, status: :unprocessable_entity }
    end
  end
end