Skip to content

Commit

Permalink
Merge pull request #1053 from ElixirTeSS/keyword-limit
Browse files Browse the repository at this point in the history
Limit keywords to 20, and only display first 10 on index pages
  • Loading branch information
fbacall authored Nov 25, 2024
2 parents f141e9d + 414a7ef commit aaac5c5
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 4 deletions.
8 changes: 6 additions & 2 deletions app/helpers/materials_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,14 @@ def keywords_and_topics(resource, limit: nil)
tags |= resource.send(field) if resource.respond_to?(field)
end

limit_exceeded = limit && (tags.length > limit)
tags = tags.first(limit) if limit

tags.map do |tag|
elements = tags.map do |tag|
content_tag(:span, tag, class: 'label label-info')
end.join(' ').html_safe
end
elements << '&hellip;' if limit_exceeded

elements.join(' ').html_safe
end
end
1 change: 1 addition & 0 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Collection < ApplicationRecord
after_commit :index_items, if: :title_previously_changed?

validates :title, presence: true
validates :keywords, length: { maximum: 20 }

clean_array_fields(:keywords)
update_suggestions(:keywords)
Expand Down
2 changes: 1 addition & 1 deletion app/models/content_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class ContentProvider < ApplicationRecord

# Validate the URL is in correct format via valid_url gem
validates :url, url: true

validates :content_provider_type, presence: true, inclusion: { in: PROVIDER_TYPE }
validates :keywords, length: { maximum: 20 }

clean_array_fields(:keywords)

Expand Down
1 change: 1 addition & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class Event < ApplicationRecord
validates :longitude, numericality: { greater_than_or_equal_to: -180, less_than_or_equal_to: 180, allow_nil: true }
# validates :duration, format: { with: /\A[0-9][0-9]:[0-5][0-9]\z/, message: "must be in format HH:MM" }, allow_blank: true
validates :presence, inclusion: { in: presences.keys, allow_blank: true }
validates :keywords, length: { maximum: 20 }
validate :allowed_url
clean_array_fields(:keywords, :fields, :event_types, :target_audience,
:eligibility, :host_institutions, :sponsors)
Expand Down
1 change: 1 addition & 0 deletions app/models/learning_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class LearningPath < ApplicationRecord
after_validation :normalize_order

validates :title, :description, presence: true
validates :keywords, length: { maximum: 20 }

clean_array_fields(:keywords, :contributors, :authors, :target_audience)
update_suggestions(:keywords, :contributors, :authors, :target_audience)
Expand Down
1 change: 1 addition & 0 deletions app/models/learning_path_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class LearningPathTopic < ApplicationRecord
after_validation :normalize_order

validates :title, presence: true
validates :keywords, length: { maximum: 20 }

clean_array_fields(:keywords)
update_suggestions(:keywords)
Expand Down
1 change: 1 addition & 0 deletions app/models/material.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Material < ApplicationRecord
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 :keywords, length: { maximum: 20 }

clean_array_fields(:keywords, :fields, :contributors, :authors,
:target_audience, :resource_type, :subsets)
Expand Down
1 change: 1 addition & 0 deletions app/models/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Workflow < ApplicationRecord
auto_strip_attributes :title, squish: false

validates :title, presence: true
validates :keywords, length: { maximum: 20 }

clean_array_fields(:keywords, :contributors, :authors, :target_audience)

Expand Down
2 changes: 1 addition & 1 deletion app/views/materials/_material.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

<div class="font-size-lg"><%= display_difficulty_level(material) %></div>

<%= keywords_and_topics(material) %>
<%= keywords_and_topics(material, limit: 10) %>
</div>

<%= item_comment(collection_item) if collection_item %>
Expand Down
17 changes: 17 additions & 0 deletions test/controllers/materials_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1541,4 +1541,21 @@ class MaterialsControllerTest < ActionController::TestCase
assert_select 'strong', text: 'Next topic:', count: 0
assert_select 'span.empty', text: 'End of learning path'
end

test 'should limit displayed keywords on index page' do
keywords = 10.times.map { |i| "keyword_#{i}" }
@material.keywords = keywords + ['extra_keyword']
@material.save!

with_settings(solr_enabled: true) do
Material.stub(:search_and_filter, MockSearch.new([@material])) do
get :index
assert_response :success
assert_includes assigns(:materials), @material
assert_select '.label', text: 'keyword_3'
assert_select '.label', text: 'keyword_9'
assert_select '.label', text: 'extra_keyword', count: 0
end
end
end
end
12 changes: 12 additions & 0 deletions test/models/event_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -745,4 +745,16 @@ class EventTest < ActiveSupport::TestCase
empty_mix = Event.from_varied_providers([], 10)
assert_empty empty_mix
end

test 'validates keywords length' do
event = events(:one)
keywords = 20.times.map { |i| "keyword_#{i}" }
event.keywords = keywords
assert event.valid?

event.keywords = keywords + ['extra_keyword']

refute event.valid?
assert event.errors.added?(:keywords, :too_long, count: 20)
end
end

0 comments on commit aaac5c5

Please sign in to comment.