Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/search/presenters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Search
module Presenters
end
end
require "search/presenters/action"
require "search/presenters/actions"
require "search/presenters/affiliations"
require "search/presenters/breadcrumbs"
require "search/presenters/icons"
Expand Down
11 changes: 0 additions & 11 deletions lib/search/presenters/action.rb

This file was deleted.

25 changes: 25 additions & 0 deletions lib/search/presenters/actions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Search::Presenters::Actions
ACTIONS = [
["email", "Email"],
["text", "Text"],
["citation", "Citation"],
["ris", "Export Citation (EndNote, Zotero, etc.)"],
["link", "Copy link"],
["toggle-selected", "Toggle selected"]
]

def initialize(exclude = [])
exclude = Array(exclude)
@actions = ACTIONS.filter_map do |uid, text|
Action.new(uid: uid, text: text) unless exclude.include?(uid)
end
end

include Enumerable

def each(&block)
@actions.each(&block)
end
end

require "search/presenters/actions/action"
14 changes: 14 additions & 0 deletions lib/search/presenters/actions/action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Search::Presenters::Actions::Action
attr_reader :uid, :text
def initialize(uid:, text:)
@uid = uid
@text = text
end

def to_s
text
end
end

require "search/presenters/actions/action/citation"
require "search/presenters/actions/action/link"
13 changes: 13 additions & 0 deletions lib/search/presenters/actions/action/citation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Search::Presenters::Actions::Action::Citation
def initialize(results)
@results = results || []
end

def csl
@results.map { |record| record.csl }
end

def ris
@results.map { |record| record.ris }
end
end
14 changes: 14 additions & 0 deletions lib/search/presenters/actions/action/link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Search::Presenters::Actions::Action::Link
def initialize(uri)
@uri = uri
end

def uri
if @uri.path.include?("/record")
# Remove query parameters if the URI contains `/record`
Addressable::URI.parse(@uri).tap { |u| u.query = nil }.to_s
else
@uri.to_s
end
end
end
18 changes: 9 additions & 9 deletions lib/search/presenters/page/datastore_static.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
class Search::Presenters::Page
class DatastoreStatic < self
ACTIONS = [
["email", "Email"],
["text", "Text"],
["citation", "Citation"],
["ris", "Export&nbsp;Citation (EndNote,&nbsp;Zotero,&nbsp;etc.)"],
["link", "Copy&nbsp;link"],
["toggle-selected", "Toggle&nbsp;selected"]
].map { |uid, text| Search::Presenters::Action.new(uid: uid, text: text) }
def self.for(slug:, uri:, patron:)
datastore = Search::Datastores.find(slug)
new(datastore: datastore, uri: uri, patron: patron)
Expand Down Expand Up @@ -37,7 +29,15 @@ def page_title
end

def actions
ACTIONS
Search::Presenters::Actions.new(nil)
end

def citation
Search::Presenters::Actions::Action::Citation.new(nil)
end

def copy_link
Search::Presenters::Actions::Action::Link.new(@uri)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/search/presenters/page/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def ris_action_url
end

def actions
ACTIONS.reject { |action| action.uid == "link" }
Search::Presenters::Actions.new(["link"])
end

def show_holdings?
Expand Down
8 changes: 6 additions & 2 deletions lib/search/presenters/page/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ def breadcrumbs
Search::Presenters::Breadcrumbs.new(current_page: CURRENT_PAGE, uri: @uri)
end

def meta_tags
record.meta_tags
def citation
Search::Presenters::Actions::Action::Citation.new([@record])
end

def ris_action_url
"#{@uri}/ris"
end

def meta_tags
record.meta_tags
end

private

def title_parts
Expand Down
4 changes: 4 additions & 0 deletions lib/search/presenters/page/results.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def scripts
super.push("datastores/results/scripts.js")
end

def citation
Search::Presenters::Actions::Action::Citation.new(entries)
end

def ris_action_url
end

