geoblacklight_admin 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +24 -11
  3. data/Rakefile +83 -47
  4. data/app/assets/javascripts/geoblacklight_admin/chosen.js +1 -0
  5. data/app/assets/stylesheets/geoblacklight_admin/_core.scss +24 -0
  6. data/app/assets/stylesheets/geoblacklight_admin/modules/_nav.scss +0 -5
  7. data/app/assets/stylesheets/geoblacklight_admin/modules/_tables.scss +1 -1
  8. data/app/controllers/admin/admin_controller.rb +16 -0
  9. data/app/controllers/admin/advanced_search_controller.rb +7 -1
  10. data/app/controllers/admin/assets_controller.rb +47 -32
  11. data/app/controllers/admin/bookmarks_controller.rb +17 -5
  12. data/app/controllers/admin/bulk_actions_controller.rb +32 -1
  13. data/app/controllers/admin/document_accesses_controller.rb +38 -0
  14. data/app/controllers/admin/document_assets_controller.rb +47 -89
  15. data/app/controllers/admin/document_distributions_controller.rb +172 -0
  16. data/app/controllers/admin/documents_controller.rb +43 -57
  17. data/app/controllers/admin/elements_controller.rb +24 -0
  18. data/app/controllers/admin/form_elements_controller.rb +33 -0
  19. data/app/controllers/admin/ids_controller.rb +6 -0
  20. data/app/controllers/admin/import_documents_controller.rb +11 -1
  21. data/app/controllers/admin/imports_controller.rb +34 -4
  22. data/app/controllers/admin/mappings_controller.rb +15 -0
  23. data/app/controllers/admin/notifications_controller.rb +27 -0
  24. data/app/controllers/admin/reference_types_controller.rb +106 -0
  25. data/app/controllers/admin/search_controller.rb +7 -0
  26. data/app/controllers/admin/users_controller.rb +10 -0
  27. data/app/helpers/asset_helper.rb +6 -0
  28. data/app/helpers/bulk_actions_helper.rb +10 -1
  29. data/app/helpers/document_helper.rb +36 -0
  30. data/app/helpers/geoblacklight_admin_helper.rb +88 -8
  31. data/app/helpers/mappings_helper.rb +26 -0
  32. data/app/indexers/document_indexer.rb +22 -2
  33. data/app/javascript/channels/consumer.js +6 -0
  34. data/app/javascript/channels/export_channel.js +30 -0
  35. data/app/javascript/channels/index.js +3 -0
  36. data/app/javascript/controllers/results_controller.js +14 -0
  37. data/app/javascript/index.js +8 -2
  38. data/app/jobs/bulk_action_revert_document_job.rb +4 -12
  39. data/app/jobs/bulk_action_run_document_job.rb +2 -10
  40. data/app/jobs/export_job.rb +35 -8
  41. data/app/jobs/geoblacklight_admin/delete_thumbnail_job.rb +19 -0
  42. data/app/jobs/geoblacklight_admin/remove_parent_dct_references_uri_job.rb +16 -0
  43. data/app/jobs/geoblacklight_admin/set_parent_dct_references_uri_job.rb +17 -0
  44. data/app/jobs/geoblacklight_admin/store_image_job.rb +22 -0
  45. data/app/jobs/import_document_job.rb +1 -4
  46. data/app/jobs/import_run_job.rb +1 -1
  47. data/app/models/admin/bookmark.rb +1 -1
  48. data/app/models/asset.rb +20 -0
  49. data/app/models/bulk_action.rb +2 -1
  50. data/app/models/bulk_actions/change_publication_state.rb +0 -25
  51. data/app/models/bulk_actions/delete_thumbnails.rb +6 -0
  52. data/app/models/bulk_actions/draft_document.rb +6 -0
  53. data/app/models/bulk_actions/harvest_thumbnails.rb +6 -0
  54. data/app/models/bulk_actions/publish_document.rb +6 -0
  55. data/app/models/bulk_actions/unpublish_document.rb +6 -0
  56. data/app/models/document/geom_validator.rb +8 -0
  57. data/app/models/document/reference.rb +65 -65
  58. data/app/models/document.rb +129 -72
  59. data/app/models/document_distribution.rb +145 -0
  60. data/app/models/element.rb +2 -0
  61. data/app/models/geoblacklight_admin/schema.rb +10 -2
  62. data/app/models/import/csv_duplicates_validator.rb +2 -0
  63. data/app/models/import/csv_header_validator.rb +3 -1
  64. data/app/models/import_btaa_aardvark.rb +2 -2
  65. data/app/models/import_document_state_machine.rb +1 -0
  66. data/app/models/import_gblv1.rb +2 -2
  67. data/app/models/reference_type.rb +40 -0
  68. data/app/models/user.rb +4 -2
  69. data/app/services/export_csv_document_distributions_service.rb +61 -0
  70. data/app/services/geoblacklight_admin/image_service/iiif_manifest.rb +39 -43
  71. data/app/services/geoblacklight_admin/image_service/tms.rb +0 -8
  72. data/app/services/geoblacklight_admin/image_service.rb +1 -1
  73. data/app/services/geoblacklight_admin/item_viewer.rb +4 -4
  74. data/app/views/admin/bookmarks/index.html.erb +19 -14
  75. data/app/views/admin/bulk_actions/show.html.erb +1 -1
  76. data/app/views/admin/document_accesses/import.html.erb +6 -2
  77. data/app/views/admin/document_assets/_assets_table.html.erb +49 -0
  78. data/app/views/admin/document_assets/_form.html.erb +2 -3
  79. data/app/views/admin/document_assets/index.html.erb +1 -47
  80. data/app/views/admin/document_distributions/_document_distribution.html.erb +39 -0
  81. data/app/views/admin/document_distributions/_document_distribution.json.jbuilder +2 -0
  82. data/app/views/admin/document_distributions/_form.html.erb +34 -0
  83. data/app/views/admin/document_distributions/destroy_all.html.erb +82 -0
  84. data/app/views/admin/document_distributions/edit.html.erb +12 -0
  85. data/app/views/admin/document_distributions/import.html.erb +80 -0
  86. data/app/views/admin/document_distributions/index.html.erb +143 -0
  87. data/app/views/admin/document_distributions/index.json.jbuilder +1 -0
  88. data/app/views/admin/document_distributions/new.html.erb +11 -0
  89. data/app/views/admin/document_distributions/show.html.erb +10 -0
  90. data/app/views/admin/document_distributions/show.json.jbuilder +1 -0
  91. data/app/views/admin/documents/_document.html.erb +1 -3
  92. data/app/views/admin/documents/_form.html.erb +2 -4
  93. data/app/views/admin/documents/_form_control.html.erb +5 -2
  94. data/app/views/admin/documents/_form_nav.html.erb +14 -5
  95. data/app/views/admin/documents/_form_nav_kithe.html.erb +4 -1
  96. data/app/views/admin/documents/_json_aardvark.jbuilder +1 -1
  97. data/app/views/admin/documents/_json_gbl_v1.jbuilder +1 -1
  98. data/app/views/admin/documents/_result_selected_options.html.erb +5 -2
  99. data/app/views/admin/documents/admin.html.erb +5 -5
  100. data/app/views/admin/documents/features/_document_references.html.erb +23 -0
  101. data/app/views/admin/documents/features/_multiple_download_links.html.erb +29 -26
  102. data/app/views/admin/ids/fetch.json.jbuilder +0 -2
  103. data/app/views/admin/ids/index.json.jbuilder +0 -2
  104. data/app/views/admin/imports/_form.html.erb +1 -1
  105. data/app/views/admin/imports/show.html.erb +1 -1
  106. data/app/views/admin/layouts/application.html.erb +4 -2
  107. data/app/views/admin/mappings/index.html.erb +1 -1
  108. data/app/views/admin/notifications/_notification.html.haml +29 -28
  109. data/app/views/admin/reference_types/_form.html.erb +25 -0
  110. data/app/views/admin/reference_types/_reference_type.html.erb +52 -0
  111. data/app/views/admin/reference_types/_reference_type.json.jbuilder +2 -0
  112. data/app/views/admin/reference_types/edit.html.erb +12 -0
  113. data/app/views/admin/reference_types/index.html.erb +52 -0
  114. data/app/views/admin/reference_types/index.json.jbuilder +1 -0
  115. data/app/views/admin/reference_types/new.html.erb +11 -0
  116. data/app/views/admin/reference_types/show.html.erb +3 -0
  117. data/app/views/admin/reference_types/show.json.jbuilder +1 -0
  118. data/app/views/admin/shared/_footer.html.erb +5 -2
  119. data/app/views/admin/shared/_js_behaviors.html.erb +2 -3
  120. data/app/views/admin/shared/_navbar.html.erb +9 -2
  121. data/app/views/admin/users/index.html.erb +0 -1
  122. data/app/views/catalog/_show_gbl_admin.html.erb +1 -1
  123. data/config/initializers/defaults.yml +310 -0
  124. data/config/initializers/rails_config.rb +8 -0
  125. data/config/locales/documents.en.yml +14 -0
  126. data/config/routes.rb +30 -5
  127. data/db/import_references_schema_support.numbers +0 -0
  128. data/db/migrate/20230316183001_add_geoblacklight_admin_gem.rb +0 -12
  129. data/db/migrate/20241009200524_create_admin_reference_types.rb +13 -0
  130. data/db/migrate/20241010161420_create_document_references.rb +14 -0
  131. data/db/migrate/20241120238823_rename_references_to_distributions.rb +5 -0
  132. data/db/seeds.rb +5 -0
  133. data/db/seeds_elements.csv +1 -1
  134. data/db/seeds_elements.numbers +0 -0
  135. data/db/seeds_reference_types.csv +29 -0
  136. data/db/seeds_reference_types.numbers +0 -0
  137. data/db/structure.sql +1 -38
  138. data/lib/compose.yml +31 -0
  139. data/lib/generators/geoblacklight_admin/config_generator.rb +53 -12
  140. data/lib/generators/geoblacklight_admin/install_generator.rb +8 -0
  141. data/lib/generators/geoblacklight_admin/templates/btaa_sample_records.csv +5 -0
  142. data/lib/generators/geoblacklight_admin/templates/config/database.yml +1 -1
  143. data/lib/generators/geoblacklight_admin/templates/config/initializers/devise.rb +0 -2
  144. data/lib/generators/geoblacklight_admin/templates/config/initializers/mime_types.rb +1 -0
  145. data/lib/generators/geoblacklight_admin/templates/demo-app/Dockerfile +31 -0
  146. data/lib/generators/geoblacklight_admin/templates/demo-app/compose.yml +42 -0
  147. data/lib/generators/geoblacklight_admin/templates/demo-app/start-server.sh +21 -0
  148. data/lib/geoblacklight_admin/engine.rb +4 -0
  149. data/lib/geoblacklight_admin/rake_task.rb +2 -0
  150. data/lib/geoblacklight_admin/tasks/distributions.rake +69 -0
  151. data/lib/geoblacklight_admin/tasks/images.rake +1 -0
  152. data/lib/geoblacklight_admin/tasks/solr.rake +31 -0
  153. data/lib/geoblacklight_admin/version.rb +1 -1
  154. data/lib/geoblacklight_admin.rb +4 -0
  155. metadata +137 -53
  156. data/app/helpers/import_documents_helper.rb +0 -5
  157. data/app/javascript/entrypoints/engine.js +0 -8
  158. data/config/locales/devise_invitable.en.yml +0 -31
  159. data/lib/generators/geoblacklight_admin/templates/devise/invitations/edit.html.erb +0 -15
  160. data/lib/generators/geoblacklight_admin/templates/devise/invitations/new.html.erb +0 -15
  161. data/lib/generators/geoblacklight_admin/templates/devise/mailer/invitation_instructions.html.erb +0 -11
  162. data/lib/generators/geoblacklight_admin/templates/devise/mailer/invitation_instructions.text.erb +0 -11
