diff --git a/app/controllers/recommendations_controller.rb b/app/controllers/recommendations_controller.rb index 0c1257d..f673fd6 100644 --- a/app/controllers/recommendations_controller.rb +++ b/app/controllers/recommendations_controller.rb @@ -1,17 +1,23 @@ class RecommendationsController < ApplicationController def index - if params[:subtopic].blank? + if params[:subtopic].strip.blank? flash[:alert] = "Please provide a subtopic parameter: '/recommendations?subtopic=X' or paste the subtopic into the search field above." redirect_to root_path and return end - @sub_topic = SubTopic.find_by_subtopic_id params[:subtopic] - @recommender = Recommender.new @sub_topic.subtopic_id, params.fetch(:per, 4), params[:page] + per = params.fetch :per, 4 + page = params[:page] + results = Recommender.new(params[:subtopic]).solve + @recommendations = Kaminari.paginate_array(results).page(page).per per respond_to do |format| - format.html + format.html do + @sub_topic = SubTopic.find_by_subtopic_id params[:subtopic] + end + format.json do - render json: { recommended_subtopics: @recommender.ask.map(&:first) } + subtopic_ids = @recommendations.map &:first + render json: subtopic_ids end end end diff --git a/app/services/recommender.rb b/app/services/recommender.rb index 0a1555a..fdf1ccb 100644 --- a/app/services/recommender.rb +++ b/app/services/recommender.rb @@ -1,41 +1,38 @@ class Recommender - def initialize(subtopic_id, per = 4, page = 0) - @subtopic_id, @per, @page = subtopic_id, per, page + def initialize(subtopic_id) + @subtopic_id = subtopic_id end - def ask - sub_topics = if get_recommendation - get_recommendation.recommended_subtopic_ids + def solve + if (r = Recommendation.find_by_subtopic_id @subtopic_id) + r.recommended_subtopic_ids else - get_sub_topics.tap do |sub_topics| - Recommendation.create({ - subtopic_id: @subtopic_id, - recommended_subtopic_ids: sub_topics.map { |s, l| [s, l.size] } - }) - end + s = get_sub_topics + binding.pry # debug + create_recommendation s + s end - - Kaminari.paginate_array(sub_topics).page(@page).per @per end - def get_recommendation - @recommendation ||= Recommendation.find_by_subtopic_id @subtopic_id - end + private def get_sub_topics - sub_topics = {} - - users_also_listened.each do |user| + binding.pry # debug + User.who_listened_to(@subtopic_id).inject({}) do |sub_topics, user| user.listens.each do |listen| next if listen.subtopic_id == @subtopic_id sub_topics[listen.subtopic_id] ||= [] sub_topics[listen.subtopic_id] << listen end - end - sub_topics.sort_by { |_, listens| listens.size }.reverse + binding.pry # debug + sub_topics + end.sort_by { |_, listens| listens.size }.reverse end - def users_also_listened - User.who_listened_to(@subtopic_id) + def create_recommendation(sub_topics) + Recommendation.create({ + subtopic_id: @subtopic_id, + recommended_subtopic_ids: sub_topics.map { |sid, listens| [sid, listens.size] } + }) end end diff --git a/app/views/recommendations/index.html.haml b/app/views/recommendations/index.html.haml index 624dff8..c3ee122 100644 --- a/app/views/recommendations/index.html.haml +++ b/app/views/recommendations/index.html.haml @@ -3,8 +3,6 @@ = succeed ", you might also like..." do = link_to @sub_topic.name, sub_topic_path(@sub_topic) -- sub_topics = @recommender.ask - .well %table.table.table-striped.table-hover %thead @@ -12,10 +10,10 @@ %th SubTopic %th Listens %tbody - - sub_topics.each do |subtopic_id, listens| + - @recommendations.each do |subtopic_id, listens| - sub_topic = SubTopic.find_by_subtopic_id subtopic_id %tr %td= link_to sub_topic.name, sub_topic %td= listens -= paginate sub_topics \ No newline at end of file += paginate @recommendations \ No newline at end of file diff --git a/lib/tasks/load.rake b/lib/tasks/load.rake index fe0adeb..e2ade42 100644 --- a/lib/tasks/load.rake +++ b/lib/tasks/load.rake @@ -59,7 +59,12 @@ namespace :load do subtopic_ids = SubTopic.pluck :subtopic_id puts "Creating #{subtopic_ids.size} recommendations..." - subtopic_ids.each { |subtopic_id| Recommender.new(subtopic_id).ask } + subtopic_ids.each_with_index do |subtopic_id, i| + subtopic = SubTopic.find_by_subtopic_id subtopic_id + puts "\tGenerating recommendations for #{subtopic.name}" + + Recommender.new(subtopic_id).solve + end puts "\aDone." end end