update user model with fixes, including style and recentMetacodes algorithm (#922)

This commit is contained in:
Devin Howard 2016-11-13 14:29:07 -08:00 committed by GitHub
parent 5b90c38b22
commit 55853c60f4
2 changed files with 11 additions and 23 deletions

View file

@ -15,9 +15,9 @@ module ApplicationHelper
@m = current_user.settings.metacodes @m = current_user.settings.metacodes
set = metacodeset set = metacodeset
@metacodes = if set && set == 'Most' @metacodes = if set && set == 'Most'
Metacode.where(id: current_user.mostUsedMetacodes).to_a Metacode.where(id: current_user.most_used_metacodes).to_a
elsif set && set == 'Recent' elsif set && set == 'Recent'
Metacode.where(id: current_user.recentMetacodes).to_a Metacode.where(id: current_user.recent_metacodes).to_a
elsif set elsif set
set.metacodes.to_a set.metacodes.to_a
else else
@ -27,11 +27,11 @@ module ApplicationHelper
end end
def user_most_used_metacodes def user_most_used_metacodes
@metacodes = current_user.mostUsedMetacodes.map { |id| Metacode.find(id) } @metacodes = current_user.most_used_metacodes.map { |id| Metacode.find(id) }
end end
def user_recent_metacodes def user_recent_metacodes
@metacodes = current_user.recentMetacodes.map { |id| Metacode.find(id) } @metacodes = current_user.recent_metacodes.map { |id| Metacode.find(id) }
end end
def invite_link def invite_link

View file

@ -64,32 +64,20 @@ class User < ApplicationRecord
json['rtype'] = 'mapper' json['rtype'] = 'mapper'
json json
end end
def all_accessible_maps def all_accessible_maps
#TODO: is there a way to keep this an ActiveRecord relation?
maps + shared_maps maps + shared_maps
end end
def recentMetacodes def recent_metacodes
array = [] topics.order(:created_at).pluck(:metacode_id).uniq.first(5)
self.topics.sort{|a,b| b.created_at <=> a.created_at }.each do |t|
if array.length < 5 and array.index(t.metacode_id) == nil
array.push(t.metacode_id)
end
end
array
end end
def mostUsedMetacodes def most_used_metacodes
self.topics.to_a.reduce({}) { |memo, topic| topics.to_a.each_with_object(Hash.new(0)) do |topic, memo|
if memo[topic.metacode_id] == nil memo[topic.metacode_id] += 1
memo[topic.metacode_id] = 1
else
memo[topic.metacode_id] = memo[topic.metacode_id] + 1
end
memo memo
}.to_a.sort{ |a, b| b[1] <=> a[1] }.map{|i| i[0]}.slice(0, 5) end.to_a.sort { |a, b| b[1] <=> a[1] }.map { |i| i[0] }.slice(0, 5)
end end
# generate a random 8 letter/digit code that they can use to invite people # generate a random 8 letter/digit code that they can use to invite people