@@ -1,6 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Admin::DocumentAccessesController
4
+ #
5
+ # This controller manages the CRUD operations for DocumentAccess objects
6
+ # within the admin namespace. It provides actions to list, show, create,
7
+ # update, and destroy document access records. It also includes custom
8
+ # actions for importing and destroying all document access links.
9
+ #
10
+ # Actions:
11
+ # - index: Lists all document accesses, optionally filtered by document_id.
12
+ # - show: Displays a specific document access.
13
+ # - new: Renders a form for creating a new document access.
14
+ # - edit: Renders a form for editing an existing document access.
15
+ # - create: Creates a new document access.
16
+ # - update: Updates an existing document access.
17
+ # - destroy: Deletes a specific document access.
18
+ # - destroy_all: Deletes all document access links provided in the params.
19
+ # - import: Imports document access links from a file provided in the params.
20
+ #
21
+ # Before Actions:
22
+ # - set_document: Sets the @document instance variable if document_id is present.
23
+ # - set_document_access: Sets the @document_access instance variable for specific actions.
24
+ #
25
+ # Private Methods:
26
+ # - set_document: Finds and sets the document based on the document_id parameter.
27
+ # - set_document_access: Finds and sets the document access based on the id parameter.
28
+ # - document_access_params: Permits only trusted parameters for document access.
4
29
  module Admin
