Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPIKE] Create mutable models facade over cocina models #3323

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/components/contents/file_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<%= filename %>
<% end %>
</td>
<td><%= hasMimeType %></td>
<td><%= mime_type %></td>
<td><%= number_to_human_size(size) %></td>
<% if image? %>
<td><%= height %></td>
Expand All @@ -15,7 +15,7 @@
<td><%= role %></td>
<td class="ps-4"><%= tag :span, class: 'bi-check' if publish %></td>
<td class="ps-4"><%= tag :span, class: 'bi-check' if shelve %></td>
<td class="ps-4"><%= tag :span, class: 'bi-check' if sdrPreserve %></td>
<td class="ps-4"><%= tag :span, class: 'bi-check' if preserve %></td>
<td><%= view_access %></td>
<td><%= download_access %></td>
</tr>
15 changes: 5 additions & 10 deletions app/components/contents/file_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ def image?
@image
end

delegate :access, :administrative, :filename, :hasMimeType, :size, :externalIdentifier, :use, :presentation, to: :file
delegate :publish, :shelve, :sdrPreserve, to: :administrative
delegate :filename, :mime_type, :size, :use, :publish, :shelve, :preserve, to: :file

def view_access
access.view.capitalize
file.view_access.capitalize
end

def download_access
access.download.capitalize
file.download_access.capitalize
end

def role
Expand All @@ -41,15 +40,11 @@ def link_attrs
end

def height
return '' if presentation&.height.blank?

"#{presentation.height} px"
"#{file.height} px" if file.height
end

def width
return '' if presentation&.width.blank?

"#{presentation.width} px"
"#{file.width} px" if file.width
end
end
end
7 changes: 2 additions & 5 deletions app/components/contents/resource_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Contents
class ResourceComponent < ViewComponent::Base
# @param [FileSet] resource
def initialize(resource:, resource_counter:, object_id:, viewable:)
@resource = resource
@resource_counter = resource_counter
Expand All @@ -23,10 +24,6 @@ def type
resource.type.delete_prefix('https://cocina.sul.stanford.edu/models/resources/')
end

delegate :label, to: :resource

def files
resource.structural.contains
end
delegate :label, :files, to: :resource
end
end
4 changes: 2 additions & 2 deletions app/components/contents/structural_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<ul class="resource-list">
<%= render Contents::ResourceComponent.with_collection(structural.contains, object_id: object_id, viewable: viewable?) %>
<%= render Contents::ExternalFileComponent.with_collection(structural.hasMemberOrders.first&.members) %>
<%= render Contents::ResourceComponent.with_collection(item.file_sets, object_id: object_id, viewable: viewable?) %>
<%= render Contents::ExternalFileComponent.with_collection(item.members) %>
</ul>
11 changes: 5 additions & 6 deletions app/components/contents/structural_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

module Contents
class StructuralComponent < ViewComponent::Base
# @param [Cocina::Models::DroStructural] structural
# @param [String] object_id the identifier of the object
# @param [Item] item
# @param [Bool] viewable if true the user will be presented with a link to download files
def initialize(structural:, object_id:, viewable:)
@structural = structural
def initialize(item:, viewable:)
@item = item
@viewable = viewable
@object_id = object_id
@object_id = item.id
end

attr_reader :structural, :object_id
attr_reader :item, :object_id

def viewable?
@viewable
Expand Down
6 changes: 3 additions & 3 deletions app/components/contents_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

class ContentsComponent < ApplicationComponent
def initialize(presenter:)
@item = presenter.item
@document = presenter.document
@cocina = presenter.cocina
@state_service = presenter.state_service
@view_token = presenter.view_token
end

def render?
@cocina.respond_to?(:structural)
@item.is_a? Item
end

def number_of_file_sets
@cocina.structural.contains.size
@item.file_sets.size
end

delegate :allows_modification?, to: :@state_service
Expand Down
2 changes: 1 addition & 1 deletion app/components/document_title_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<header class="document-header row mb-5 pb-3">
<%= render OpenCloseComponent.new(id: @document.id) if helpers.can?(:manage_item, @presenter.cocina) %>
<%= render OpenCloseComponent.new(id: @document.id) if helpers.can?(:manage_item, @presenter.item) %>

