From 6d6a5099e96e76b1f790a0726d9728abdf951ec1 Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Wed, 5 Oct 2016 10:45:39 -0400 Subject: [PATCH] Enable access to Most Used and Recently Used metacodes in lists and carousel (#708) * used and recent * enable most used and recent in all metacode select situations * selected changed to active at some point * switch recent and most used positions * remove index doc page --- app/helpers/application_helper.rb | 19 +++++++- app/models/user.rb | 22 +++++++++ app/views/shared/_metacodeoptions.html.erb | 26 +++++++++++ app/views/shared/_switchmetacodes.html.erb | 54 ++++++++++++++++++++-- frontend/src/Metamaps/Create.js | 2 +- 5 files changed, 117 insertions(+), 6 deletions(-) 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 @@
+ <% most_used = user_most_used_metacodes() %> +
+ <% @list = '' %> + <% most_used.each_with_index do |m, index| %> + <% @list += '
  • ' + m.name + '

    ' + m.name.downcase + '

  • ' %> + <% end %> +
    +

    The 5 Metacodes you've used the most.

    +
    +
      + <%= @list.html_safe %> +
    +
    +
    +
    + +
    <% allMetacodeSets.each_with_index do |m, localindex| %>
    @@ -39,7 +85,7 @@
    - @@ -62,7 +108,7 @@
    - diff --git a/frontend/src/Metamaps/Create.js b/frontend/src/Metamaps/Create.js index 92271223..87b91540 100644 --- a/frontend/src/Metamaps/Create.js +++ b/frontend/src/Metamaps/Create.js @@ -32,7 +32,7 @@ const Create = { // // SWITCHING METACODE SETS $('#metacodeSwitchTabs').tabs({ - selected: self.selectedMetacodeSetIndex + active: self.selectedMetacodeSetIndex }).addClass('ui-tabs-vertical ui-helper-clearfix') $('#metacodeSwitchTabs .ui-tabs-nav li').removeClass('ui-corner-top').addClass('ui-corner-left') $('.customMetacodeList li').click(self.toggleMetacodeSelected) // within the custom metacode set tab