Skip to content

Commit

Permalink
Merge pull request #1050 from DaanVanVugt/feature/disable_materials
Browse files Browse the repository at this point in the history
disable materials
  • Loading branch information
fbacall authored Nov 28, 2024
2 parents aaac5c5 + cc0bf0a commit 371d0a0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 34 deletions.
14 changes: 7 additions & 7 deletions app/controllers/materials_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# The controller for actions related to the Materials model
class MaterialsController < ApplicationController
before_action :feature_enabled?
before_action :set_material, only: [:show, :edit, :update, :destroy, :update_collections, :clone,
:add_term, :reject_term, :add_data, :reject_data]
before_action :set_material, only: %i[show edit update destroy update_collections clone
add_term reject_term add_data reject_data]
before_action :set_breadcrumbs
before_action :set_learning_path_navigation, only: :show

Expand Down Expand Up @@ -82,8 +82,8 @@ def check_exists
end
else
respond_to do |format|
format.html { render :nothing => true, :status => 200, :content_type => 'text/html' }
format.json { render json: {}, :status => 200, :content_type => 'application/json' }
format.html { render nothing: true, status: 200, content_type: 'text/html' }
format.json { render json: {}, status: 200, content_type: 'application/json' }
end
end
end
Expand Down Expand Up @@ -168,14 +168,14 @@ def material_params
:last_scraped, :scraper_record, :remote_created_date, :remote_updated_date,
:content_provider_id, :difficulty_level, :version, :status,
:date_created, :date_modified, :date_published, :other_types,
:prerequisites, :syllabus, :learning_objectives, { subsets: [] },
:prerequisites, :syllabus, :visible, :learning_objectives, { subsets: [] },
{ contributors: [] }, { authors: [] }, { target_audience: [] },
{ collection_ids: [] }, { keywords: [] }, { resource_type: [] },
{ scientific_topic_names: [] }, { scientific_topic_uris: [] },
{ operation_names: [] }, { operation_uris: [] },
{ node_ids: [] }, { node_names: [] }, { fields: [] },
external_resources_attributes: [:id, :url, :title, :_destroy],
external_resources: [:url, :title],
external_resources_attributes: %i[id url title _destroy],
external_resources: %i[url title],
event_ids: [], locked_fields: [])
end

Expand Down
49 changes: 23 additions & 26 deletions app/models/material.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class Material < ApplicationRecord
text :target_audience
text :keywords
text :resource_type
boolean :visible
text :content_provider do
self.content_provider.try(:title)
content_provider.try(:title)
end
text :scientific_topics do
scientific_topics_and_synonyms
Expand All @@ -45,23 +46,23 @@ class Material < ApplicationRecord
end
# other fields
string :title
string :authors, :multiple => true
string :scientific_topics, :multiple => true do
string :authors, multiple: true
string :scientific_topics, multiple: true do
scientific_topics_and_synonyms
end
string :operations, :multiple => true do
string :operations, multiple: true do
operations_and_synonyms
end
string :target_audience, :multiple => true
string :keywords, :multiple => true
string :fields, :multiple => true
string :resource_type, :multiple => true
string :contributors, :multiple => true
string :target_audience, multiple: true
string :keywords, multiple: true
string :fields, multiple: true
string :resource_type, multiple: true
string :contributors, multiple: true
string :content_provider do
self.content_provider.try(:title)
content_provider.try(:title)
end
string :node, multiple: true do
self.associated_nodes.pluck(:name)
associated_nodes.pluck(:name)
end
time :updated_at
time :created_at
Expand All @@ -77,7 +78,7 @@ class Material < ApplicationRecord
collections.where(public: true).pluck(:title)
end
string :status do
MaterialStatusDictionary.instance.lookup_value(self.status, 'title')
MaterialStatusDictionary.instance.lookup_value(status, 'title')
end
end
# :nocov:
Expand All @@ -94,15 +95,15 @@ class Material < ApplicationRecord
has_ontology_terms(:scientific_topics, branch: EDAM.topics)
has_ontology_terms(:operations, branch: EDAM.operations)

has_many :stars, as: :resource, dependent: :destroy
has_many :stars, as: :resource, dependent: :destroy

# Remove trailing and squeezes (:squish option) white spaces inside the string (before_validation):
# e.g. "James Bond " => "James Bond"
auto_strip_attributes :title, :description, :url, squish: false

validates :title, :description, :url, presence: true
validates :url, url: true
validates :other_types, presence: true, if: Proc.new { |m| m.resource_type.include?('other') }
validates :other_types, presence: true, if: proc { |m| m.resource_type.include?('other') }
validates :keywords, length: { maximum: 20 }

