diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 151afe4d2..82d80225d 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -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 @@ -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 diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index 714955a9a..868bfaa03 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -26,8 +26,6 @@ def index else search_without_params end - @pagy, @records = pagy_array(@profiles) - @profiles_count = @pagy.count end def show @@ -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] @@ -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 @@ -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 diff --git a/app/models/profile.rb b/app/models/profile.rb index 787cc42e5..5604e416f 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -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 diff --git a/app/views/profiles/_profile.html.erb b/app/views/profiles/_profile.html.erb index 85812207e..86203f9c3 100644 --- a/app/views/profiles/_profile.html.erb +++ b/app/views/profiles/_profile.html.erb @@ -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 } %>
- <%= profile_image_link(Profile.find(profile[:id])) %> + <%= profile_image_link(Profile.find(profile.id)) %>
- <%= link_to profile[:fullname], profile_path(profile[:id]) %> + <%= link_to profile.fullname, profile_path(profile.id) %>

- <%= 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 %>

- <%= t(:"result#{'_' + current_region.to_s if current_region}", scope: 'search', count: @profiles_count).html_safe %><%= params[:search] %> + <%= t(:"result#{'_' + current_region.to_s if current_region}", scope: 'search', count: @pagy.count).html_safe %><%= params[:search] %>
@@ -39,7 +39,7 @@ <% if params[:category_id] %>

<%= @category.name %>

-

<%= t(:"profiles_in_category#{'_' + current_region.to_s if current_region}", scope: 'profiles.index', count: @profiles_count).html_safe %>

+

<%= t(:"profiles_in_category#{'_' + current_region.to_s if current_region}", scope: 'profiles.index', count: @pagy.count).html_safe %>

<% if current_region %>

<%= t(:expand_search, scope: 'search') %> @@ -48,7 +48,7 @@ <% end %> <% elsif params[:tag_filter] %>

- <%= 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 %>

<% else %>

<%= t(:all_profiles, scope: 'profiles.index') %>

diff --git a/app/views/shared/_search_filter.erb b/app/views/shared/_search_filter.erb index 7249d1662..c5ab7ef21 100644 --- a/app/views/shared/_search_filter.erb +++ b/app/views/shared/_search_filter.erb @@ -1,4 +1,4 @@ -<% if @profiles_count > 0 %> +<% if @pagy.count > 0 %>
    diff --git a/spec/controllers/profiles_controller_spec.rb b/spec/controllers/profiles_controller_spec.rb index 8f4dcab38..4480a82e7 100644 --- a/spec/controllers/profiles_controller_spec.rb +++ b/spec/controllers/profiles_controller_spec.rb @@ -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