Skip to content

Commit 5cadf4d

Browse files
committed
Merge pull request #980 from avalonmediasystem/feature/bookmark_count
Feature/bookmark count
2 parents 93d9cea + 271acaf commit 5cadf4d

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

app/assets/javascripts/blacklight/bookmark_toggle.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
//change form submit toggle to checkbox
55
Blacklight.do_bookmark_toggle_behavior = function() {
66
$(Blacklight.do_bookmark_toggle_behavior.selector).bl_checkbox_submit({
7-
//css_class is added to elements added, plus used for id base
8-
css_class: "toggle_bookmark"
7+
//css_class is added to elements added, plus used for id base
8+
css_class: "toggle_bookmark",
9+
success: function(checked, response) {
10+
if (response.bookmarks) {
11+
$('#bookmarks_nav span[data-role=bookmark-counter]').text(response.bookmarks.count);
12+
}
13+
}
914
});
1015
};
1116
Blacklight.do_bookmark_toggle_behavior.selector = "form.bookmark_toggle";

app/assets/javascripts/blacklight/checkbox_submit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
update_state_for(checked);
110110
label.removeAttr("disabled");
111111
checkbox.removeAttr("disabled");
112-
options.success.call(form, checked);
112+
options.success.call(form, checked, xhr.responseJSON);
113113
} else {
114114
alert("Error");
115115
update_state_for(checked);

app/controllers/bookmarks_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ def create
6464
current_or_guest_user.save! unless current_or_guest_user.persisted?
6565

6666
success = @bookmarks.all? do |bookmark|
67-
current_or_guest_user.bookmarks.create(bookmark) unless current_or_guest_user.bookmarks.where(bookmark).exists?
67+
current_or_guest_user.bookmarks.where(bookmark).exists? || current_or_guest_user.bookmarks.create(bookmark)
6868
end
6969

7070
if request.xhr?
71-
success ? head(:no_content) : render(:text => "", :status => "500")
71+
success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count }}) : render(:text => "", :status => "500")
7272
else
7373
if @bookmarks.length > 0 && success
7474
flash[:notice] = I18n.t('blacklight.bookmarks.add.success', :count => @bookmarks.length)
@@ -96,10 +96,10 @@ def destroy
9696
redirect_to :back
9797
else
9898
# ajaxy request needs no redirect and should not have flash set
99-
success ? head(:no_content) : render(:text => "", :status => "500")
99+
success ? render(json: { bookmarks: { count: current_or_guest_user.bookmarks.count }}) : render(:text => "", :status => "500")
100100
end
101101
end
102-
102+
103103
def clear
104104
if current_or_guest_user.bookmarks.clear
105105
flash[:notice] = I18n.t('blacklight.bookmarks.clear.success')

app/views/_user_util_links.html.erb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
<ul class="nav navbar-nav">
33
<% if render_bookmarks_control? %>
44
<li>
5-
<%= link_to t('blacklight.header_links.bookmarks'), bookmarks_path %>
5+
<%= link_to bookmarks_path, id:'bookmarks_nav' do %>
6+
<%= t('blacklight.header_links.bookmarks') %>
7+
(<span data-role='bookmark-counter'><%= current_or_guest_user.bookmarks.count %></span>)
8+
<% end %>
69
</li>
710
<% end %>
811
<% if has_user_authentication_provider? and current_user %>
@@ -33,4 +36,4 @@
3336
<% end %>
3437
</ul>
3538
<% end %>
36-
</div>
39+
</div>

spec/controllers/bookmarks_controller_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
describe BookmarksController do
44
# jquery 1.9 ajax does error callback if 200 returns empty body. so use 204 instead.
55
describe "update" do
6-
it "has a 204 status code when creating a new one" do
6+
it "has a 200 status code when creating a new one" do
77
xhr :put, :update, :id => '2007020969', :format => :js
88
expect(response).to be_success
9-
expect(response.code).to eq "204"
9+
expect(response.code).to eq "200"
10+
expect(JSON.parse(response.body)["bookmarks"]["count"]).to eq 1
1011
end
1112

12-
it "has a 500 status code when fails is success" do
13+
it "has a 500 status code when create is not success" do
1314
allow(@controller).to receive_message_chain(:current_or_guest_user, :existing_bookmark_for).and_return(false)
1415
allow(@controller).to receive_message_chain(:current_or_guest_user, :persisted?).and_return(true)
1516
allow(@controller).to receive_message_chain(:current_or_guest_user, :bookmarks, :where, :exists?).and_return(false)
@@ -25,10 +26,11 @@
2526
@controller.send(:current_or_guest_user).bookmarks.create! document_id: '2007020969', document_type: "SolrDocument"
2627
end
2728

28-
it "has a 204 status code when delete is success" do
29+
it "has a 200 status code when delete is success" do
2930
xhr :delete, :destroy, :id => '2007020969', :format => :js
3031
expect(response).to be_success
31-
expect(response.code).to eq "204"
32+
expect(response.code).to eq "200"
33+
expect(JSON.parse(response.body)["bookmarks"]["count"]).to eq 0
3234
end
3335

3436
it "has a 500 status code when delete is not success" do
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'spec_helper'
2+
3+
describe "_user_util_links" do
4+
5+
let :blacklight_config do
6+
Blacklight::Configuration.new
7+
end
8+
9+
it "should render the correct bookmark count" do
10+
count = rand(99)
11+
allow(view).to receive(:blacklight_config).and_return(blacklight_config)
12+
allow(view).to receive(:render_bookmarks_control?).and_return true
13+
allow(view).to receive(:has_user_authentication_provider?). and_return false
14+
allow(view).to receive_message_chain(:current_or_guest_user, :bookmarks, :count).and_return(count)
15+
render :partial => "user_util_links"
16+
expect(rendered).to have_selector('#bookmarks_nav span[data-role=bookmark-counter]', text: "#{count}")
17+
end
18+
19+
end

0 commit comments

Comments
 (0)