Class: SourcesController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- SourcesController
- Defined in:
- app/controllers/sources_controller.rb
Instance Method Summary collapse
-
#api_index ⇒ Object
GET /api/v1/sources.
-
#api_show ⇒ Object
GET /api/v1/sources/:id.
- #attributes ⇒ Object
- #autocomplete ⇒ Object
- #autocomplete_params ⇒ Object private
-
#batch_load ⇒ Object
GET /sources/batch_load.
- #batch_params ⇒ Object private
-
#citation_object_types ⇒ Object
GET /sources/citation_object_types.json.
-
#clone ⇒ Object
POST /sources/1/clone.json.
-
#create ⇒ Object
POST /sources POST /sources.json.
- #create_bibtex_batch_load ⇒ Object
-
#csl_types ⇒ Object
GET /sources/csl_types.json.
-
#destroy ⇒ Object
DELETE /sources/1 DELETE /sources/1.json.
-
#download ⇒ Object
GET /sources/download.
-
#edit ⇒ Object
GET /sources/1/edit.
-
#generate ⇒ Object
GET /sources/generate.json?<filter params>.
-
#index ⇒ Object
GET /sources GET /sources.json.
- #list ⇒ Object
-
#new ⇒ Object
GET /sources/new.
- #new_source ⇒ Object private
- #parse ⇒ Object
- #preview_bibtex_batch_load ⇒ Object
- #search ⇒ Object
-
#select_options ⇒ Object
GET /sources/select_options.
- #set_source ⇒ Object private
-
#show ⇒ Object
GET /sources/1 GET /sources/1.json.
- #source_params ⇒ Object private
-
#update ⇒ Object
PATCH/PUT /sources/1 PATCH/PUT /sources/1.json.
Methods included from DataControllerConfiguration::SharedDataControllerConfiguration
Methods included from RedirectHelper
Methods included from RequestType
Methods included from LogRecent
Methods included from Cookies
#digest_cookie, #digested_cookie_exists?
Methods included from Whitelist
Methods included from Api::Intercept
Methods included from TokenAuthentication
#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate
Instance Method Details
#api_index ⇒ Object
GET /api/v1/sources
230 231 232 233 234 235 |
# File 'app/controllers/sources_controller.rb', line 230 def api_index @sources = Queries::Source::Filter.new(params.merge!(api: true)).all .order('sources.id') .page(params[:page]).per(params[:per]) render '/sources/api/v1/index' end |
#api_show ⇒ Object
GET /api/v1/sources/:id
238 239 240 |
# File 'app/controllers/sources_controller.rb', line 238 def api_show render '/sources/api/v1/show' end |
#attributes ⇒ Object
87 88 89 90 91 92 |
# File 'app/controllers/sources_controller.rb', line 87 def attributes render json: ::Source.columns.select{ |a| Queries::Source::Filter::ATTRIBUTES.include?( a.name.to_sym) }.collect{|b| {'name' => b.name, 'type' => b.type } } end |
#autocomplete ⇒ Object
146 147 148 149 150 151 152 |
# File 'app/controllers/sources_controller.rb', line 146 def autocomplete @term = params.require(:term) @sources = Queries::Source::Autocomplete.new( @term, **autocomplete_params ).autocomplete end |
#autocomplete_params ⇒ Object (private)
252 253 254 |
# File 'app/controllers/sources_controller.rb', line 252 def autocomplete_params params.permit(:limit_to_project).merge(project_id: sessions_current_project_id).to_h.symbolize_keys end |
#batch_load ⇒ Object
GET /sources/batch_load
164 165 |
# File 'app/controllers/sources_controller.rb', line 164 def batch_load end |
#batch_params ⇒ Object (private)
261 262 |
# File 'app/controllers/sources_controller.rb', line 261 def batch_params end |
#citation_object_types ⇒ Object
GET /sources/citation_object_types.json
95 96 97 98 99 100 101 |
# File 'app/controllers/sources_controller.rb', line 95 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 |
#clone ⇒ Object
POST /sources/1/clone.json
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/controllers/sources_controller.rb', line 41 def clone respond_to do |format| @source = @source.clone if @source.valid? format.html { redirect_to edit_source_path(@source), notice: 'Clone successful, on new record.' } format.json { render :show } else format.html { redirect_to edit_source_path(@source), notice: 'Clone failed. On original original.' } format.json { render json: @source.errors, status: :unprocessable_entity } end end end |
#create ⇒ Object
POST /sources POST /sources.json
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/controllers/sources_controller.rb', line 66 def create @source = new_source respond_to do |format| # We must check for manually added errors first, as they # are lost when .valid? is called during the callback chain. if !@source.errors.any? && @source.save format.html { redirect_to url_for(@source.), notice: "#{@source.type} successfully created." } format.json { render action: 'show', status: :created, location: @source. } else format.html { render action: 'new' } format.json { render json: @source.errors, status: :unprocessable_entity } end end end |
#create_bibtex_batch_load ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/controllers/sources_controller.rb', line 187 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 [: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_types ⇒ Object
GET /sources/csl_types.json
104 105 106 |
# File 'app/controllers/sources_controller.rb', line 104 def csl_types render json: ::CSL_STYLES end |
#destroy ⇒ Object
DELETE /sources/1 DELETE /sources/1.json
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'app/controllers/sources_controller.rb', line 132 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 |
#download ⇒ Object
GET /sources/download
209 210 211 212 213 214 215 |
# File 'app/controllers/sources_controller.rb', line 209 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 |
#edit ⇒ Object
GET /sources/1/edit
55 56 57 |
# File 'app/controllers/sources_controller.rb', line 55 def edit redirect_to new_source_task_path(source_id: @source.id), notice: 'Editing in new interface.' end |
#generate ⇒ Object
GET /sources/generate.json?<filter params>
218 219 220 221 222 223 224 225 226 227 |
# File 'app/controllers/sources_controller.rb', line 218 def generate sources = Queries::Source::Filter.new(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 |
#index ⇒ Object
GET /sources GET /sources.json
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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(params).all .order(:cached) .page(params[:page]) .per(params[:per]) } format.bib { # TODO - handle count and download @sources = Queries::Source::Filter.new(params).all .order(:cached) .page(params[:page]) .per(params[:per] || 2000) } end end |
#list ⇒ Object
31 32 33 |
# File 'app/controllers/sources_controller.rb', line 31 def list @sources = Source.page(params[:page]) end |
#new ⇒ Object
GET /sources/new
60 61 62 |
# File 'app/controllers/sources_controller.rb', line 60 def new redirect_to new_source_task_path, notice: "Redirected to new interface." end |
#new_source ⇒ Object (private)
244 245 246 247 248 249 250 |
# File 'app/controllers/sources_controller.rb', line 244 def new_source if params[:bibtex_input].blank? Source.new(source_params) else Source::Bibtex.new_from_bibtex_text(params[:bibtex_input]) end end |
#parse ⇒ Object
108 109 110 111 |
# File 'app/controllers/sources_controller.rb', line 108 def parse @source = new_source render '/sources/show' end |
#preview_bibtex_batch_load ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'app/controllers/sources_controller.rb', line 167 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, = Source.batch_preview(file.tempfile) if @sources.size > 0 sha256 = Digest::SHA256.file(file.tempfile) [:batch_sources_md5] = sha256.hexdigest render 'sources/batch_load/bibtex/bibtex_batch_preview' else redirect_to batch_load_sources_path, notice: "Error parsing BibTeX :#{}." end end end |
#search ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'app/controllers/sources_controller.rb', line 154 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_options ⇒ Object
GET /sources/select_options
83 84 85 |
# File 'app/controllers/sources_controller.rb', line 83 def @sources = Source.select_optimized(sessions_current_user_id, sessions_current_project_id, params[:klass]) end |
#set_source ⇒ Object (private)
256 257 258 259 |
# File 'app/controllers/sources_controller.rb', line 256 def set_source @source = Source.find(params[:id]) @recent_object = @source end |
#show ⇒ Object
GET /sources/1 GET /sources/1.json
37 38 |
# File 'app/controllers/sources_controller.rb', line 37 def show end |
#source_params ⇒ Object (private)
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'app/controllers/sources_controller.rb', line 264 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 |
#update ⇒ Object
PATCH/PUT /sources/1 PATCH/PUT /sources/1.json
115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/controllers/sources_controller.rb', line 115 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.), notice: 'Source was successfully updated.' } format.json { render :show, status: :ok, location: @source. } else format.html { render action: 'edit' } format.json { render json: @source.errors, status: :unprocessable_entity } end end end |