Skip to content

Commit

Permalink
big refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Salazar committed Jan 25, 2017
1 parent 021380a commit 234677c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
16 changes: 11 additions & 5 deletions app/controllers/recommendations_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
43 changes: 20 additions & 23 deletions app/services/recommender.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 2 additions & 4 deletions app/views/recommendations/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
= 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
%tr
%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
= paginate @recommendations
7 changes: 6 additions & 1 deletion lib/tasks/load.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 234677c

Please sign in to comment.