Expand Down
35 changes: 35 additions & 0 deletions spec/presenters/actions/action/citation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
RSpec.describe Search::Presenters::Actions::Action::Citation do
before(:each) do
@results = [
OpenStruct.new(csl: "CSL Citation 1", ris: "RIS Citation 1"),
OpenStruct.new(csl: "CSL Citation 2", ris: "RIS Citation 2"),
OpenStruct.new(csl: "CSL Citation 3", ris: "RIS Citation 3")
]
end

subject do
described_class.new(@results)
end

context "#csl" do
it "returns an array of CSL citations" do
expect(subject.csl).to eq(@results.map { |record| record.csl })
end

it "returns an empty array if there are no results" do
empty_subject = described_class.new([])
expect(empty_subject.csl).to eq([])
end
end

context "#ris" do
it "returns an array of RIS citations" do
expect(subject.ris).to eq(@results.map { |record| record.ris })
end

it "returns an empty array if there are no results" do
empty_subject = described_class.new([])
expect(empty_subject.ris).to eq([])
end
end
end
21 changes: 21 additions & 0 deletions spec/presenters/actions/action/link_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
RSpec.describe Search::Presenters::Actions::Action::Link do
url = "/catalog?query=example"
before(:each) do
@uri = URI.parse(url)
end

subject do
described_class.new(@uri)
end

context "#uri" do
it "returns the full URI as a string" do
expect(subject.uri).to eq(url)
end

it "returns the URI without query parameters if it contains `/record`" do
@uri = URI.parse("/record/123?query=example")
expect(subject.uri).to eq("/record/123")
end
end
end
16 changes: 16 additions & 0 deletions spec/presenters/actions/action_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
RSpec.describe Search::Presenters::Actions::Action do
before(:each) do
@uid = "email"
@text = "Email"
end

subject do
described_class.new(uid: @uid, text: @text)
end

context "#to_s" do
it "returns the text of the action" do
expect(subject.to_s).to eq(@text)
end
end
end
20 changes: 20 additions & 0 deletions spec/presenters/actions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
RSpec.describe Search::Presenters::Actions do
before(:each) do
@exclude = ["link"]
end

subject do
described_class.new(@exclude)
end

context "#each" do
it "iterates over the actions with the body" do
actions = []
subject.each do |action|
actions << action.uid
end

expect(actions).to eq(["email", "text", "citation", "ris", "toggle-selected"])
end
end
end
11 changes: 1 addition & 10 deletions views/datastores/partials/actions/action/_link.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<%
# Get the full URL with paths
uri = S.base_url + request.path
# Check if not viewing a full record, and there are query parameters
if !request.path.include?("/record") && !request.query_string.empty?
# Apply the query parameters to the full path
uri = uri + "?" + request.query_string
end
%>
<%= erb :"datastores/partials/actions/_alert", locals: { type: "success", message: "Link copied!" } %>
<form class="actions__form action__link--form">
<label for="action__link--input">Copy link</label>
<input type="url" id="action__link--input" name="action__link--input" autocomplete="off" value="<%= uri %>">
<input type="url" id="action__link--input" name="action__link--input" autocomplete="off" value="<%= @presenter.copy_link.uri %>">
<button class="link__copy" type="submit" disabled>Copy link</button>
</form>
10 changes: 1 addition & 9 deletions views/datastores/partials/actions/action/citation/_csl.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
<%
csl = []
if @record
csl = [@record.csl]
elsif defined?(@presenter.entries)
csl = @presenter.entries.map { |entry| entry.csl }
end
%>
<textarea class="citation__csl" readonly><%= csl.to_json %></textarea>
<textarea class="citation__csl" readonly><%= @presenter.citation&.csl&.to_json %></textarea>
10 changes: 1 addition & 9 deletions views/datastores/partials/actions/action/ris/_textarea.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
<%
ris = []
if @record
ris = [@record.ris]
elsif defined?(@presenter.entries)
ris = @presenter.entries.map { |entry| entry.ris }
end
%>
<textarea class="citation__ris" readonly><%= ris.to_json %></textarea>
<textarea class="citation__ris" readonly><%= @presenter.citation&.ris&.to_json %></textarea>
Loading