5
30
  class DocumentAccessesController < Admin::AdminController
6
31
  before_action :set_document
@@ -8,6 +33,7 @@ module Admin
8
33
 
9
34
  # GET /documents/#id/access
10
35
  # GET /documents/#id/access.json
36
+ # Lists all document accesses, optionally filtered by document_id.
11
37
  def index
12
38
  if params[:document_id]
13
39
  @document_accesses = DocumentAccess.where(friendlier_id: @document.friendlier_id).order(institution_code: :asc)
@@ -18,20 +44,24 @@ module Admin
18
44
 
19
45
  # GET /document_accesses/1
20
46
  # GET /document_accesses/1.json
47
+ # Displays a specific document access.
21
48
  def show
22
49
  end
23
50
 
24
51
  # GET /document_accesses/new
52
+ # Renders a form for creating a new document access.
25
53
  def new
26
54
  @document_access = DocumentAccess.new
27
55
  end
28
56
 
29
57
  # GET /document_accesses/1/edit
58
+ # Renders a form for editing an existing document access.
30
59
  def edit
31
60
  end
32
61
 
33
62
  # POST /document_accesses
34
63
  # POST /document_accesses.json
64
+ # Creates a new document access.
35
65
  def create
36
66
  @document_access = DocumentAccess.new(document_access_params)
