Skip to content

Commit

Permalink
Merge pull request #2010 from seek4science/emt-json-creation
Browse files Browse the repository at this point in the history
System Admin has right to create, manage (enable, disable), and delete extended metadata types via user interface #1999
  • Loading branch information
whomingbird authored Oct 17, 2024
2 parents ffc3b5d + bdf0203 commit a36017d
Show file tree
Hide file tree
Showing 45 changed files with 1,201 additions and 69 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@
//= require select2.full.min
//= require licenses
//= require svg-pan-zoom-3.6.1/svg-pan-zoom.min
//= require extended_metadata_type
13 changes: 13 additions & 0 deletions app/assets/javascripts/extended_metadata_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var ExtendedMetadataType = {

addNewMarker: function(new_id, type) {
var isNested = (type === "ExtendedMetadata");
$j("#nested-tab, #nested-tab").parent().toggleClass("active", isNested);
$j("#nested-metadata-table").toggleClass("active in", isNested);
$j("#top-level-tab, #top-level-tab").parent().toggleClass("active", !isNested);
$j("#top-level-metadata-table").toggleClass("active in", !isNested);

var table = isNested ? $j("#nested-metadata-table") : $j("#top-level-metadata-table");
table.find("tbody td#" + new_id).append(' <sup style="color:red; font-weight:bold; font-size: 12px">new</sup>');
}
};
78 changes: 73 additions & 5 deletions app/controllers/extended_metadata_types_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class ExtendedMetadataTypesController < ApplicationController
respond_to :json
respond_to :json, :html
skip_before_action :project_membership_required
before_action :is_user_admin_auth, except: [:form_fields, :show, :index]
before_action :find_requested_item, only: [:administer_update, :show]
before_action :find_requested_item, only: [:administer_update, :show, :destroy]
include Seek::IndexPager
after_action :log_event, only: [:create, :destroy]

api_actions :index, :show

Expand All @@ -25,10 +27,40 @@ def form_fields
end
end

def create

if params[:emt_json_file].blank?
flash[:error] = 'Please select a file to upload!'
redirect_to new_extended_metadata_type_path and return
end

uploaded_file = params[:emt_json_file]
@extended_metadata_type = Seek::ExtendedMetadataType::EMTExtractor.extract_extended_metadata_type(uploaded_file)

if @extended_metadata_type.save
flash[:notice] = 'Extended metadata type was successfully created.'
redirect_to administer_extended_metadata_types_path(emt: @extended_metadata_type.id)
else
flash[:error] = @extended_metadata_type.errors.full_messages.join(', ')
redirect_to new_extended_metadata_type_path
end
rescue StandardError => e
flash[:error] = e.message
redirect_to new_extended_metadata_type_path
end


def new
respond_to do |format|
format.html
end
end

def show
respond_to do |format|
format.json {render json: @extended_metadata_type}
end
respond_to do |format|
format.json {render json: @extended_metadata_type}
format.html
end
end

def index
Expand All @@ -46,6 +78,26 @@ def index
end
end

def destroy

# if a nested metadata type is linked by other metadata types
return if @extended_metadata_type.linked_metadata_attributes.any?

# if a top level metadata type has been used to create metadatas
return if @extended_metadata_type.extended_metadatas.present?

if @extended_metadata_type.destroy
flash[:notice] = 'Extended metadata type was successfully deleted.'
else
flash[:alert] = 'Failed to delete the extended metadata type.'
end

respond_to do |format|
format.html { redirect_to administer_extended_metadata_types_path }
end

end

def administer_update
@extended_metadata_type.update(extended_metadata_type_params)
unless @extended_metadata_type.save
Expand All @@ -57,6 +109,9 @@ def administer_update
end

def administer
if params[:emt]
@extended_metadata_type = ExtendedMetadataType.find(params[:emt])
end
@extended_metadata_types = ExtendedMetadataType.order(:supported_type)
respond_to do |format|
format.html
Expand All @@ -69,4 +124,17 @@ def extended_metadata_type_params
params.require(:extended_metadata_type).permit(:title, :enabled)
end

def log_event

return if object_invalid_or_unsaved?(@extended_metadata_type)