<%= content_tag @as, class: @classes do %>
<%= title -%>
Expand Down
2 changes: 1 addition & 1 deletion app/components/show/admin_policy_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="col">
<%= render Show::ExternalLinksComponent.new(document: document) %>
<%= render Show::ControlsComponent.new(presenter: presenter,
manager: helpers.can?(:manage_item, cocina)) %>
manager: helpers.can?(:manage_item, item)) %>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/components/show/admin_policy_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def initialize(presenter:)

attr_reader :presenter

delegate :document, :cocina, :view_token, to: :presenter
delegate :document, :item, :view_token, to: :presenter
end
end
2 changes: 1 addition & 1 deletion app/components/show/agreement_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="col">
<%= render Show::ExternalLinksComponent.new(document: document) %>
<%= render Show::ControlsComponent.new(presenter: presenter,
manager: helpers.can?(:manage_item, presenter.cocina)) %>
manager: helpers.can?(:manage_item, presenter.item)) %>
</div>
</div>

Expand Down
4 changes: 2 additions & 2 deletions app/components/show/apo/default_object_rights_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class DefaultObjectRightsComponent < ApplicationComponent
# @param [ArgoShowPresenter] presenter
def initialize(presenter:)
@presenter = presenter
@default_access = @presenter.cocina.administrative.accessTemplate
@default_access = @presenter.item.access_template
end

def access_rights
Expand All @@ -22,7 +22,7 @@ def license
end

def use_and_reproduction
@default_access.useAndReproductionStatement || 'None'
@default_access.use_statement || 'None'
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/components/show/apo/overview_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class OverviewComponent < ApplicationComponent
def initialize(presenter:)
@presenter = presenter
@solr_document = presenter.document
@registration_workflow = presenter.cocina.administrative.registrationWorkflow
@registration_workflows = presenter.item.registration_workflows
end

def registration_workflow
@registration_workflow.present? ? @registration_workflow.join(', ') : 'None'
@registration_workflows.present? ? @registration_workflows.join(', ') : 'None'
end

delegate :id, :status, to: :@solr_document
Expand Down
6 changes: 3 additions & 3 deletions app/components/show/apo/roles_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ def permissions
private

def model
@presenter.cocina
@presenter.item
end

def manage_permissions
manage_role = model.administrative.roles&.find { |role| role.name == 'dor-apo-manager' }
manage_role = model.roles&.find { |role| role.name == 'dor-apo-manager' }
managers = manage_role ? manage_role.members.map { |member| "#{member.type}:#{member.identifier}" } : []
build_permissions(managers, 'manage')
end

def view_permissions
view_role = model.administrative.roles&.find { |role| role.name == 'dor-apo-viewer' }
view_role = model.roles&.find { |role| role.name == 'dor-apo-viewer' }
viewers = view_role ? view_role.members.map { |member| "#{member.type}:#{member.identifier}" } : []
build_permissions(viewers, 'view')
end
Expand Down
2 changes: 1 addition & 1 deletion app/components/show/collection_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="col">
<%= render Show::ExternalLinksComponent.new(document: document) %>
<%= render Show::ControlsComponent.new(presenter: presenter,
manager: helpers.can?(:manage_item, cocina)) %>
manager: helpers.can?(:manage_item, item)) %>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/components/show/collection_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def initialize(presenter:)

attr_reader :presenter

delegate :document, :cocina, :view_token, to: :presenter
delegate :document, :item, :view_token, to: :presenter
end
end
2 changes: 1 addition & 1 deletion app/components/show/item/details_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(presenter:)
end

def render?
!@presenter.cocina.is_a? NilModel
@presenter.item
end

delegate :object_type, :created_date, :preservation_size, to: :@solr_document
Expand Down
2 changes: 1 addition & 1 deletion app/components/show/item/overview_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(presenter:)
end

def render?
!@presenter.cocina.is_a? NilModel
@presenter.item
end

delegate :id, :status, to: :@solr_document
Expand Down
2 changes: 1 addition & 1 deletion app/components/show/item_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="col-md-9">
<%= render Show::ExternalLinksComponent.new(document: document) %>
<%= render Show::ControlsComponent.new(presenter: presenter,
manager: helpers.can?(:manage_item, presenter.cocina)) %>
manager: helpers.can?(:manage_item, presenter.item)) %>
</div>
</div>