37
67
  logger.debug("DA Params: #{DocumentAccess.new(document_access_params).inspect}")
@@ -52,6 +82,7 @@ module Admin
52
82
 
53
83
  # PATCH/PUT /document_accesses/1
54
84
  # PATCH/PUT /document_accesses/1.json
85
+ # Updates an existing document access.
55
86
  def update
56
87
  respond_to do |format|
57
88
  if @document_access.update(document_access_params)
@@ -68,6 +99,7 @@ module Admin
68
99
 
69
100
  # DELETE /document_accesses/1
70
101
  # DELETE /document_accesses/1.json
102
+ # Deletes a specific document access.
71
103
  def destroy
72
104
  @document_access.destroy
73
105
  respond_to do |format|
@@ -78,6 +110,8 @@ module Admin
78
110
  end
79
111
  end
80
112
 
113
+ # DELETE /document_accesses/destroy_all
114
+ # Deletes all document access links provided in the params.
81
115
  def destroy_all
82
116
  logger.debug("Destroy Access Links")
83
117
  return unless params.dig(:document_access, :assets, :file)
@@ -95,6 +129,7 @@ module Admin
95
129
 
96
130
  # GET /documents/#id/access/import
97
131
  # POST /documents/#id/access/import
132
+ # Imports document access links from a file provided in the params.
98
133
  def import
99
134
  logger.debug("Import Action")
100
135
  return unless params.dig(:document_access, :assets, :file)
@@ -113,17 +148,20 @@ module Admin
113
148
  private
114
149
 
115
150
  # Use callbacks to share common setup or constraints between actions.