clean_array_fields(:keywords, :fields, :contributors, :authors,
Expand All @@ -111,23 +112,23 @@ class Material < ApplicationRecord
update_suggestions(:keywords, :contributors, :authors, :target_audience,
:resource_type)

def description= desc
def description=(desc)
super(Rails::Html::FullSanitizer.new.sanitize(desc))
end

def short_description= desc
def short_description=(desc)
self.description = desc unless @_long_description_set
end

def long_description= desc
def long_description=(desc)
@_long_description_set = true
self.description = desc
end

def self.facet_fields
field_list = %w(scientific_topics operations tools standard_database_or_policy content_provider keywords
field_list = %w[scientific_topics operations tools standard_database_or_policy content_provider keywords
difficulty_level fields licence target_audience authors contributors resource_type
related_resources user node collections status)
related_resources user node collections status]

field_list.delete('operations') if TeSS::Config.feature['disabled'].include? 'operations'
field_list.delete('scientific_topics') if TeSS::Config.feature['disabled'].include? 'topics'
Expand All @@ -149,13 +150,9 @@ def self.check_exists(material_params)

scope = provider_id.present? ? where(content_provider_id: provider_id) : all

if given_material.url.present?
material = scope.where(url: given_material.url).last
end
material = scope.where(url: given_material.url).last if given_material.url.present?

if provider_id.present? && given_material.title.present?
material ||= scope.where(content_provider_id: provider_id, title: given_material.title).last
end
material ||= scope.where(content_provider_id: provider_id, title: given_material.title).last if provider_id.present? && given_material.title.present?

material
end
Expand All @@ -170,7 +167,7 @@ def duplicate
external_resources.each do |er|
c.external_resources.build(url: er.url, title: er.title)
end
[:events, :scientific_topics, :operations, :nodes].each do |field|
%i[events scientific_topics operations nodes].each do |field|
c.send("#{field}=", send(field))
end

Expand Down
3 changes: 3 additions & 0 deletions app/views/materials/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<%= render partial: 'common/error_summary', locals: { resource: @material } %>

<!-- Field: Disabled -->
<%= f.input :visible, label: 'Show this material?', input_html: { title: t('materials.hints.visible') } %>

<%# Necessary to allow removal of all field locks %>
<%= hidden_field_tag 'material[locked_fields][]', '' %>

Expand Down
7 changes: 7 additions & 0 deletions app/views/search/common/_facet_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ Variable that should be available
disable_text: t('sidebar.filter.values.hide_disabled_events') } %>
<% elsif resource_type.name == 'Material' || resource_type.name == 'LearningPath' %>
<% resource_name = resource_type.model_name.human.downcase.pluralize %>
<% if resource_type.name == 'Material' %>
<%= render partial: 'search/common/facet_sidebar_boolean_filter',
locals: { facet_field: 'include_disabled',
count: '-',
enable_text: t('sidebar.filter.values.show_disabled', resource: resource_name),
disable_text: t('sidebar.filter.values.hide_disabled', resource: resource_name) } %>
<% end %>
<%= render partial: 'search/common/facet_sidebar_boolean_filter',
locals: { facet_field: 'include_archived',
count: '-',
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ en:
resource_type: 'Select the type of resource that best matches the training material. '
url: 'Preferred URL to direct people to your training material landing page.'
version: 'Indicate the current version of the training material.'
visible: 'Whether or not this material should be shown on the materials page'
learning_paths:
hints:
authors: >
Expand Down Expand Up @@ -830,6 +831,8 @@ en:
hide_past_events: 'Hide past events'
show_disabled_events: 'Show disabled events'
hide_disabled_events: 'Hide disabled events'
show_disabled: 'Show disabled %{resource}'
hide_disabled: 'Hide disabled %{resource}'
show_archived: 'Show archived %{resource}'
hide_archived: 'Hide archived %{resource}'
hidden:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241119152528_add_visible_to_material.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddVisibleToMaterial < ActiveRecord::Migration[7.0]
def change
add_column :materials, :visible, :bool, default: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_10_22_113543) do
ActiveRecord::Schema[7.0].define(version: 2024_11_19_152528) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -378,6 +378,7 @@
t.text "contact"
t.text "learning_objectives"
t.string "fields", default: [], array: true
t.boolean "visible", defaut: true
t.index ["content_provider_id"], name: "index_materials_on_content_provider_id"
t.index ["slug"], name: "index_materials_on_slug", unique: true
t.index ["user_id"], name: "index_materials_on_user_id"
Expand Down
5 changes: 5 additions & 0 deletions public/api/definitions/tess.yml
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,11 @@ paths:
- Active
- Under development
- Archived
- name: include_disabled
in: query
description: Include materials that are disabled.
schema:
type: boolean
responses:
200:
description: A collection of materials, and facets available to filter them.
Expand Down

0 comments on commit 371d0a0

Please sign in to comment.