ActivityLog.create(action: action_name.downcase,
culprit: current_user,
controller_name: self.controller_name.downcase,
activity_loggable: object_for_request,
data: object_for_request.title,
user_agent: request.env['HTTP_USER_AGENT'])

end

end
4 changes: 4 additions & 0 deletions app/models/extended_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ def attribute_class
ExtendedMetadataAttribute
end

def self.supports_extended_metadata?
true
end

end
23 changes: 14 additions & 9 deletions app/models/extended_metadata_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class ExtendedMetadataType < ApplicationRecord
validates :title, presence: true
validates :extended_metadata_attributes, presence: true
validates :supported_type, presence: true
validate :supported_type_must_be_valid_type
validate :unique_titles_for_extended_metadata_attributes
validate :cannot_disable_nested_extended_metadata
validate :supports_extended_metadata

alias_method :metadata_attributes, :extended_metadata_attributes

Expand Down Expand Up @@ -34,13 +34,11 @@ def extended_type?
supported_type == 'ExtendedMetadata'
end

def supported_type_must_be_valid_type
return if supported_type.blank? # already convered by presence validation
unless Seek::Util.lookup_class(supported_type, raise: false)
errors.add(:supported_type, 'is not a type that can supported extended metadata')
end
def linked_metadata_attributes
ExtendedMetadataAttribute.where(linked_extended_metadata_type_id: id)
end


def unique_titles_for_extended_metadata_attributes
titles = extended_metadata_attributes.collect(&:title)
if titles != titles.uniq
Expand All @@ -54,6 +52,16 @@ def cannot_disable_nested_extended_metadata
end
end

def supports_extended_metadata
begin
unless Seek::Util.lookup_class(self.supported_type).supports_extended_metadata?
errors.add(:supported_type, " '#{self.supported_type}' does not support extended metadata!")
end
rescue NameError
errors.add(:supported_type, "'#{self.supported_type}' is not a valid support type!")
end
end

def usage
extended_metadatas.count
end
Expand All @@ -66,7 +74,4 @@ def self.disabled_but_in_use
disabled.select{|emt| emt.disabled_but_used?}
end




end
4 changes: 4 additions & 0 deletions app/models/file_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@ def supports_spreadsheet_explore?
def self.user_creatable?
Seek::Config.file_templates_enabled
end

def self.supports_extended_metadata?
false
end

end
4 changes: 4 additions & 0 deletions app/models/placeholder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ def self.user_creatable?
Seek::Config.placeholders_enabled
end

def self.supports_extended_metadata?
false
end

end
4 changes: 4 additions & 0 deletions app/models/publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ def contributor_credited?
false
end

def self.supports_extended_metadata?
false
end

def is_in_isa_publishable?
false
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/sample.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def self.can_create?
User.logged_in_and_member? && Seek::Config.samples_enabled
end

def self.supports_extended_metadata?
false
end

def related_data_files
[originating_data_file].compact + linked_data_files
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/sample_attribute_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def linked_extended_metadata_multi?
base_type == Seek::Samples::BaseType::LINKED_EXTENDED_METADATA_MULTI
end

def linked_extended_metadata_or_multi?
[Seek::Samples::BaseType::LINKED_EXTENDED_METADATA, Seek::Samples::BaseType::LINKED_EXTENDED_METADATA_MULTI].include?(base_type)
end

def seek_sample?
base_type == Seek::Samples::BaseType::SEEK_SAMPLE
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/sample_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,5 +274,9 @@ def validate_title_is_not_type_of_seek_sample_multi
end
end

def self.supports_extended_metadata?
false
end

class UnknownAttributeException < RuntimeError; end
end
5 changes: 5 additions & 0 deletions app/models/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@ def isa_tag_white_list(template_level)
[]
end
end

def self.supports_extended_metadata?
false
end

end
4 changes: 4 additions & 0 deletions app/models/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ def contributor_credited?
false
end

def self.supports_extended_metadata?
false
end

MATURITY_LEVELS = {
0 => :work_in_progress,
1 => :released,
Expand Down
Loading

0 comments on commit a36017d

Please sign in to comment.