Skip to content

Commit

Permalink
Add browse to search (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccozkan authored Jul 8, 2023
1 parent 1053184 commit f9a3f39
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
44 changes: 44 additions & 0 deletions app/controllers/browse_search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class BrowseSearchController < SearchFeedsController
CATEGORY_MAP = {
arts: "Arts",
business: "Business",
comedy: "Comedy",
education: "Education",
fiction: "Fiction",
government: "Government",
history: "History",
health_and_fitness: "Health &amp; Fitness",
kids_and_family: "Kids &amp; Family",
leisure: "Leisure",
music: "Music",
news: "News",
religion_and_spirituality: "Religion &amp; Spirituality",
science: "Science",
society_and_culture: "Society &amp; Culture",
sports: "Sports",
technology: "Technology",
true_crime: "True Crime",
}.freeze
ALLOWED_CATEGORIES = CATEGORY_MAP.keys

before_action :ensure_category_allowed, only: :show

def show
@categories = CATEGORY_MAP

search_results = retrieve_search_results(params[:category])
if search_results.empty?
@no_results_found = true
else
@already_subscribed = current_user.feeds.pluck(:external_id) if current_user
@pagy, @items = pagy_array(search_results)
end
render "search_feeds/index"
end

def ensure_category_allowed
category = params[:category]

redirect_to search_path unless ALLOWED_CATEGORIES.include?(category.to_sym)
end
end
7 changes: 4 additions & 3 deletions app/controllers/search_feeds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ class SearchFeedsController < ApplicationController
include Pagy::Backend

def index
@categories = BrowseSearchController::CATEGORY_MAP
@query = permitted_params[:query]
return unless @query

search_results = retrieve_search_results
search_results = retrieve_search_results(@query)
if search_results.empty?
@no_results_found = true
else
Expand All @@ -16,8 +17,8 @@ def index

private

def retrieve_search_results
service = FeedSearcherService.new(permitted_params[:query]).call
def retrieve_search_results(param)
service = FeedSearcherService.new(param).call

if service.success?
service.payload
Expand Down
3 changes: 2 additions & 1 deletion app/models/interaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def toggle_from_listen_it_later!(episode_id, user_id)

def bookmarked_second(episode_id, user_id)
find_interaction(episode_id, user_id)
@interaction&.bookmarked_at_second || nil
output = @interaction&.bookmarked_at_second || nil
output&.zero? ? nil : output
end

def bookmarked_at_pretty_time(episode_id, user_id)
Expand Down
11 changes: 9 additions & 2 deletions app/views/search_feeds/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
<div class="column"></div>
<div class="column"></div>
</div>
<div class="columns">
<div class="column is-half">
<div class="buttons">
<% @categories.each do |cat| %>
<a class="button is-warning is-small" href="<%= browse_search_path(cat[0]) %>"><%= cat[1].downcase.html_safe %></a>
<% end %>
</div>
</div>
</div>
</div>
<hr>
<div class="container">
</div>
</section>

<div class="container">
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
resources :listen_it_later_episodes, only: [:index, :update], param: :episode_id, path: :later
resources :bookmark_episodes, only: [:update], param: :episode_id, path: :bookmark
resources :random_episodes, only: [:show], param: :feed_slug, path: :random_episodes
resources :browse_search, only: [:show], param: :category, path: :browse
resources :unsubscriptions, only: [:destroy], param: :feed_id

put 'preferences' => 'preferences#update', as: 'preferences'
Expand Down

0 comments on commit f9a3f39

Please sign in to comment.