Skip to content
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Layout/IndentationConsistency:
Exclude:
- 'db/migrate/*'

Style/ParenthesesAroundCondition:
Enabled: true
AllowInMultilineConditions: true

# Configuration parameters: EnforcedStyle, SupportedStyles.
# SupportedStyles: symmetrical, new_line, same_line
Layout/MultilineArrayBraceLayout:
Expand Down Expand Up @@ -109,7 +113,6 @@ Layout/ExtraSpacing:
Exclude:
- 'db/migrate/*'


# Detect hard tabs, no hard tabs.
Layout/IndentationStyle:
Enabled: true
Expand Down
42 changes: 21 additions & 21 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ class Document < ApplicationRecord
include PgSearch::Model

ACCEPTED_CONTENT_TYPES = [
"image/jpeg", # jpg
"image/jpeg", # jpeg
"image/gif", # gif
"image/png", # png
"image/bmp", # bmp
"image/tiff", # tif
"image/tiff", # tiff
"application/vnd.ms-powerpoint", # ppt
"application/vnd.openxmlformats-officedocument.presentationml.presentation", # pptx
"application/vnd.ms-excel", # xls
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", # xlsx
"application/rtf", # rtf
"text/plain", # txt
"application/msword", # doc
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", # docx
"application/pdf", # pdf
"text/csv", # csv
"text/tab-separated-values", # tsv
"application/vnd.oasis.opendocument.text", # odt
"application/vnd.oasis.opendocument.spreadsheet", # ods
"application/vnd.oasis.opendocument.presentation" # odp
'image/jpeg', # jpg
'image/jpeg', # jpeg
'image/gif', # gif
'image/png', # png
'image/bmp', # bmp
'image/tiff', # tif
'image/tiff', # tiff
'application/vnd.ms-powerpoint', # ppt
'application/vnd.openxmlformats-officedocument.presentationml.presentation', # pptx
'application/vnd.ms-excel', # xls
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', # xlsx
'application/rtf', # rtf
'text/plain', # txt
'application/msword', # doc
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', # docx
'application/pdf', # pdf
'text/csv', # csv
'text/tab-separated-values', # tsv
'application/vnd.oasis.opendocument.text', # odt
'application/vnd.oasis.opendocument.spreadsheet', # ods
'application/vnd.oasis.opendocument.presentation' # odp
].freeze

pg_search_scope :search_by_title, against: :title,
Expand Down
79 changes: 53 additions & 26 deletions app/models/listing_change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,21 @@ def duplicates(comparison_attributes_override = {})
comparison_attributes.merge(comparison_attributes_override.symbolize_keys)
)
)

if party_listing_distribution
relation = relation.includes(:party_listing_distribution).references(:party_listing_distribution).where(
party_listing_distribution.comparison_conditions(
party_listing_distribution.comparison_attributes.except(:listing_change_id)
)
)
end

if annotation
relation = relation.includes(:annotation).references(:annotation).where(
annotation.comparison_conditions
)
end

relation
end

Expand Down Expand Up @@ -223,22 +226,11 @@ def event_designation_mismatch
end
end

def listing_change_before_save_callback
# check if annotation should be deleted
if annotation &&
annotation.short_note_en.blank? &&
annotation.short_note_fr.blank? &&
annotation.short_note_es.blank? &&
annotation.full_note_en.blank? &&
annotation.full_note_fr.blank? &&
annotation.full_note_es.blank?
ann = annotation
self.annotation = nil
if ann.reload.listing_changes.empty?
ann.delete
end
end

##
# Called before save: if either excluded_geo_entities_ids or
# excluded_taxon_concepts_ids are set, create or replace ListingChanges with
# type `EXCEPTION` linked to this ListingChange (as the parent) accordingly.
def populate_exceptions_from_exclusions
original_change_type = ChangeType.find(change_type_id)