151
+ # Finds and sets the document based on the document_id parameter.
116
152
  def set_document
117
153
  return unless params[:document_id] # If not nested
118
154
 
119
155
  @document = Document.includes(:leaf_representative).find_by!(friendlier_id: params[:document_id])
120
156
  end
121
157
 
158
+ # Finds and sets the document access based on the id parameter.
122
159
  def set_document_access
123
160
  @document_access = DocumentAccess.find(params[:id])
124
161
  end
125
162
 
126
163
  # Only allow a list of trusted parameters through.
164
+ # Permits only trusted parameters for document access.
127
165
  def document_access_params
128
166
  params.require(:document_access).permit(:friendlier_id, :institution_code, :access_url)
129
167
  end
@@ -1,11 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Admin::DocumentAssetsController
4
+ #
5
+ # This controller manages the document assets within the admin namespace.
6
+ # It provides actions to list, show, edit, update, destroy, and attach files
7
+ # to document assets.
8
+ #
9
+ # Before Actions:
10
+ # - set_document: Sets the document based on the document_id parameter.
11
+ # - set_document_asset: Sets the document asset based on the id parameter for specific actions.
4
12
  module Admin
5
13
  class DocumentAssetsController < Admin::AdminController
6
14
  before_action :set_document
7
15
  before_action :set_document_asset, only: %i[show edit update destroy]
8
16
 
17
+ # GET /admin/document_assets
18
+ #
19
+ # Lists all document assets. If a document_id is provided, it filters
20
+ # the assets by the specified document.
9
21
  def index
10
22
  scope = Asset
11
23
 
@@ -21,6 +33,10 @@ module Admin
21
33
  @document_assets = scope
22
34
  end
23
35
 
36
+ # GET /admin/document_assets/:id
37
+ #
38
+ # Shows a specific document asset. If the asset is stored, it also
39
+ # retrieves the fixity checks associated with the asset.
24
40
  def show
25
41
  @asset = Asset.find_by_friendlier_id!(params[:id])
26
42
 
@@ -31,17 +47,23 @@ module Admin
31
47
  @earliest_check = @checks.first
32
48
  end
33
49
 
50
+ # GET /admin/document_assets/:id/edit
51
+ #
52
+ # Renders the edit form for a specific document asset.
34
53
  def edit
35
54
  end
36
55
 
37
- # PATCH/PUT /works/1
38
- # PATCH/PUT /works/1.json
56
+ # PATCH/PUT /admin/document_assets/:id
57
+ #
58
+ # Updates a specific document asset. If successful, redirects to the
59
+ # document assets index with a success notice. Otherwise, re-renders
60
+ # the edit form with errors.
39
61
  def update
40
- @document_asset = DocumentAsset.find_by_friendlier_id!(params[:id])
62
+ @document_asset = Asset.find_by_friendlier_id!(params[:id])
41
63
 
42
64
  respond_to do |format|
43
65
  if @document_asset.update(document_asset_params)
44
- format.html { redirect_to admin_document_document_assets_path(@document, @document_asset.parent), notice: "Asset was successfully updated." }
66
+ format.html { redirect_to admin_document_document_assets_path(@document), notice: "Asset was successfully updated." }
45
67
  format.json { render :show, status: :ok, location: @document_asset }
46
68
  else
47
69
  format.html { render :edit }
@@ -50,6 +72,10 @@ module Admin
50
72
  end
51
73
  end
52
74
 
75
+ # DELETE /admin/document_assets/:id
76
+ #
77
+ # Destroys a specific document asset. Redirects to the document assets
78
+ # index with a success notice.
53
79
  def destroy
54
80
  @asset = Asset.find_by_friendlier_id!(params[:id])
55
81
  @asset.destroy
@@ -63,25 +89,17 @@ module Admin
63
89
  end
64
90
  end
65
91
 
66
- def check_fixity
67
- @asset = Asset.find_by_friendlier_id!(params[:asset_id])
68
- SingleAssetCheckerJob.perform_later(@asset)
69
- redirect_to admin_asset_url(@asset), notice: "This file will be checked shortly."
70
- end
71
-
72
- def fixity_report
73
- @fixity_report = FixityReport.new
74
- end
75
-
92
+ # GET /admin/document_assets/display_attach_form
93
+ #
94
+ # Displays the form to attach files to a document.
76
95
  def display_attach_form
