diff --git a/ruby/lib/jam_ruby/models/band.rb b/ruby/lib/jam_ruby/models/band.rb index a0c3a5033..2d656dba0 100644 --- a/ruby/lib/jam_ruby/models/band.rb +++ b/ruby/lib/jam_ruby/models/band.rb @@ -69,6 +69,7 @@ module JamRuby end def follower_count + # FIXME: this could be a lot of followers; calling size loads all the data into memory self.followers.size end diff --git a/ruby/lib/jam_ruby/models/band_search.rb b/ruby/lib/jam_ruby/models/band_search.rb index 416a55ccf..327e40574 100644 --- a/ruby/lib/jam_ruby/models/band_search.rb +++ b/ruby/lib/jam_ruby/models/band_search.rb @@ -77,6 +77,10 @@ module JamRuby PERF_SAMPLES_VALS = TOUR_OPTION_VALS.clone PERF_SAMPLES = TOUR_OPTIONS.clone + COUNT_FOLLOW = :count_follow + COUNT_RECORD = :count_record + COUNT_SESSION = :count_session + def self.json_schema return @@jschema if @@jschema @@jschema = { @@ -241,16 +245,19 @@ module JamRuby rel.includes([:instruments]) end - def process_results_page(_results) + def _process_results_page(_results) @results = _results if user - @user_counters = @results.inject({}) { |hh,val| hh[val.id] = []; hh } - mids = "'#{@results.map(&:id).join("','")}'" + @user_counters = @results.inject({}) { |hh,val| hh[val.id] = {}; hh } # this gets counts for each search result @results.each do |bb| - counters = { } - @user_counters[bb.id] << counters + counters = { + COUNT_FOLLOW => Follow.where(:followable_id => bb.id).count, + COUNT_RECORD => Recording.where(:band_id => bb.id).count, + COUNT_SESSION => MusicSession.where(:band_id => bb.id).count + } + @user_counters[bb.id] = counters end else @user_counters = {} @@ -262,7 +269,7 @@ module JamRuby def _count(musician, key) if mm = @user_counters[musician.id] - return mm.detect { |ii| ii.is_a?(Hash) }[key] + return mm[key] end if @user_counters 0 end @@ -273,10 +280,6 @@ module JamRuby _count(musician, COUNT_FOLLOW) end - def friend_count(musician) - _count(musician, COUNT_FRIEND) - end - def record_count(musician) _count(musician, COUNT_RECORD) end @@ -285,13 +288,6 @@ module JamRuby _count(musician, COUNT_SESSION) end - def is_friend?(musician) - if mm = @user_counters[musician.id] - return mm.include?(RESULT_FRIEND) - end if @user_counters - false - end - def is_follower?(musician) if mm = @user_counters[musician.id] return mm.include?(RESULT_FOLLOW) @@ -352,7 +348,7 @@ module JamRuby rel = self.search_includes(rel) @page_count = rel.total_pages - process_results_page(rel.all) + _process_results_page(rel.all) end def description(filter) diff --git a/ruby/spec/jam_ruby/models/band_filter_search_spec.rb b/ruby/spec/jam_ruby/models/band_filter_search_spec.rb index 80d998643..f773ba0f6 100644 --- a/ruby/spec/jam_ruby/models/band_filter_search_spec.rb +++ b/ruby/spec/jam_ruby/models/band_filter_search_spec.rb @@ -53,6 +53,12 @@ describe 'Band Search Model' do expect(search.results.count).to eq(Band.count) end + it "has follower counts" do + Follow.create(user_id: searcher.id, followable: Band.first) + search.search_results_page + expect(search.user_counters[Band.first.id][BandSearch::COUNT_FOLLOW]).to eq(1) + end + end describe "filtering by keys" do diff --git a/web/app/assets/javascripts/member_search_filter.js.coffee b/web/app/assets/javascripts/member_search_filter.js.coffee index fc40a05af..ec3a050c6 100644 --- a/web/app/assets/javascripts/member_search_filter.js.coffee +++ b/web/app/assets/javascripts/member_search_filter.js.coffee @@ -790,12 +790,30 @@ context.JK.BandSearchFilter = class BandSearchFilter extends BaseSearchFilter context.JK.bindHoverEvents() return - _bindMessageBand: () => - - _bindFriendBand: () => - _bindFollowBand: () => - + objThis = this + if 0 == $(this).closest('.button-orange').size() + return false + $(this).click (ee) -> + ee.preventDefault() + return + evt.stopPropagation() + newFollowing = {} + newFollowing.band_id = $(this).parent().data('band-id') + url = '/api/users/' + context.JK.currentUserId + '/followings' + $.ajax + type: 'POST' + dataType: 'json' + contentType: 'application/json' + url: url + data: JSON.stringify(newFollowing) + processData: false + success: (response) -> + $('div[data-band-id=' + newFollowing.band_id + '] .search-m-follow').removeClass('button-orange').addClass 'button-grey' + return + error: app.ajaxError(arguments) + return + _formatLocation: (band) -> friendRequestCallback: (user_id)=> diff --git a/web/app/views/api_search/index.rabl b/web/app/views/api_search/index.rabl index b2b043c61..812bf4f3d 100644 --- a/web/app/views/api_search/index.rabl +++ b/web/app/views/api_search/index.rabl @@ -49,7 +49,6 @@ if @search.is_a?(BaseSearch) end node :follow_count do |musician| @search.follow_count(musician) end - node :friend_count do |musician| @search.friend_count(musician) end node :recording_count do |musician| @search.record_count(musician) end node :session_count do |musician| @search.session_count(musician) end diff --git a/web/app/views/clients/_band_search_filter.html.slim b/web/app/views/clients/_band_search_filter.html.slim index 0ca5e8be0..958c5b66c 100644 --- a/web/app/views/clients/_band_search_filter.html.slim +++ b/web/app/views/clients/_band_search_filter.html.slim @@ -183,18 +183,13 @@ script#template-find-band-row type="text/template" .clearall script#template-band-action-btns type="text/template" - a.button-orange.smallbutton href="{profile_url}" - PROFILE - a.smallbutton.search-m-follow href="#" class="{button_follow}" - FOLLOW + a.button-orange.smallbutton href="{profile_url}" PROFILE + a.smallbutton.search-m-follow href="#" class="{button_follow}" FOLLOW script#template-band-edit-btns type="text/template" - a.button-orange.smallbutton href="{profile_url}" - PROFILE - a.button-orange.smallbutton href="{band_edit_url}" - EDIT BAND - a.button-orange.smallbutton href="{band_member_url}" - INVITE + a.button-orange.smallbutton href="{profile_url}" PROFILE + a.button-orange.smallbutton href="{band_edit_url}" EDIT BAND + a.button-orange.smallbutton href="{band_member_url}" INVITE script#template-band-player-info type="text/template" tr