/
curation_concern_controller.rb
212 lines (179 loc) · 6.28 KB
/
curation_concern_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
module CurationConcerns::CurationConcernController
extend ActiveSupport::Concern
include Blacklight::Base
include Blacklight::AccessControls::Catalog
included do
copy_blacklight_config_from(::CatalogController)
include CurationConcerns::ThemedLayoutController
with_themed_layout '1_column'
helper CurationConcerns::AbilityHelper
class_attribute :_curation_concern_type, :show_presenter
self.show_presenter = CurationConcerns::WorkShowPresenter
attr_accessor :curation_concern
helper_method :curation_concern, :contextual_path
end
module ClassMethods
def curation_concern_type=(curation_concern_type)
load_and_authorize_resource class: curation_concern_type, instance_name: :curation_concern, except: [:show, :file_manager]
self._curation_concern_type = curation_concern_type
end
def curation_concern_type
_curation_concern_type
end
def cancan_resource_class
CurationConcerns::ControllerResource
end
end
def new
build_form
end
def create
if actor.create(attributes_for_actor)
after_create_response
else
respond_to do |wants|
wants.html do
build_form
render 'new', status: :unprocessable_entity
end
wants.json { render_json_response(response_type: :unprocessable_entity, options: { errors: curation_concern.errors }) }
end
end
end
# Finds a solr document matching the id and sets @presenter
# @raises CanCan::AccessDenied if the document is not found
# or the user doesn't have access to it.
def show
respond_to do |wants|
wants.html { presenter && parent_presenter }
wants.json do
# load and authorize @curation_concern manually because it's skipped for html
# This has to use #find instead of #load_instance_from_solr because
# we want to return values like file_set_ids in the json
@curation_concern = _curation_concern_type.find(params[:id]) unless curation_concern
authorize! :show, @curation_concern
render :show, status: :ok
end
additional_response_formats(wants)
document_export_formats(wants)
end
end
def edit
build_form
end
def update
if actor.update(attributes_for_actor)
after_update_response
else
respond_to do |wants|
wants.html do
build_form
render 'edit', status: :unprocessable_entity
end
wants.json { render_json_response(response_type: :unprocessable_entity, options: { errors: curation_concern.errors }) }
end
end
end
def destroy
title = curation_concern.to_s
curation_concern.destroy
CurationConcerns.config.callback.run(:after_destroy, curation_concern.id, current_user)
after_destroy_response(title)
end
def file_manager
presenter
end
attr_writer :actor
protected
# Gives the class of the form. Override this if you want
# to use a different form.
def form_class
CurationConcerns.const_get("#{_curation_concern_type.to_s.demodulize}Form")
end
def build_form
@form = form_class.new(curation_concern, current_ability)
end
def actor
@actor ||= CurationConcerns::CurationConcern.actor(curation_concern, current_user)
end
def presenter
@presenter ||= show_presenter.new(curation_concern_from_search_results, current_ability, request)
end
def parent_presenter
@parent_presenter ||=
begin
if params[:parent_id]
@parent_presenter ||= show_presenter.new(search_result_document(id: params[:parent_id]), current_ability, request)
end
end
end
def _prefixes
@_prefixes ||= super + ['curation_concerns/base']
end
def after_create_response
respond_to do |wants|
wants.html { redirect_to contextual_path(curation_concern, parent_presenter) }
wants.json { render :show, status: :created, location: polymorphic_path([main_app, curation_concern]) }
end
end
def after_update_response
# TODO: visibility or lease/embargo status
if curation_concern.visibility_changed? && curation_concern.file_sets.present?
redirect_to main_app.confirm_curation_concerns_permission_path(curation_concern)
else
respond_to do |wants|
wants.html { redirect_to [main_app, curation_concern] }
wants.json { render :show, status: :ok, location: polymorphic_path([main_app, curation_concern]) }
end
end
end
def after_destroy_response(title)
flash[:notice] = "Deleted #{title}"
respond_to do |wants|
wants.html { redirect_to main_app.search_catalog_path }
wants.json { render_json_response(response_type: :deleted, message: "Deleted #{curation_concern.id}") }
end
end
def attributes_for_actor
form_class.model_attributes(params[hash_key_for_curation_concern])
end
def hash_key_for_curation_concern
_curation_concern_type.model_name.param_key
end
# Override this method to add additional response
# formats to your local app
def additional_response_formats(_)
# nop
end
def search_builder_class
CurationConcerns::WorkSearchBuilder
end
def contextual_path(presenter, parent_presenter)
::CurationConcerns::ContextualPath.new(presenter, parent_presenter).show
end
def document_export_formats(format)
format.any do
format_name = params.fetch(:format, '').to_sym
raise ActionController::UnknownFormat unless presenter.export_formats.include? format_name
render_document_export_format format_name
end
end
##
# Render the document export formats for a response
# First, try to render an appropriate template (e.g. index.endnote.erb)
# If that fails, just concatenate the document export responses with a newline.
def render_document_export_format(format_name)
render
rescue ActionView::MissingTemplate
render text: presenter.export_as(format_name), layout: false
end
private
def curation_concern_from_search_results
search_result_document(params)
end
def search_result_document(search_params)
_, document_list = search_results(search_params)
raise CanCan::AccessDenied.new(nil, :show) if document_list.empty?
document_list.first
end
end