Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix category view of profiles #1338

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@
class PagesController < ApplicationController
def home
# we take the seven newest profiles and keep one as an empty example
@newest_profiles = []
last_seven_profiles.each do |profile|
@newest_profiles << {
id: profile.id,
fullname: profile.fullname,
iso_languages: profile.iso_languages,
city: profile.city,
willing_to_travel: profile.willing_to_travel,
nonprofit: profile.nonprofit,
main_topic_or_first_topic: profile.main_topic_or_first_topic
}
end
@newest_profiles = last_seven_profiles

select_special_medicine_profiles

Expand All @@ -41,7 +30,7 @@ def features_and_profiles
{
title: feature.title,
description: feature.description,
profiles: feature.profiles.map(&:profile_card_details)
profiles: feature.profiles
}
end
end
Expand Down
50 changes: 27 additions & 23 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ def index
else
search_without_params
end
@pagy, @records = pagy_array(@profiles)
@profiles_count = @pagy.count
end

def show
Expand Down Expand Up @@ -133,7 +131,7 @@ def profile_params

def search_with_search_params
@profiles = matching_profiles.map(&:profile_card_details)

@pagy, @records = pagy_array(@profiles)
# search results aggregated according to certain attributes to display as filters
aggs = ProfileGrouper.new(params[:locale], @profiles.map { |profile| profile[:id] }).agg_hash
@aggs_languages = aggs[:languages]
Expand Down Expand Up @@ -170,7 +168,7 @@ def matching_profiles
end

def search_with_category_id
@profiles = profiles_for_category.map(&:profile_card_details)
@profiles = profiles_for_category
@category = Category.find(params[:category_id])
build_categories_and_tags_for_tags_filter
end
Expand All @@ -183,44 +181,50 @@ def profiles_for_category
.translated_in_current_language_and_not_translated(I18n.locale)
.pluck(:name)

Profile
.with_attached_image
.is_published
.by_region(current_region)
.includes(:taggings, :translations, :topics)
.where(tags: { name: tag_names })
@pagy, @records = pagy(
Profile
.with_attached_image
.is_published
.by_region(current_region)
.includes(:topics)
.where(tags: { name: tag_names })
)
end

def search_with_tags
@tags = params[:tag_filter].split(/\s*,\s*/)
last_tag = @tags.last
last_tag_id = ActsAsTaggableOn::Tag.where(name: last_tag).last.id
@profiles = profiles_with_tags(@tags).map(&:profile_card_details)
@profiles = profiles_with_tags(@tags)
@category = Category.select{|cat| cat.tag_ids.include?(last_tag_id)}.last
build_categories_and_tags_for_tags_filter
end

def profiles_with_tags(tags)
Profile
.is_published
.by_region(current_region)
.has_tags(tags)
@pagy, @records = pagy(
Profile
.is_published
.by_region(current_region)
.has_tags(tags)
)
end

def search_without_params
@profiles = profiles_for_index.map(&:profile_card_details)
@profiles = profiles_for_index
@category = Category.first
build_categories_and_tags_for_tags_filter
end

def profiles_for_index
Profile
.with_attached_image
.is_published
.by_region(current_region)
.includes(:translations)
.main_topic_translated_in(I18n.locale)
.random
@pagy, @records = pagy(
Profile
.with_attached_image
.is_published
.by_region(current_region)
.includes(:translations)
.main_topic_translated_in(I18n.locale)
.random
)
end

def build_categories_and_tags_for_tags_filter
Expand Down
18 changes: 15 additions & 3 deletions app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,28 @@ def self.typeahead(term, region: nil)
suggestions.map { |s| s.downcase }.uniq
end

Struct.new(
'ProfileCardDetails',
:id,
:fullname,
:iso_languages,
:city,
:willing_to_travel,
:nonprofit,
:main_topic_or_first_topic,
keyword_init: true
)

def profile_card_details
{
Struct::ProfileCardDetails.new(
id: id,
fullname: fullname,
iso_languages: iso_languages,
city: city,
willing_to_travel: willing_to_travel,
nonprofit: nonprofit,
main_topic_or_first_topic: main_topic_or_first_topic
}
main_topic_or_first_topic: main_topic_or_first_topic,
)
end

def fullname
Expand Down
8 changes: 4 additions & 4 deletions app/views/profiles/_profile.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<% if profile %>
<% tooltip_empty = !profile[:iso_languages].present? && profile[:city].nil? && profile[:willing_to_travel].nil? && profile[:nonprofit].nil? %>
<% tooltip_empty = !profile.iso_languages.present? && profile.city.nil? && profile.willing_to_travel.nil? && profile.nonprofit.nil? %>
<% title = render partial: "profiles/profile_tooltip", locals: { profile: profile } %>
<div class="col-sm-6 col-md-6 col-lg-3 my-3 profile-box">
<%= profile_image_link(Profile.find(profile[:id])) %>
<%= profile_image_link(Profile.find(profile.id)) %>
<div class="bg--white caption">
<div class="profile-name">
<%= link_to profile[:fullname], profile_path(profile[:id]) %>
<%= link_to profile.fullname, profile_path(profile.id) %>
</div>
<div class="profile-subtitle">
<p>
<%= profile[:main_topic_or_first_topic].truncate(60) if profile[:main_topic_or_first_topic] %>
<%= profile.main_topic_or_first_topic.truncate(60) if profile.main_topic_or_first_topic %>
</p>
<div
class="profile-tooltip"
Expand Down
6 changes: 3 additions & 3 deletions app/views/profiles/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="row">
<div class="col-12 py-5">
<span class="h4">
<%= t(:"result#{'_' + current_region.to_s if current_region}", scope: 'search', count: @profiles_count).html_safe %><b><%= params[:search] %></b>
<%= t(:"result#{'_' + current_region.to_s if current_region}", scope: 'search', count: @pagy.count).html_safe %><b><%= params[:search] %></b>
</span>
</div>
</div>
Expand Down Expand Up @@ -39,7 +39,7 @@
<a id="speakers_anchor"></a>
<% if params[:category_id] %>
<h1><%= @category.name %></h1>
<h3><%= t(:"profiles_in_category#{'_' + current_region.to_s if current_region}", scope: 'profiles.index', count: @profiles_count).html_safe %> </h3>
<h3><%= t(:"profiles_in_category#{'_' + current_region.to_s if current_region}", scope: 'profiles.index', count: @pagy.count).html_safe %> </h3>
<% if current_region %>
<p>
<%= t(:expand_search, scope: 'search') %>
Expand All @@ -48,7 +48,7 @@
<% end %>
<% elsif params[:tag_filter] %>
<h1>
<%= t(:all_tagged_speakerinnen, scope: 'profiles.index', count: @profiles_count).html_safe + t(:tag_filter, scope: 'profiles.index', count: @tags.count).html_safe %>
<%= t(:all_tagged_speakerinnen, scope: 'profiles.index', count: @pagy.count).html_safe + t(:tag_filter, scope: 'profiles.index', count: @tags.count).html_safe %>
</h1>
<% else %>
<h1><%= t(:all_profiles, scope: 'profiles.index') %></h1>
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_search_filter.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if @profiles_count > 0 %>
<% if @pagy.count > 0 %>
<div class="row row-cols-4">
<div class="col-12 col-sm-6 col-md-3">
<ul id="facet_languages" class="search-aggregation list-unstyled">
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/profiles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end

it 'displays published profiles' do
expect(assigns(:profiles).map { |hash| hash[:id] }).to eq ([ada.id])
expect(assigns(:records).pluck(:id)).to eq ([ada.id])
end

it 'does not include unpublished profiles' do
Expand Down
Loading