diff --git a/lib/search/presenters.rb b/lib/search/presenters.rb index 12a0d023..6d24a543 100644 --- a/lib/search/presenters.rb +++ b/lib/search/presenters.rb @@ -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" diff --git a/lib/search/presenters/action.rb b/lib/search/presenters/action.rb deleted file mode 100644 index 64f0e2e3..00000000 --- a/lib/search/presenters/action.rb +++ /dev/null @@ -1,11 +0,0 @@ -class Search::Presenters::Action - attr_reader :uid, :text - def initialize(uid:, text:) - @uid = uid - @text = text - end - - def to_s - text - end -end diff --git a/lib/search/presenters/actions.rb b/lib/search/presenters/actions.rb new file mode 100644 index 00000000..d39185fc --- /dev/null +++ b/lib/search/presenters/actions.rb @@ -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" diff --git a/lib/search/presenters/actions/action.rb b/lib/search/presenters/actions/action.rb new file mode 100644 index 00000000..97730956 --- /dev/null +++ b/lib/search/presenters/actions/action.rb @@ -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" diff --git a/lib/search/presenters/actions/action/citation.rb b/lib/search/presenters/actions/action/citation.rb new file mode 100644 index 00000000..b9a5130a --- /dev/null +++ b/lib/search/presenters/actions/action/citation.rb @@ -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 diff --git a/lib/search/presenters/actions/action/link.rb b/lib/search/presenters/actions/action/link.rb new file mode 100644 index 00000000..918615e7 --- /dev/null +++ b/lib/search/presenters/actions/action/link.rb @@ -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 diff --git a/lib/search/presenters/page/datastore_static.rb b/lib/search/presenters/page/datastore_static.rb index 861f7b84..5bd2bb79 100644 --- a/lib/search/presenters/page/datastore_static.rb +++ b/lib/search/presenters/page/datastore_static.rb @@ -1,13 +1,5 @@ class Search::Presenters::Page class DatastoreStatic < self - ACTIONS = [ - ["email", "Email"], - ["text", "Text"], - ["citation", "Citation"], - ["ris", "Export Citation (EndNote, Zotero, etc.)"], - ["link", "Copy link"], - ["toggle-selected", "Toggle 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) @@ -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 diff --git a/lib/search/presenters/page/list.rb b/lib/search/presenters/page/list.rb index cda7ae2b..7ba4e11d 100644 --- a/lib/search/presenters/page/list.rb +++ b/lib/search/presenters/page/list.rb @@ -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? diff --git a/lib/search/presenters/page/record.rb b/lib/search/presenters/page/record.rb index e4108ba3..9973e4bd 100644 --- a/lib/search/presenters/page/record.rb +++ b/lib/search/presenters/page/record.rb @@ -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 diff --git a/lib/search/presenters/page/results.rb b/lib/search/presenters/page/results.rb index 5c0c2fcf..1f188463 100644 --- a/lib/search/presenters/page/results.rb +++ b/lib/search/presenters/page/results.rb @@ -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 diff --git a/spec/presenters/actions/action/citation_spec.rb b/spec/presenters/actions/action/citation_spec.rb new file mode 100644 index 00000000..59324708 --- /dev/null +++ b/spec/presenters/actions/action/citation_spec.rb @@ -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 diff --git a/spec/presenters/actions/action/link_spec.rb b/spec/presenters/actions/action/link_spec.rb new file mode 100644 index 00000000..0c010610 --- /dev/null +++ b/spec/presenters/actions/action/link_spec.rb @@ -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 diff --git a/spec/presenters/actions/action_spec.rb b/spec/presenters/actions/action_spec.rb new file mode 100644 index 00000000..699e3a2a --- /dev/null +++ b/spec/presenters/actions/action_spec.rb @@ -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 diff --git a/spec/presenters/actions_spec.rb b/spec/presenters/actions_spec.rb new file mode 100644 index 00000000..a51ad00d --- /dev/null +++ b/spec/presenters/actions_spec.rb @@ -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 diff --git a/views/datastores/partials/actions/action/_link.erb b/views/datastores/partials/actions/action/_link.erb index 6d0558fa..234dd33e 100644 --- a/views/datastores/partials/actions/action/_link.erb +++ b/views/datastores/partials/actions/action/_link.erb @@ -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!" } %> diff --git a/views/datastores/partials/actions/action/citation/_csl.erb b/views/datastores/partials/actions/action/citation/_csl.erb index 60eb6f52..363967f2 100644 --- a/views/datastores/partials/actions/action/citation/_csl.erb +++ b/views/datastores/partials/actions/action/citation/_csl.erb @@ -1,9 +1 @@ -<% - csl = [] - if @record - csl = [@record.csl] - elsif defined?(@presenter.entries) - csl = @presenter.entries.map { |entry| entry.csl } - end -%> - + diff --git a/views/datastores/partials/actions/action/ris/_textarea.erb b/views/datastores/partials/actions/action/ris/_textarea.erb index a0a97c0b..94bb095a 100644 --- a/views/datastores/partials/actions/action/ris/_textarea.erb +++ b/views/datastores/partials/actions/action/ris/_textarea.erb @@ -1,9 +1 @@ -<% - ris = [] - if @record - ris = [@record.ris] - elsif defined?(@presenter.entries) - ris = @presenter.entries.map { |entry| entry.ris } - end -%> - +