From 271acafb79af0b28a588008f564bce5e0c4a1b0d Mon Sep 17 00:00:00 2001 From: Brian Keese Date: Tue, 26 Aug 2014 14:51:28 -0400 Subject: [PATCH] Add bookmark count to nav button --- .../javascripts/blacklight/bookmark_toggle.js | 9 +++++++-- .../javascripts/blacklight/checkbox_submit.js | 2 +- app/controllers/bookmarks_controller.rb | 8 ++++---- app/views/_user_util_links.html.erb | 7 +++++-- spec/controllers/bookmarks_controller_spec.rb | 12 +++++++----- spec/views/_user_util_links.html.erb_spec.rb | 19 +++++++++++++++++++ 6 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 spec/views/_user_util_links.html.erb_spec.rb diff --git a/app/assets/javascripts/blacklight/bookmark_toggle.js b/app/assets/javascripts/blacklight/bookmark_toggle.js index a4ef51bda2..015e228945 100644 --- a/app/assets/javascripts/blacklight/bookmark_toggle.js +++ b/app/assets/javascripts/blacklight/bookmark_toggle.js @@ -4,8 +4,13 @@ //change form submit toggle to checkbox Blacklight.do_bookmark_toggle_behavior = function() { $(Blacklight.do_bookmark_toggle_behavior.selector).bl_checkbox_submit({ - //css_class is added to elements added, plus used for id base - css_class: "toggle_bookmark" + //css_class is added to elements added, plus used for id base + css_class: "toggle_bookmark", + success: function(checked, response) { + if (response.bookmarks) { + $('#bookmarks_nav span[data-role=bookmark-counter]').text(response.bookmarks.count); + } + } }); }; Blacklight.do_bookmark_toggle_behavior.selector = "form.bookmark_toggle"; diff --git a/app/assets/javascripts/blacklight/checkbox_submit.js b/app/assets/javascripts/blacklight/checkbox_submit.js index ceacc67e28..98ebba22fe 100644 --- a/app/assets/javascripts/blacklight/checkbox_submit.js +++ b/app/assets/javascripts/blacklight/checkbox_submit.js @@ -109,7 +109,7 @@ update_state_for(checked); label.removeAttr("disabled"); checkbox.removeAttr("disabled"); - options.success.call(form, checked); + options.success.call(form, checked, xhr.responseJSON); } else { alert("Error"); update_state_for(checked); diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb index b1c283a03f..4fb04cb4fd 100644 --- a/app/controllers/bookmarks_controller.rb +++ b/app/controllers/bookmarks_controller.rb @@ -64,11 +64,11 @@ def create current_or_guest_user.save! unless current_or_guest_user.persisted? success = @bookmarks.all? do |bookmark| - current_or_guest_user.bookmarks.create(bookmark) unless current_or_guest_user.bookmarks.where(bookmark).exists? + current_or_guest_user.bookmarks.where(bookmark).exists? || current_or_guest_user.bookmarks.create(bookmark) end if request.xhr? - success ? head(:no_content) : render(:text => "", :status => "500") + success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count }}) : render(:text => "", :status => "500") else if @bookmarks.length > 0 && success flash[:notice] = I18n.t('blacklight.bookmarks.add.success', :count => @bookmarks.length) @@ -96,10 +96,10 @@ def destroy redirect_to :back else # ajaxy request needs no redirect and should not have flash set - success ? head(:no_content) : render(:text => "", :status => "500") + success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count }}) : render(:text => "", :status => "500") end end - + def clear if current_or_guest_user.bookmarks.clear flash[:notice] = I18n.t('blacklight.bookmarks.clear.success') diff --git a/app/views/_user_util_links.html.erb b/app/views/_user_util_links.html.erb index 96f78b36d1..cb65fc8b57 100644 --- a/app/views/_user_util_links.html.erb +++ b/app/views/_user_util_links.html.erb @@ -2,7 +2,10 @@ <% end %> - \ No newline at end of file + diff --git a/spec/controllers/bookmarks_controller_spec.rb b/spec/controllers/bookmarks_controller_spec.rb index 168c3bb49b..ee256f543e 100644 --- a/spec/controllers/bookmarks_controller_spec.rb +++ b/spec/controllers/bookmarks_controller_spec.rb @@ -3,13 +3,14 @@ describe BookmarksController do # jquery 1.9 ajax does error callback if 200 returns empty body. so use 204 instead. describe "update" do - it "has a 204 status code when creating a new one" do + it "has a 200 status code when creating a new one" do xhr :put, :update, :id => '2007020969', :format => :js expect(response).to be_success - expect(response.code).to eq "204" + expect(response.code).to eq "200" + expect(JSON.parse(response.body)["bookmarks"]["count"]).to eq 1 end - it "has a 500 status code when fails is success" do + it "has a 500 status code when create is not success" do allow(@controller).to receive_message_chain(:current_or_guest_user, :existing_bookmark_for).and_return(false) allow(@controller).to receive_message_chain(:current_or_guest_user, :persisted?).and_return(true) allow(@controller).to receive_message_chain(:current_or_guest_user, :bookmarks, :where, :exists?).and_return(false) @@ -25,10 +26,11 @@ @controller.send(:current_or_guest_user).bookmarks.create! document_id: '2007020969', document_type: "SolrDocument" end - it "has a 204 status code when delete is success" do + it "has a 200 status code when delete is success" do xhr :delete, :destroy, :id => '2007020969', :format => :js expect(response).to be_success - expect(response.code).to eq "204" + expect(response.code).to eq "200" + expect(JSON.parse(response.body)["bookmarks"]["count"]).to eq 0 end it "has a 500 status code when delete is not success" do diff --git a/spec/views/_user_util_links.html.erb_spec.rb b/spec/views/_user_util_links.html.erb_spec.rb new file mode 100644 index 0000000000..ef82e6b1eb --- /dev/null +++ b/spec/views/_user_util_links.html.erb_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +describe "_user_util_links" do + + let :blacklight_config do + Blacklight::Configuration.new + end + + it "should render the correct bookmark count" do + count = rand(99) + allow(view).to receive(:blacklight_config).and_return(blacklight_config) + allow(view).to receive(:render_bookmarks_control?).and_return true + allow(view).to receive(:has_user_authentication_provider?). and_return false + allow(view).to receive_message_chain(:current_or_guest_user, :bookmarks, :count).and_return(count) + render :partial => "user_util_links" + expect(rendered).to have_selector('#bookmarks_nav span[data-role=bookmark-counter]', text: "#{count}") + end + +end