77
96
  @document = Document.find_by_friendlier_id!(params[:document_id])
78
97
  end
79
98
 
80
- # Receives json hashes for direct uploaded files in params[:files],
81
- # and id in params[:id] (friendlier_id)
82
- # creates filesets for them and attach.
83
- #
84
99
  # POST /document/:id/ingest
100
+ #
101
+ # Attaches files to a document. Receives JSON hashes for direct uploaded
102
+ # files in params[:files] and creates filesets for them.
85
103
  def attach_files
86
104
  @parent = Document.find_by_friendlier_id!(params[:id])
87
105
 
@@ -114,77 +132,11 @@ module Admin
114
132
  redirect_to admin_document_document_assets_path(@parent.friendlier_id, anchor: "nav-members")
115
133
  end
116
134
 
117
- def convert_to_child_work
118
- @asset = Asset.find_by_friendlier_id!(params[:id])
119
-
120
- parent = @asset.parent
121
-
122
- new_child = Work.new(title: @asset.title)
123
-
124
- # Asking for permission to create a new Work,
125
- # which is arguably the main thing going on in this method.
126
- # authorize! :create, Work as the first line of the method
127
- # would be better, but we currently aren't allowed to do that
128
- # see (https://github.com/chaps-io/access-granted/pull/56).
129
- authorize! :create, new_child
130
-
131
- new_child.parent = parent
132
- # collections
133
- new_child.contained_by = parent.contained_by
134
- new_child.position = @asset.position
135
- new_child.representative = @asset
136
- # we can copy _all_ the non-title metadata like this...
137
- new_child.json_attributes = parent.json_attributes
138
-
139
- @asset.parent = new_child
140
-
141
- Kithe::Model.transaction do
142
- new_child.save!
143
- @asset.save! # to get new parent
144
-
145
- if parent.representative_id == @asset.id
146
- parent.representative = new_child
147
- parent.save!
148
- end
149
- end
150
-
151
- redirect_to edit_admin_work_path(new_child), notice: "Asset promoted to child work #{new_child.title}"
152
- end
153
-
154
- # requires params[:active_encode_status_id]
155
- def refresh_active_encode_status
156
- status = ActiveEncodeStatus.find(params[:active_encode_status_id])
157
-
158
- RefreshActiveEncodeStatusJob.perform_later(status)
159
-
160
- redirect_to admin_asset_url(status.asset),
161
- notice: "Started refresh for ActiveEncode job #{status.active_encode_id}"
162
- end
163
-
164
- def work_is_oral_history?
165
- (@asset.parent.is_a? Work) && @asset.parent.genre && @asset.parent.genre.include?("Oral histories")
166
- end
167
- helper_method :work_is_oral_history?
168
-
169
- def asset_is_collection_thumbnail?
170
- @asset.parent.is_a? Collection
171
- end
172
- helper_method :asset_is_collection_thumbnail?
173
-
174
- def edit_path(asset)
175
- asset.parent.is_a? Collection ? edit_admin_collection_path(asset.parent) : edit_admin_asset_path(asset)
176
- end
177
- helper_method :edit_path
178
-
179
- def parent_path(asset)
180
- return nil if asset.parent.nil?
181
-
182
- asset.parent.is_a? Collection ? collection_path(asset.parent) : admin_work_path(asset.parent)
183
- end
184
- helper_method :parent_path
185
-
186
135
  private
187
136
 
137
+ # Strong parameters for asset.
138
+ #
139
+ # Returns a list of permitted parameters for asset creation and update.
188
140
  def asset_params
