Class: WorksController

Inherits:
ApplicationController show all
Includes:
ERB::Util
Defined in:
app/controllers/works_controller.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Methods inherited from ApplicationController

#new_session_path

Instance Method Details

#add_messageObject



199
200
201
202
203
204
205
206
207
208
# File 'app/controllers/works_controller.rb', line 199

def add_message
  work = Work.find(params[:id])
  if params["new-message"].present?
    new_message_param = params["new-message"]
    sanitized_new_message = html_escape(new_message_param)

    work.add_message(sanitized_new_message, current_user.id)
  end
  redirect_to work_path(id: params[:id])
end

#add_provenance_noteObject



210
211
212
213
214
215
216
217
218
219
220
# File 'app/controllers/works_controller.rb', line 210

def add_provenance_note
  work = Work.find(params[:id])
  if params["new-provenance-note"].present?
    new_date = params["new-provenance-date"]
    new_label = params["change_label"]
    new_note = html_escape(params["new-provenance-note"])

    work.add_provenance_note(new_date, new_note, current_user.id, new_label)
  end
  redirect_to work_path(id: params[:id])
end

#approveObject



158
159
160
161
162
163
# File 'app/controllers/works_controller.rb', line 158

def approve
  @work = Work.find(params[:id])
  @work.approve!(current_user)
  flash[:notice] = "Your files are being moved to the #{@work.files_mode_human} bucket in the background. Depending on the file sizes this may take some time."
  redirect_to work_path(@work)
end

#assign_curatorObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/controllers/works_controller.rb', line 184

def assign_curator
  work = Work.find(params[:id])
  work.change_curator(params[:uid], current_user)
  if work.errors.count > 0
    render json: { errors: work.errors.map(&:type) }, status: :bad_request
  else
    render json: {}
  end
rescue => ex
  # This is necessary for JSON responses
  Rails.logger.error("Error changing curator for work: #{work.id}. Exception: #{ex.message}")
  Honeybadger.notify("Error changing curator for work: #{work.id}. Exception: #{ex.message}")
  render(json: { errors: ["Cannot save dataset"] }, status: :bad_request)
end

#awaiting_approvalObject

This method allows any user to visit /works/awaiting-approval.rss and a list is generated for works with a draft DOI in an RSS format so that the works are harvestable by PDC Discovery. In order to support landing pages for not yet approved works in PDC Discovery (see github.com/pulibrary/pdc_describe/issues/2204).



56
57
58
59
60
61
62
63
64
# File 'app/controllers/works_controller.rb', line 56

def awaiting_approval
  if rss_awaiting_approval_request?
    rss_awaiting_approval_index
  else
    # If a user who is not a super admin attempts to visit /awaiting_approval they will be redirected to the root_path and receive a flash notice that they do not have access to that page.
    flash[:notice] = "You do not have access to this page."
    redirect_to root_path
  end
end

#bibtexObject

Returns the raw BibTex citation information



245
246
247
248
249
250
251
# File 'app/controllers/works_controller.rb', line 245

def bibtex
  work = Work.find(params[:id])
  creators = work.resource.creators.map { |creator| "#{creator.family_name}, #{creator.given_name}" }
  citation = DatasetCitation.new(creators, [work.resource.publication_year], work.resource.titles.first.title, work.resource.resource_type, work.resource.publisher, work.resource.doi)
  bibtex = citation.bibtex
  send_data bibtex, filename: "#{citation.bibtex_id}.bibtex", type: "text/plain", disposition: "attachment"
end

#createObject

only non wizard mode



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/works_controller.rb', line 75

def create
  @work = Work.new(created_by_user_id: current_user.id, group_id: params_group_id, user_entered_doi: params["doi"].present?)
  @work.resource = FormToResourceService.convert(params, @work)
  @work.resource.migrated = migrated?
  if @work.valid?
    @work.draft!(current_user)
    upload_service = WorkUploadsEditService.new(@work, current_user)
    upload_service.update_precurated_file_list(added_files_param, deleted_files_param)
    redirect_to work_url(@work), notice: "Work was successfully created."
  else
    @work_decorator = WorkDecorator.new(@work, current_user)
    @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
    # return 200 so the loadbalancer doesn't capture the error
    render :new
  end
end

#dataciteObject

Outputs the Datacite XML representation of the work



223
224
225
226
# File 'app/controllers/works_controller.rb', line 223

def datacite
  work = Work.find(params[:id])
  render xml: work.to_xml
end

#datacite_validateObject



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

def datacite_validate
  @errors = []
  @work = Work.find(params[:id])
  validator = WorkValidator.new(@work)
  unless validator.valid_datacite?
    @errors = @work.errors.full_messages
  end
end

#editObject

GET /works/1/edit only non wizard mode



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

def edit
  @work = Work.find(params[:id])
  @work_decorator = WorkDecorator.new(@work, current_user)
  if validate_modification_permissions(work: @work,
                                       uneditable_message: "Can not update work: #{@work.id} is not editable by #{current_user.uid}",
                                       current_state_message: "Can not update work: #{@work.id} is not editable in current state by #{current_user.uid}")
    @uploads = @work.uploads
    @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
  end
