Class: SourcesController

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

Instance Attribute Summary

Attributes inherited from ApplicationController

#is_data_controller, #is_task_controller, #meta_data, #meta_description, #meta_keywords, #meta_title, #page_title, #site_name

Instance Method Summary (collapse)

Methods included from DataControllerConfiguration::SharedDataControllerConfiguration

#set_is_shared_data_model

Methods inherited from ApplicationController

#digest_cookie, #digested_cookie_exists?, #disable_turbolinks, #invalid_object, #is_data_controller?, #is_task_controller?, #log_user_recent_route, #project_matches, #record_not_found, #set_project_from_params

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#project_link, #project_tag, #projects_list, #projects_search_form

Methods included from InterceptApi

#intercept_api, #token_authenticate

Instance Method Details

- (Object) autocomplete



86
87
88
89
90
91
# File 'app/controllers/sources_controller.rb', line 86

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

- (Object) autocomplete_params (private)



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

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

- (Object) batch_load

GET /sources/batch_load



103
104
# File 'app/controllers/sources_controller.rb', line 103

def batch_load
end

- (Object) batch_params (private)



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

def batch_params
end

- (Object) create

POST /sources POST /sources.json



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/sources_controller.rb', line 40

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

- (Object) create_bibtex_batch_load



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/controllers/sources_controller.rb', line 126

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

- (Object) destroy

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



78
79
80
81
82
83
84
# File 'app/controllers/sources_controller.rb', line 78

def destroy
  @source.destroy!
  respond_to do |format|
    format.html { redirect_to sources_url }
    format.json { head :no_content }
  end
end

- (Object) download

GET /sources/download



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

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

- (Object) edit

GET /sources/1/edit



35
36
# File 'app/controllers/sources_controller.rb', line 35

def edit
end

- (Object) filter_params (private)



166
167
168
# File 'app/controllers/sources_controller.rb', line 166

def filter_params
  params.permit(:query_term, :project_id, author_ids: [])
end

- (Object) index

GET /sources GET /sources.json



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

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.distinct.page(params[:page]).per(500)
    }
  end
end

- (Object) list



20
21
22
# File 'app/controllers/sources_controller.rb', line 20

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

- (Object) new

GET /sources/new



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

def new
  @source = Source.new
end

- (Object) new_source (private)



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

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

- (Object) parse



54
55
56
57
58
59
60
# File 'app/controllers/sources_controller.rb', line 54

def parse
  if @source = new_source
    render '/sources/show'
  else
    render json: {status: :failed}
  end
end

- (Object) preview_bibtex_batch_load



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/sources_controller.rb', line 106

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

- (Object) search



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

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

- (Object) set_source (private)



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

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

- (Object) show

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



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

def show
end

- (Object) source_params (private)



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/controllers/sources_controller.rb', line 179

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,
    roles_attributes: [
      :id,
      :_destroy,
      :type,
      :person_id,
      :position,
      person_attributes: [
        :last_name, :first_name, :suffix, :prefix
      ]
    ],
    project_sources_attributes: [:project_id]
  )
end

- (Object) update

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



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

def update
  respond_to do |format|
    if @source.update(source_params)
      format.html { redirect_to url_for(@source.metamorphosize), notice: 'Source was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @source.errors, status: :unprocessable_entity }
    end
  end
end