189
141
  allowed_params = [:title, :derivative_storage_type, :alt_text, :caption,
190
142
  :transcription, :english_translation,
@@ -193,19 +145,25 @@ module Admin
193
145
  params.require(:asset).permit(*allowed_params)
194
146
  end
195
147
 
148
+ # Sets the document based on the document_id parameter.
149
+ #
150
+ # If the document_id is not present, it does nothing.
196
151
  def set_document
197
152
  return unless params[:document_id] # If not nested
198
153
 
199
154
  @document = Document.includes(:leaf_representative).find_by!(friendlier_id: params[:document_id])
200
155
  end
201
156
 
157
+ # Sets the document asset based on the id parameter.
202
158
  def set_document_asset
203
159
  @document_asset = DocumentAsset.find_by_friendlier_id(params[:id])
204
160
  end
205
161
 
206
- # Only allow a list of trusted parameters through.
162
+ # Strong parameters for document asset.
163
+ #
164
+ # Returns a list of permitted parameters for document asset update.
207
165
  def document_asset_params
208
- params.require(:asset).permit(:title, :label, :dct_references_uri_key)
166
+ params.require(:asset).permit(:parent_id, :title, :label, :dct_references_uri_key, :thumbnail)
209
167
  end
210
168
  end
211
169
  end
@@ -0,0 +1,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Admin::DocumentDistributionsController
4
+ #
5
+ # This controller manages the CRUD operations for Document Distributions within the Admin namespace.
6
+ # It includes actions for listing, showing, creating, updating, and deleting document distributions.
7
+ # Additionally, it provides functionality for importing and destroying all distributions.
8
+ module Admin
9
+ class DocumentDistributionsController < Admin::AdminController
10
+ before_action :set_document
11
+ before_action :set_document_distribution, only: %i[show edit update destroy]
12
+
13
+ # GET /admin/document_distributions or /admin/document_distributions.json
14
+ #
15
+ # Lists all document distributions. If a document_id is provided, it lists distributions
16
+ # associated with that document, ordered by position. Otherwise, it paginates all
17
+ # document distributions.
18
+ def index
19
+ @document_distributions = DocumentDistribution.all
20
+ if params[:document_id]
21
+ @document_distributions = DocumentDistribution.where(friendlier_id: @document.friendlier_id).order(position: :asc)
22
+ else
23
+ @pagy, @document_distributions = pagy(DocumentDistribution.all.order(friendlier_id: :asc, updated_at: :desc), items: 20)
24
+ end
25
+ end
26
+
27
+ # GET /admin/document_distributions/1 or /admin/document_distributions/1.json
28
+ #
29
+ # Shows a specific document distribution.
30
+ def show
31
+ end
32
+
33
+ # GET /admin/document_references/new
34
+ #
35
+ # Initializes a new document distribution.
36
+ def new
37
+ @document_distribution = DocumentDistribution.new
38
+ end
39
+
40
+ # GET /admin/document_distributions/1/edit
41
+ #
42
+ # Edits an existing document distribution.
43
+ def edit
44
+ end
45
+
46
+ # POST /admin/document_distributions or /admin/document_distributions.json
47
+ #
48
+ # Creates a new document distribution. If successful, redirects to the document distributions
49
+ # list with a success notice. Otherwise, renders the new form with errors.
50
+ def create
51
+ @document_distribution = DocumentDistribution.new(document_distribution_params)
52
+
53
+ respond_to do |format|
54
+ if @document_distribution.save
55
+ format.html { redirect_to admin_document_document_distributions_path(@document), notice: "Document distribution was successfully created." }
56
+ format.json { render :show, status: :created, location: @document_distribution }
57
+ else
58
+ format.html { render :new, status: :unprocessable_entity }
59
+ format.json { render json: @document_distribution.errors, status: :unprocessable_entity }
60
+ end
61
+ end
62
+ end
63
+
64
+ # PATCH/PUT /admin/document_distributions/1 or /admin/document_distributions/1.json
65
+ #
66
+ # Updates an existing document distribution. If successful, redirects to the document distribution
67
+ # with a success notice. Otherwise, renders the edit form with errors.
68
+ def update
69
+ respond_to do |format|
70
+ if @document_distribution.update(document_distribution_params)
71
+ format.html { redirect_to admin_document_document_distributions_path(@document), notice: "Document distribution was successfully updated." }
72
+ format.json { render :show, status: :ok, location: @document_distribution }
73
+ else
74
+ format.html { render :edit, status: :unprocessable_entity }
75
+ format.json { render json: @document_distribution.errors, status: :unprocessable_entity }
76
+ end
77
+ end
78
+ end
79
+
80
+ # DELETE /admin/document_distributions/1 or /admin/document_distributions/1.json
81
+ #
82
+ # Deletes a document distribution. Redirects to the document distributions list with a success notice.
83
+ def destroy
84
+ @document_distribution.destroy!
85
+
86
+ respond_to do |format|
87
+ format.html { redirect_to admin_document_document_distributions_path(@document), status: :see_other, notice: "Document distribution was successfully destroyed." }
88
+ format.json { head :no_content }
89
+ end
90
+ end
91
+
92
+ # DELETE /admin/document_distributions/destroy_all
93
+ #
94
+ # Destroys all document distributions provided in the file parameter. If successful, redirects
95
+ # with a success notice. Otherwise, redirects with an error notice.
96
+ def destroy_all
97
+ return if request.get?
98
+
99
+ logger.debug("Destroy Distributions")
100
+ unless params.dig(:document_distribution, :distributions, :file)
101
+ raise ArgumentError, "File does not exist or is invalid."
102
+ end
103
+
104
+ respond_to do |format|
105
+ if DocumentDistribution.destroy_all(params.dig(:document_distribution, :distributions, :file))
106
+ format.html { redirect_to admin_document_distributions_path, notice: "Distributions were destroyed." }
107
+ else
108
+ format.html { redirect_to admin_document_distributions_path, notice: "Distributions could not be destroyed." }
109
+ end
110
+ rescue => e
111
+ format.html { redirect_to admin_document_distributions_path, notice: "Distributions could not be destroyed. #{e}" }
112
+ end
113
+ end
114
+
115
+ # GET/POST /documents/1/distributions/import
116
+ #
117
+ # Imports document distributions from a file. If successful, redirects with a success notice.
118
+ # Otherwise, redirects with an error notice.
119
+ def import
120
+ return if request.get?
121
+
122
+ logger.debug("Import Distributions")
123
+
124
+ unless params.dig(:document_distribution, :distributions, :file)
125
+ raise ArgumentError, "File does not exist or is invalid."
126
+ end
127
+
128
+ if DocumentDistribution.import(params.dig(:document_distribution, :distributions, :file))
129
+ logger.debug("Distributions were created successfully.")
130
+ if params[:document_id]
131
+ redirect_to admin_document_document_distributions_path(@document), notice: "Distributions were created successfully."
132
+ else
133
+ redirect_to admin_document_document_distributions_path, notice: "Distributions were created successfully."
134
+ end
135
+ else
136
+ logger.debug("Distributions could not be created.")
137
+ if params[:document_id]
138
+ redirect_to admin_document_document_distributions_path(@document), warning: "Distributions could not be created."
139
+ else
140
+ redirect_to admin_document_document_distributions_path, warning: "Distributions could not be created."
141
+ end
142
+ end
143
+ rescue => e
144
+ logger.debug("Distributions could not be created. #{e}")
145
+ if params[:document_id]
146
+ redirect_to admin_document_document_distributions_path(@document), notice: "Distributions could not be created. #{e}"
147
+ else
148
+ redirect_to admin_document_document_distributions_path, notice: "Distributions could not be created. #{e}"
149
+ end
150
+ end
151
+
152
+ private
153
+
154
+ # Sets the document based on the document_id parameter.
155
+ # If not nested, it does nothing.
156
+ def set_document
157
+ return unless params[:document_id] # If not nested
158
+
159
+ @document = Document.includes(:leaf_representative).find_by!(friendlier_id: params[:document_id])
160
+ end
161
+
162
+ # Sets the document distribution based on the id parameter.
163
+ def set_document_distribution
164
+ @document_distribution = DocumentDistribution.find(params[:id])
165
+ end
166
+
167
+ # Permits only the trusted parameters for document distributions.
168
+ def document_distribution_params
169
+ params.require(:document_distribution).permit(:friendlier_id, :reference_type_id, :url, :label, :position)
170
+ end
171
+ end
172
+ end