Skip to content

Commit

Permalink
I741 fix adventist valkyrie lint (#754)
Browse files Browse the repository at this point in the history
# Story

The Lint step is failing on the adventist pipeline. Run rubocop and
correct the reported errors.

Refs

- [ ] #741

# Expected Behavior Before Changes

Linting does not pass.

# Expected Behavior After Changes

Linting pipeline should pass.

# Screenshots / Video

# Notes

- Run `bundle exec rubocop` in the `samvera/` directory. All tests
should pass.
  • Loading branch information
sjproctor authored Aug 19, 2024
2 parents 58daa68 + b474be9 commit 598f02f
Show file tree
Hide file tree
Showing 144 changed files with 1,423 additions and 607 deletions.
22 changes: 20 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ AllCops:
# NOTE: When we run knapsack's rubocop, we don't want to check the submodule
# for Hyku. We'll assume it's okay and has it's own policing policies.
- "hyrax-webapp/**/*"
- 'bin/**/*'
- 'db/**/*'
- 'config/**/*'
- 'vendor/**/*'
- 'lib/tasks/rubocop.rake'

Layout/HashAlignment:
Exclude:
- app/forms/hyrax/forms/admin/adl_appearance_decorator.rb

Metrics/BlockLength:
IgnoredMethods: ['included', 'describe', 'it', 'context']
AllowedMethods: ['included', 'describe', 'it', 'context']
Exclude:
- "spec/**/*.rb"
- app/controllers/catalog_controller_decorator.rb

Metrics/ModuleLength:
Exclude:
- lib/dog_biscuits.rb
- app/models/solr_document_decorator.rb

Style/AsciiComments:
Enabled: false
Expand Down Expand Up @@ -75,4 +90,7 @@ RSpec/NestedGroups:
Enabled: false

RSpec/LeadingSubject:
Enabled: false
Enabled: false

RSpec/ExampleLength:
Max: 16
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Metrics/CyclomaticComplexity:
# Offense count: 14
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Layout/LineLength:
Max: 161

# Offense count: 24
Expand Down
185 changes: 91 additions & 94 deletions app/actors/hyrax/actors/collections_membership_actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,106 +30,103 @@ def update(env)

private

##
# Attaches any unattached members. Deletes those that are marked _delete
#
# @param env [Hyrax::Actors::Enviornment]
# @return [Boolean]
#
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/CyclomaticComplexity
def assign_nested_attributes_for_collection(env)
attributes_collection = env.attributes.delete(:member_of_collections_attributes)
return true unless attributes_collection

# OVERRIDE Hyrax 3.5.0 to skip permission checks if importing
# rubocop:disable Metrics/LineLength
return false unless env.importing ||
valid_membership?(env,
collection_ids: attributes_collection.map { |_, attributes| attributes['id'] })

# rubocop:enable Metrics/LineLength
attributes_collection = attributes_collection.sort_by { |i, _| i.to_i }.map { |_, attributes| attributes }
# checking for existing works to avoid rewriting/loading works that are already attached
existing_collections = env.curation_concern.member_of_collection_ids
boolean_type_caster = ActiveModel::Type::Boolean.new
attributes_collection.each do |attributes|
next if attributes['id'].blank?
if boolean_type_caster.cast(attributes['_destroy'])
# Likely someone in the UI sought to add the collection, then
# changed their mind and checked the "delete" checkbox and posted
# their update.
next unless existing_collections.include?(attributes['id'])
remove(env.curation_concern, attributes['id'])
else
# Let's not try to add an item already
next if existing_collections.include?(attributes['id'])
add(env, attributes['id'])
end
##
# Attaches any unattached members. Deletes those that are marked _delete
#
# @param env [Hyrax::Actors::Enviornment]
# @return [Boolean]
#
# rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity, Metrics/MethodLength
def assign_nested_attributes_for_collection(env)
attributes_collection = env.attributes.delete(:member_of_collections_attributes)
return true unless attributes_collection

# OVERRIDE Hyrax 3.5.0 to skip permission checks if importing
# rubocop:disable Layout/LineLength
return false unless env.importing ||
valid_membership?(env,
collection_ids: attributes_collection.map { |_, attributes| attributes['id'] })

# rubocop:enable Layout/LineLength
attributes_collection = attributes_collection.sort_by { |i, _| i.to_i }.map { |_, attributes| attributes }
# checking for existing works to avoid rewriting/loading works that are already attached
existing_collections = env.curation_concern.member_of_collection_ids
boolean_type_caster = ActiveModel::Type::Boolean.new
attributes_collection.each do |attributes|
next if attributes['id'].blank?
if boolean_type_caster.cast(attributes['_destroy'])
# Likely someone in the UI sought to add the collection, then
# changed their mind and checked the "delete" checkbox and posted
# their update.
next unless existing_collections.include?(attributes['id'])
remove(env.curation_concern, attributes['id'])
else
# Let's not try to add an item already
next if existing_collections.include?(attributes['id'])
add(env, attributes['id'])
end
true
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity

# Adds the item to the ordered members so that it displays in the items
# along side the FileSets on the show page
def add(env, id)
collection = Hyrax.config.collection_class.find(id)
collection.try(:reindex_extent=, Hyrax::Adapters::NestingIndexAdapter::LIMITED_REINDEX)

return unless env.current_ability.can?(:deposit, collection)
env.curation_concern.member_of_collections << collection
end
true
end
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

# Remove the object from the members set and the ordered members list
def remove(curation_concern, id)
collection = Hyrax.config.collection_class.find(id)
curation_concern.member_of_collections.delete(collection)
end
# Adds the item to the ordered members so that it displays in the items
# along side the FileSets on the show page
def add(env, id)
collection = Hyrax.config.collection_class.find(id)
collection.try(:reindex_extent=, Hyrax::Adapters::NestingIndexAdapter::LIMITED_REINDEX)

# Extact a singleton collection id from the collection attributes and save it in env. Later in the actor stack,
# in apply_permission_template_actor.rb, `env.attributes[:collection_id]` will be used to apply the
# permissions of the collection to the created work. With one and only one collection, the work is seen as
# being created directly in that collection. The permissions will not be applied to the work if the collection
# type is configured not to allow that or if the work is being created in more than one collection.
#
# @param env [Hyrax::Actors::Enviornment]
#
# Given an array of collection_attributes when it is size:
# * 0 do not set `env.attributes[:collection_id]`
# * 1 set `env.attributes[:collection_id]` to the one and only one collection
# * 2+ do not set `env.attributes[:collection_id]`
#
# NOTE: Only called from create. All collections are being added as parents of a work. None are being removed.
def extract_collection_id(env)
attributes_collection =
env.attributes.fetch(:member_of_collections_attributes) { nil }

# Determine if the work is being created in one and only one collection.
return unless attributes_collection && attributes_collection.size == 1

# Extract the collection id from attributes_collection,
collection_id = attributes_collection.first.second['id']

# Do not apply permissions to work if collection type is configured not to
collection = Hyrax.config.collection_class.find(collection_id)
return unless collection.share_applies_to_new_works?

# Save the collection id in env for use in apply_permission_template_actor
env.attributes[:collection_id] = collection_id
end
return unless env.current_ability.can?(:deposit, collection)
env.curation_concern.member_of_collections << collection
end

def valid_membership?(env, collection_ids:)
checker = Hyrax::MultipleMembershipChecker.new(item: env.curation_concern)
multiple_memberships = checker.check(collection_ids: collection_ids)
if multiple_memberships
env.curation_concern.errors.add(:collections, multiple_memberships)
return false
end
true
# Remove the object from the members set and the ordered members list
def remove(curation_concern, id)
collection = Hyrax.config.collection_class.find(id)
curation_concern.member_of_collections.delete(collection)
end

# Extact a singleton collection id from the collection attributes and save it in env. Later in the actor stack,
# in apply_permission_template_actor.rb, `env.attributes[:collection_id]` will be used to apply the
# permissions of the collection to the created work. With one and only one collection, the work is seen as
# being created directly in that collection. The permissions will not be applied to the work if the collection
# type is configured not to allow that or if the work is being created in more than one collection.
#
# @param env [Hyrax::Actors::Enviornment]
#
# Given an array of collection_attributes when it is size:
# * 0 do not set `env.attributes[:collection_id]`
# * 1 set `env.attributes[:collection_id]` to the one and only one collection
# * 2+ do not set `env.attributes[:collection_id]`
#
# NOTE: Only called from create. All collections are being added as parents of a work. None are being removed.
def extract_collection_id(env)
attributes_collection =
env.attributes.fetch(:member_of_collections_attributes) { nil }

# Determine if the work is being created in one and only one collection.
return unless attributes_collection && attributes_collection.size == 1

# Extract the collection id from attributes_collection,
collection_id = attributes_collection.first.second['id']

# Do not apply permissions to work if collection type is configured not to
collection = Hyrax.config.collection_class.find(collection_id)
return unless collection.share_applies_to_new_works?

# Save the collection id in env for use in apply_permission_template_actor
env.attributes[:collection_id] = collection_id
end

def valid_membership?(env, collection_ids:)
checker = Hyrax::MultipleMembershipChecker.new(item: env.curation_concern)
multiple_memberships = checker.check(collection_ids:)
if multiple_memberships
env.curation_concern.errors.add(:collections, multiple_memberships)
return false
end
true
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module CreateWithRemoteFilesActorDecorator
module IngestRemoteFilesServiceDecorator
##
# @return true
# rubocop:disable Metrics/MethodLength
def attach!
return true unless remote_files
remote_files.each do |file_info|
Expand All @@ -27,27 +28,28 @@ def attach!
add_ordered_members! if ordered
true
end
# rubocop:enable Layout/LineLength

def create_file_from_url(uri, file_name, auth_header, override_default_thumbnail = nil)
import_url = URI.decode_www_form_component(uri.to_s)
use_valkyrie = false
case curation_concern
when Valkyrie::Resource
file_set = Hyrax.persister.save(resource: Hyrax::FileSet.new(import_url: import_url, label: file_name))
file_set = Hyrax.persister.save(resource: Hyrax::FileSet.new(import_url:, label: file_name))
use_valkyrie = true
else
# OVERRIDE Hyrax 3.5 to override default_thumbnail
file_set = ::FileSet.new(import_url: import_url,
file_set = ::FileSet.new(import_url:,
label: file_name,
override_default_thumbnail: override_default_thumbnail)
override_default_thumbnail:)
end
__create_file_from_url(file_set: file_set, uri: uri, auth_header: auth_header, use_valkyrie: use_valkyrie)
__create_file_from_url(file_set:, uri:, auth_header:, use_valkyrie:)
end
end
end
end
end

# rubocop:disable Metrics/LineLength
# rubocop:disable Layout/LineLength
Hyrax::Actors::CreateWithRemoteFilesActor::IngestRemoteFilesService.prepend Hyrax::Actors::CreateWithRemoteFilesActorDecorator::IngestRemoteFilesServiceDecorator
# rubocop:enable Metrics/LineLength
# rubocop:enable Layout/LineLength
6 changes: 4 additions & 2 deletions app/actors/hyrax/actors/file_set_actor_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
module Hyrax
module Actors
module FileSetActorDecorator
# rubocop:disable Metrics/AbcSize
def attach_to_valkyrie_work(work, file_set_params)
work = Hyrax.query_service.find_by(id: work.id) unless work.new_record
file_set.visibility = work.visibility unless assign_visibility?(file_set_params)
fs = Hyrax.persister.save(resource: file_set)
Hyrax.publisher.publish('object.metadata.updated', object: fs, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: fs, user:)
work.member_ids << fs.id
work.representative_id = fs.id if work.representative_id.blank?
# OVERRIDE Hyrax 3.5.0 to override default_thumbnail
Expand All @@ -18,8 +19,9 @@ def attach_to_valkyrie_work(work, file_set_params)
# Save the work so the association between the work and the file_set is persisted (head_id)
# NOTE: the work may not be valid, in which case this save doesn't do anything.
Hyrax.persister.save(resource: work)
Hyrax.publisher.publish('object.metadata.updated', object: work, user: user)
Hyrax.publisher.publish('object.metadata.updated', object: work, user:)
end
# rubocop:enable Metrics/AbcSize

# Adds a FileSet to the work using ore:Aggregations.
def attach_to_af_work(work, file_set_params)
Expand Down
1 change: 0 additions & 1 deletion app/controllers/catalog_controller_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,3 @@ def self.uploaded_field
config.add_sort_field "#{CatalogController.modified_field} asc", label: "Upload Date (Ascending)"
config.add_sort_field "#{CatalogController.modified_field} desc", label: "Upload Date (Descending)"
end
# rubocop:enable Metrics/BlockLength
2 changes: 1 addition & 1 deletion app/controllers/hyrax/conference_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ConferenceItemsController < ApplicationController
# Use a Valkyrie aware form service to generate Valkyrie::ChangeSet style
# forms.
self.work_form_service = Hyrax::FormFactory.new

# Use this line if you want to use a custom presenter
self.show_presenter = Hyrax::ConferenceItemPresenter
end
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/hyrax/homepage_controller_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# OVERRIDE Hyku 5 to add homepage about blocks
module Hyrax
module HomepageControllerDecorator

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def index
# BEGIN copy Hyrax prime's Hyrax::HomepageController#index
@presenter = presenter_class.new(current_ability, collections)
Expand All @@ -15,7 +15,7 @@ def index
@homepage_about_section_heading = ContentBlock.for(:homepage_about_section_heading)
@homepage_about_section_content = ContentBlock.for(:homepage_about_section_content)
# END OVERRIDE

recent
# END copy

Expand All @@ -42,8 +42,7 @@ def index
document_export_formats(format)
end
end


# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
end
end

Expand Down
13 changes: 7 additions & 6 deletions app/indexers/app_indexer_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

module AppIndexerDecorator
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def generate_solr_document
super.tap do |solr_doc|
solr_doc["account_cname_tesim"] = Site.instance&.account&.cname
Expand All @@ -11,22 +12,22 @@ def generate_solr_document
solr_doc['fedora_id_ssi'] = object.id
solr_doc[ActiveFedora.id_field.to_sym] = object.to_param
solr_doc['source_sim'] = solr_doc['source_tesim']
solr_doc['file_set_text_tsimv'] = child_works_file_sets(object: object).map { |fs| all_text(object: fs) }
.select(&:present?)
.join("\n---------------------------\n")
solr_doc['file_set_text_tsimv'] = child_works_file_sets(object:).map { |fs| all_text(object: fs) }
.select(&:present?)
.join("\n---------------------------\n")

if object.date_created.present?
# rubocop:disable Metrics/LineLength
# rubocop:disable Layout/LineLength
date_created = object.date_created.is_a?(ActiveTriples::Relation) ? object.date_created.first : object.date_created
# rubocop:enable Metrics/LineLength
# rubocop:enable Layout/LineLength
# expects date created to be array with single string in yyyy-mm-dd format
solr_doc['sorted_date_isi'] = date_created.tr('-', '').to_i
solr_doc['sorted_month_isi'] = date_created.tr('-', '').slice(0..5).to_i
solr_doc['sorted_year_isi'] = date_created.slice(0..3).to_i
end
end
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength

def child_works_file_sets(object:)
object.works.map { |work| work.file_sets.to_a }.flatten
Expand Down
Loading

0 comments on commit 598f02f

Please sign in to comment.