Expand Down
10 changes: 5 additions & 5 deletions app/components/workflow_process_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
class WorkflowProcessRow < ApplicationComponent
# @param [Dor::Workflow::Response::Process] process_status the model for the WorkflowProcess
# @param [Integer] index the row index
# @param [Cocina::Models::DRO,Cocina::Models::Collection] cocina_object the repository object that the workflow is about
def initialize(process:, index:, cocina_object:)
# @param [Item,Collection] item the repository object that the workflow is about
def initialize(process:, index:, item:)
@process = process
@index = index
@cocina_object = cocina_object
@item = item
end

delegate :druid, :workflow_name, :repository, :name, :status, :datetime,
Expand All @@ -25,8 +25,8 @@ def error?
end

def show_reset_button?
error? && can?(:manage_item, cocina_object)
error? && can?(:manage_item, item)
end

attr_reader :process, :index, :cocina_object
attr_reader :process, :index, :item
end
4 changes: 2 additions & 2 deletions app/controllers/apo_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ def edit
end

def new
authorize! :create, Cocina::Models::AdminPolicy
authorize! :create, AdminPolicy
@form = ApoForm.new(nil, search_service: search_service)

render layout: 'one_column'
end

def create
authorize! :create, Cocina::Models::AdminPolicy
authorize! :create, AdminPolicy
@form = ApoForm.new(nil, search_service: search_service)
unless @form.validate(params.require(:apo).to_unsafe_h.merge(registered_by: current_user.login))
respond_to do |format|
Expand Down
12 changes: 5 additions & 7 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class ApplicationController < ActionController::Base

layout :determine_layout

def allows_modification?(cocina_object)
state_service = StateService.new(cocina_object)
def allows_modification?(item)
state_service = StateService.new(item)
state_service.allows_modification?
end

Expand All @@ -36,13 +36,11 @@ def default_html_head

protected

def enforce_versioning
return redirect_to solr_document_path(@cocina.externalIdentifier), flash: { error: 'Unable to retrieve the cocina model' } if @cocina.is_a? NilModel

def enforce_versioning(item = @item)
# if this object has been submitted and doesn't have an open version, they cannot change it.
return true if allows_modification?(@cocina)
return true if allows_modification?(item)

redirect_to solr_document_path(@cocina.externalIdentifier), flash: { error: 'Object cannot be modified in its current state.' }
redirect_to solr_document_path(item.id), flash: { error: 'Object cannot be modified in its current state.' }
false
end
end
9 changes: 6 additions & 3 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,13 @@ def show
params[:id] = Druid.new(params[:id]).with_namespace
_deprecated_response, @document = search_service.fetch(params[:id])

@cocina = Repository.find(params[:id])
flash[:alert] = 'Warning: this object cannot currently be represented in the Cocina model.' if @cocina.instance_of?(NilModel)
begin
@item = Repository.find(params[:id])
rescue Repository::NotCocina
flash[:alert] = 'Warning: this object cannot currently be represented in the Cocina model.'
end

authorize! :view_metadata, @cocina
authorize! :view_metadata, @item

@workflows = WorkflowService.workflows_for(druid: params[:id])

Expand Down
14 changes: 7 additions & 7 deletions app/controllers/catkeys_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CatkeysController < ApplicationController
end

def edit
@change_set = CatkeyForm.new(@cocina)
@change_set = CatkeyForm.new(@item)
respond_to do |format|
format.html { render layout: !request.xhr? }
end
Expand All @@ -20,20 +20,20 @@ def edit
def update
return unless enforce_versioning

form = CatkeyForm.new(@cocina)
form = CatkeyForm.new(@item)
form.validate(catkey: update_params[:catkey].strip)
form.save
Argo::Indexer.reindex_druid_remotely(@cocina.externalIdentifier)
Argo::Indexer.reindex_druid_remotely(@item.id)

msg = "Catkey for #{@cocina.externalIdentifier} has been updated!"
redirect_to solr_document_path(@cocina.externalIdentifier), notice: msg
msg = "Catkey for #{@item.id} has been updated!"
redirect_to solr_document_path(@item.id), notice: msg
end

private

def load_and_authorize_resource
@cocina = Repository.find(params[:item_id])
authorize! :manage_item, @cocina
@item = Repository.find(params[:item_id])
authorize! :manage_item, @item
end

def update_params
Expand Down
Loading