diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 1c9b4da5..03cbfbf4 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -3,13 +3,22 @@ module ApplicationHelper def metacodeset metacodes = current_user.settings.metacodes return false unless metacodes[0].include?('metacodeset') + if metacodes[0].sub('metacodeset-', '') == 'Most' + return 'Most' + elsif metacodes[0].sub('metacodeset-', '') == 'Recent' + return 'Recent' + end MetacodeSet.find(metacodes[0].sub('metacodeset-', '').to_i) end def user_metacodes @m = current_user.settings.metacodes set = metacodeset - @metacodes = if set + @metacodes = if set && set == 'Most' + Metacode.where(id: current_user.mostUsedMetacodes).to_a + elsif set && set == 'Recent' + Metacode.where(id: current_user.recentMetacodes).to_a + elsif set set.metacodes.to_a else Metacode.where(id: @m).to_a @@ -17,6 +26,14 @@ module ApplicationHelper @metacodes.sort! { |m1, m2| m2.name.downcase <=> m1.name.downcase }.rotate!(-1) end + def user_most_used_metacodes + @metacodes = current_user.mostUsedMetacodes.map { |id| Metacode.find(id) } + end + + def user_recent_metacodes + @metacodes = current_user.recentMetacodes.map { |id| Metacode.find(id) } + end + def invite_link "#{request.base_url}/join" + (current_user ? "?code=#{current_user.code}" : '') end diff --git a/app/models/user.rb b/app/models/user.rb index 4da66e57..4f679c1b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -64,6 +64,28 @@ class User < ApplicationRecord json['rtype'] = 'mapper' json end + + def recentMetacodes + array = [] + 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 + + def mostUsedMetacodes + self.topics.to_a.reduce({}) { |memo, topic| + if memo[topic.metacode_id] == nil + memo[topic.metacode_id] = 1 + else + memo[topic.metacode_id] = memo[topic.metacode_id] + 1 + end + + memo + }.to_a.sort{ |a, b| b[1] <=> a[1] }.map{|i| i[0]}.slice(0, 5) + end # generate a random 8 letter/digit code that they can use to invite people def generate_code diff --git a/app/views/shared/_metacodeoptions.html.erb b/app/views/shared/_metacodeoptions.html.erb index a6092c3e..54fb9e48 100644 --- a/app/views/shared/_metacodeoptions.html.erb +++ b/app/views/shared/_metacodeoptions.html.erb @@ -5,6 +5,32 @@
' + m.name.downcase + '
' + m.name.downcase + '