app/controllers/docs_controller.rb
# add new docs to a project
add new docs to a project
add new docs to a project
def add
		begin
			project = Project.editable(current_user).find_by_name(params[:project_id])
			raise "The project does not exist, or you are not authorized to make a change to the project.\n" unless project.present?
			docspecs =  if params["_json"] && params["_json"].class == Array
										params["_json"].collect{|d| d.symbolize_keys}
									elsif params["sourcedb"].present? && params["sourceid"].present?
										[{sourcedb:params["sourcedb"], sourceid:params["sourceid"]}]
									elsif params[:ids].present? && params[:sourcedb].present?
										params[:ids].strip.split(/[ ,"':|\t\n\r]+/).collect{|id| id.strip}.collect{|id| {sourcedb:params[:sourcedb], sourceid:id}}
									else
										[]
									end
			raise ArgumentError, "no valid document specification found." if docspecs.empty?
			docspecs.each{|d| d[:sourceid].sub!(/^(PMC|pmc)/, '')}
			docspecs.uniq!
			if docspecs.length == 1
				docspec = docspecs.first
				begin
					result = project.add_doc(docspec[:sourcedb], docspec[:sourceid])
					raise ArgumentError, "The document already exists." if result == nil
					expire_fragment("sourcedb_counts_#{project.name}")
					expire_fragment("count_docs_#{project.name}")
					expire_fragment("count_#{docspec[:sourcedb]}_#{project.name}")
					message = "#{docspec[:sourcedb]}:#{docspec[:sourceid]} - added."
				rescue => e
					message = "#{docspec[:sourcedb]}:#{docspec[:sourceid]} - #{e.message}"
				end
			else
				AddDocsToProjectJob.perform_later(project, docspecs)
				message = "The task, 'add documents to the project', is created."
			end
		rescue => e
			message = e.message
		end
		respond_to do |format|
			format.html {redirect_back fallback_location: root_path, flash: { notice: message}}
			format.json {render json:{message:message}}
		end
	end
def add begin project = Project . editable ( current_user ) . find_by_name ( params [ :project_id ] ) raise "The project does not exist, or you are not authorized to make a change to the project.\n" unless project . present? docspecs = if params [ "_json" ] && params [ "_json" ] . class == Array params [ "_json" ] . collect { | d | d . symbolize_keys } elsif params [ "sourcedb" ] . present? && params [ "sourceid" ] . present? [ { sourcedb : params [ "sourcedb" ] , sourceid : params [ "sourceid" ] } ] elsif params [ :ids ] . present? && params [ :sourcedb ] . present? params [ :ids ] . strip . split ( / [ ,"':| \t \n \r ]+ / ) . collect { | id | id . strip } . collect { | id | { sourcedb : params [ :sourcedb ] , sourceid : id } } else [ ] end raise ArgumentError , "no valid document specification found." if docspecs . empty? docspecs . each { | d | d [ :sourceid ] . sub! ( / ^(PMC|pmc) / , '' ) } docspecs . uniq! if docspecs . length == 1 docspec = docspecs . first begin result = project . add_doc ( docspec [ :sourcedb ] , docspec [ :sourceid ] ) raise ArgumentError , "The document already exists." if result == nil expire_fragment ( "sourcedb_counts_#{project.name}" ) expire_fragment ( "count_docs_#{project.name}" ) expire_fragment ( "count_#{docspec[:sourcedb]}_#{project.name}" ) message = "#{docspec[:sourcedb]}:#{docspec[:sourceid]} - added." rescue => e message = "#{docspec[:sourcedb]}:#{docspec[:sourceid]} - #{e.message}" end else AddDocsToProjectJob . perform_later ( project , docspecs ) message = "The task, 'add documents to the project', is created." end rescue => e message = e . message end respond_to do | format | format . html { redirect_back fallback_location : root_path , flash : { notice : message } } format . json { render json : { message : message } } end end
add new docs to a project
add new docs to a project
# get the docspecs list # AddDocsToProjectJob.perform_now(project, docspecs)