Skip to content

Commit

Permalink
Merge pull request #980 from avalonmediasystem/feature/bookmark_count
Browse files Browse the repository at this point in the history
Feature/bookmark count
  • Loading branch information
jcoyne committed Aug 28, 2014
2 parents 93d9cea + 271acaf commit 5cadf4d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
9 changes: 7 additions & 2 deletions app/assets/javascripts/blacklight/bookmark_toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/blacklight/checkbox_submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/bookmarks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
7 changes: 5 additions & 2 deletions app/views/_user_util_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<ul class="nav navbar-nav">
<% if render_bookmarks_control? %>
<li>
<%= link_to t('blacklight.header_links.bookmarks'), bookmarks_path %>
<%= link_to bookmarks_path, id:'bookmarks_nav' do %>
<%= t('blacklight.header_links.bookmarks') %>
(<span data-role='bookmark-counter'><%= current_or_guest_user.bookmarks.count %></span>)
<% end %>
</li>
<% end %>
<% if has_user_authentication_provider? and current_user %>
Expand Down Expand Up @@ -33,4 +36,4 @@
<% end %>
</ul>
<% end %>
</div>
</div>
12 changes: 7 additions & 5 deletions spec/controllers/bookmarks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/views/_user_util_links.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5cadf4d

Please sign in to comment.