Skip to content

Commit

Permalink
Extract #is_bookmarked? helper
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Mar 13, 2015
1 parent 0006292 commit 4adc5a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/helpers/blacklight/catalog_helper_behavior.rb
Expand Up @@ -223,6 +223,10 @@ def current_bookmarks response = nil
response ||= @response
@current_bookmarks ||= current_or_guest_user.bookmarks_for_documents(response.documents).to_a
end

def is_bookmarked? document
current_bookmarks.any? { |x| x.document_id == document.id and x.document_type == document.class }
end

def render_marc_tools
return unless defined? Blacklight::Marc
Expand All @@ -247,4 +251,5 @@ def render_endnote_action? config, options = {}
def render_librarian_view_control? config, options = {}
respond_to? :librarian_view_catalog_path and options[:document] and options[:document].respond_to?(:to_marc)
end

end
2 changes: 1 addition & 1 deletion app/views/catalog/_bookmark_control.html.erb
Expand Up @@ -4,7 +4,7 @@
# but it was simpler to leave them seperate instead of DRYing them, got confusing trying that.
# the data-doc-id attribute is used by our JS that converts to a checkbox/label.
-%>
<% if current_bookmarks.find { |x| x.document_id == document.id and x.document_type == document.class }.blank? %>
<% unless is_bookmarked? document %>
<%= form_tag( bookmark_path( document ), :method => :put, :class => "bookmark_toggle", "data-doc-id" => document.id, :'data-present' => t('blacklight.search.bookmarks.present'), :'data-absent' => t('blacklight.search.bookmarks.absent'), :'data-inprogress' => t('blacklight.search.bookmarks.inprogress')) do %>
<%= submit_tag(t('blacklight.bookmarks.add.button'), :id => "bookmark_toggle_#{document.id.to_s.parameterize}", :class => "bookmark_add") %>
Expand Down
18 changes: 18 additions & 0 deletions spec/helpers/catalog_helper_spec.rb
Expand Up @@ -305,4 +305,22 @@ def render_grouped_response?
end
end

describe "#is_bookmarked?" do

let(:bookmark) { Bookmark.new document: bookmarked_document }
let(:bookmarked_document) { SolrDocument.new(id: 'a') }

before do
allow(helper).to receive(:current_bookmarks).and_return([bookmark])
end

it "should be bookmarked if the document is in the bookmarks" do
expect(helper.is_bookmarked?(bookmarked_document)).to eq true
end

it "should not be bookmarked if the document is not listed in the bookmarks" do
expect(helper.is_bookmarked?(SolrDocument.new(id: 'b'))).to eq false
end
end

end

0 comments on commit 4adc5a5

Please sign in to comment.