end

#file_listObject

only non wizard mode



114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/works_controller.rb', line 114

def file_list
  if params[:id] == "NONE"
    # This is a special case when we render the file list for a work being created
    # (i.e. it does not have an id just yet)
    render json: file_list_ajax_response(nil)
  else
    work = Work.find(params[:id])
    render json: file_list_ajax_response(work)
  end
end

#indexObject

This method allows any user to visit /works.rss and a list is generated of all approved works in an RSS format so that the works are harvestable by PDC Discovery.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/works_controller.rb', line 35

def index
  if rss_index_request?
    rss_index
  # If the user is authenticated as a super admin and visits /works it will return a list of all approved works in an HTML format.
  elsif current_user.super_admin?
    @works = Work.all
    respond_to do |format|
      format.html
    end
  else
    # If a user who is not a super admin attempts to visit /works they will be redirected to the root_path and receive a flash notice that they do not have access to that page.
    flash[:notice] = "You do not have access to this page."
    redirect_to root_path
  end
end

#migrating?Boolean

Returns:

  • (Boolean)


237
238
239
240
241
# File 'app/controllers/works_controller.rb', line 237

def migrating?
  return @work.resource.migrated if @work&.resource && !params.key?(:migrate)

  params[:migrate]
end

#newObject

only non wizard mode



67
68
69
70
71
72
# File 'app/controllers/works_controller.rb', line 67

def new
  group = Group.find_by(code: params[:group_code]) || current_user.default_group
  @work = Work.new(created_by_user_id: current_user.id, group:)
  @work_decorator = WorkDecorator.new(@work, current_user)
  @form_resource_decorator = FormResourceDecorator.new(@work, current_user)
end

#resolve_arkObject



130
131
132
133
# File 'app/controllers/works_controller.rb', line 130

def resolve_ark
  @work = Work.find_by_ark(params[:ark])
  redirect_to @work
end

#resolve_doiObject



125
126
127
128
# File 'app/controllers/works_controller.rb', line 125

def resolve_doi
  @work = Work.find_by_doi(params[:doi])
  redirect_to @work
end

#resubmitObject



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

def resubmit
  @work = Work.find(params[:id])
  @work.resubmit!(current_user)
  redirect_to work_path(@work)
end

#revert_to_draftObject



177
178
179
180
181
182
# File 'app/controllers/works_controller.rb', line 177

def revert_to_draft
  @work = Work.find(params[:id])
  @work.revert_to_draft!(current_user)

  redirect_to work_path(@work)
end

#showObject

Show the information for the dataset with the given id When requested as .json, return the internal json resource



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

def show
  @work = Work.find(params[:id])
  UpdateSnapshotJob.perform_later(work_id: @work.id, last_snapshot_id: work.upload_snapshots.first&.id)
  @work_decorator = WorkDecorator.new(@work, current_user)

  respond_to do |format|
    format.html do
      # Ensure that the Work belongs to a Group
      group = @work_decorator.group
      raise(Work::InvalidGroupError, "The Work #{@work.id} does not belong to any Group") unless group

      @can_curate = current_user.can_admin?(group)
      @work.mark_new_notifications_as_read(current_user.id)
    end
    format.json { render json: @work.to_json }
  end
end

#updateObject

PATCH /works/1 only non wizard mode



150
151
152
153
154
155
156
# File 'app/controllers/works_controller.rb', line 150

def update
  @work = Work.find(params[:id])
  if validate_modification_permissions(work: @work, uneditable_message: "Can not update work: #{@work.id} is not editable by #{current_user.uid}",
                                       current_state_message: "Can not update work: #{@work.id} is not editable in current state by #{current_user.uid}")
    update_work
  end
end

#upload_filesObject

POST /works/1/upload-files (called via Uppy)



254
255
256
257
258
259
# File 'app/controllers/works_controller.rb', line 254

def upload_files
  @work = Work.find(params[:id])
  upload_service = WorkUploadsEditService.new(@work, current_user)
  upload_service.update_precurated_file_list(params["files"], [])
  render plain: params["files"].map(&:original_filename).join(",")
end

#validateObject

Validates that the work is ready to be approved GET /works/1/validate



263
264
265
266
267
268
269
270
271
272
# File 'app/controllers/works_controller.rb', line 263

def validate
  @work = Work.find(params[:id])
  if @work.valid_to_complete
    redirect_to work_review_path(@work)
  else
    message = @work.errors.to_a.join(", ")
    transition_error_message = "We apologize, the following errors were encountered: #{message}. Please contact the PDC Describe administrators for any assistance."
    redirect_to edit_work_url(id: @work.id), notice: transition_error_message, params:
  end
end

#withdrawObject



165
166
167
168
169
# File 'app/controllers/works_controller.rb', line 165

def withdraw
  @work = Work.find(params[:id])
  @work.withdraw!(current_user)
  redirect_to work_path(@work)
end