Skip to content

Commit

Permalink
extract function into helper and remove unnecessary comments
Browse files Browse the repository at this point in the history
  • Loading branch information
imadbourouche committed Dec 23, 2024
1 parent 1e819c3 commit 82d5b70
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
10 changes: 2 additions & 8 deletions controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CategoriesController < ApplicationController
get do
check_last_modified_collection(LinkedData::Models::Category)
categories = Category.where.include(*Category.goo_attrs_to_load(includes_param), ontologies: [:viewingRestriction]).to_a
categories = reject_private_ontologies(categories) unless current_user.admin? # only portal admin can see private ontologies
categories = reject_private_ontologies(categories) unless current_user.admin?
reply categories
end

Expand All @@ -24,7 +24,7 @@ class CategoriesController < ApplicationController
acronym = params["acronym"]
category = Category.find(acronym).include(*Category.goo_attrs_to_load(includes_param), ontologies: [:viewingRestriction]).first
error 404, "Category #{acronym} not found" if category.nil?
category = reject_private_ontologies([category]).first unless current_user.admin? # only portal admin can see private ontologies
category = reject_private_ontologies([category]).first unless current_user.admin?
reply 200, category
end

Expand Down Expand Up @@ -85,12 +85,6 @@ def create_category
reply 201, category
end

def reject_private_ontologies(categories)
categories.each do |category|
public_ontologies = category.ontologies.reject { |ontology| ontology.viewingRestriction == "private" }
category.instance_variable_set(:@ontologies, public_ontologies)
end
end

end
end
8 changes: 0 additions & 8 deletions controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class GroupsController < ApplicationController
get do
check_last_modified_collection(LinkedData::Models::Group)
groups = Group.where.include(*Group.goo_attrs_to_load(includes_param), ontologies: [:viewingRestriction]).to_a
# Private ontologies viewd only by admins of the portal
groups = reject_private_ontologies(groups) unless current_user.admin?
reply groups
end
Expand All @@ -25,7 +24,6 @@ class GroupsController < ApplicationController
acronym = params["acronym"]
g = Group.find(acronym).include(*Group.goo_attrs_to_load(includes_param), ontologies: [:viewingRestriction]).first
error 404, "Group #{acronym} not found" if g.nil?
# Private ontologies viewd only by admins of the portal
g = reject_private_ontologies([g]).first unless current_user.admin?
reply 200, g
end
Expand Down Expand Up @@ -86,12 +84,6 @@ def create_group
reply 201, group
end

def reject_private_ontologies(groups)
groups.each do |group|
public_ontologies = group.ontologies.reject { |ontology| ontology.viewingRestriction == "private" }
group.instance_variable_set(:@ontologies, public_ontologies)
end
end

end
end
9 changes: 9 additions & 0 deletions helpers/ontology_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ def add_file_to_submission(ont, submission)
end
return filename, tmpfile
end

# reject private ontologies in groups and categories
def reject_private_ontologies(items)
items.each do |item|
public_ontologies = item.ontologies.reject { |ontology| ontology.viewingRestriction == "private" }
item.instance_variable_set(:@ontologies, public_ontologies)
end
end

end
end
end
Expand Down

0 comments on commit 82d5b70

Please sign in to comment.