@excluded_geo_entities_ids = @excluded_geo_entities_ids &&
Expand All @@ -251,34 +243,69 @@ def listing_change_before_save_callback
return self if @excluded_geo_entities_ids.nil? &&
@excluded_taxon_concepts_ids.nil?

new_exclusions = []
exclusion_change_type = ChangeType.find_by(
name: ChangeType::EXCEPTION, designation_id: original_change_type.designation_id
)

# geographic exclusions
excluded_geo_entities =
if @excluded_geo_entities_ids.present?
new_exclusions << ListingChange.new(
change_type_id: exclusion_change_type.id,
species_listing_id: species_listing_id,
taxon_concept_id: taxon_concept_id,
geo_entity_ids: @excluded_geo_entities_ids
)
[
ListingChange.new(
change_type_id: exclusion_change_type.id,
species_listing_id: species_listing_id,
taxon_concept_id: taxon_concept_id,
geo_entity_ids: @excluded_geo_entities_ids,
effective_at: effective_at
)
]
else
[]
end

# taxonomic exclusions
excluded_taxon_concepts =
if @excluded_taxon_concepts_ids.present?
@excluded_taxon_concepts_ids.map do |id|
new_exclusions << ListingChange.new(
ListingChange.new(
change_type_id: exclusion_change_type.id,
species_listing_id: species_listing_id,
taxon_concept_id: id
taxon_concept_id: id,
effective_at: effective_at
)
end
else
[]
end

self.exclusions = new_exclusions
self.exclusions = excluded_taxon_concepts + excluded_geo_entities

self
end

##
# Before save, check if annotation should be deleted
def delete_empty_annotation
if (
annotation &&
annotation.short_note_en.blank? &&
annotation.short_note_fr.blank? &&
annotation.short_note_es.blank? &&
annotation.full_note_en.blank? &&
annotation.full_note_fr.blank? &&
annotation.full_note_es.blank?
)
ann = annotation
self.annotation = nil

if ann.reload.listing_changes.empty?
ann.delete
end
end
end

def listing_change_before_save_callback
delete_empty_annotation
populate_exceptions_from_exclusions
end
end
13 changes: 11 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ def can_be_deleted?
Reference, TaxonConceptReference, DistributionReference,
Trade::AnnualReportUpload, Trade::Shipment
]

for i in 0..tracked_objects.length - 1
if tracked_objects[i].where([ 'created_by_id = :id OR updated_by_id = :id', id: self.id ]).limit(1).count > 0
return false
end
end

true
end

Expand Down Expand Up @@ -150,19 +152,25 @@ def send_devise_notification(notification, *)
def sync_with_captive_breeding_db
# Only interested if role, name, encrypted_password, and email is changed.
# Or user deleted.
return unless (previous_changes.keys & %w[email role name encrypted_password]).present? || destroyed?
return unless
previous_changes.keys.intersect?(
%w[email role name encrypted_password]
).present? || destroyed?

role_was = previous_changes['role']&.first

action =
if destroyed? # User record deleted.
:delete
elsif is_elibrary_user? || is_manager? # Is admin or elibrary.
:create_or_update
elsif role_was == MANAGER || role_was == ELIBRARY_USER # Was admin or elibrary.
elsif role_was == MANAGER || role_was == ELIBRARY_USER # rubocop:disable Lint/DuplicateBranch
# Was admin or elibrary, but (because previous condition not met), is not any more
:delete
else
:none
end

return if action == :none

email_was = previous_changes['email']&.first
Expand All @@ -180,6 +188,7 @@ def sync_with_captive_breeding_db
CaptiveBreedingUser.create!(email:, name:, encrypted_password:)
else # Update the first CB user record, which is using the new email address (if changed).
existing_cb_users.first.update!(email:, name:, encrypted_password:)

if existing_cb_users[1].present? # Duplicate user!? Remove it?
# TODO: Do not have requirement for this yet, not sure is it safe to delete.
# https://unep-wcmc.codebasehq.com/projects/cites-support-maintenance/tickets/241
Expand Down
Loading