Skip to content
Closed
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
5 changes: 0 additions & 5 deletions app/helpers/rubygems_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ def download_link(version)
link_to_page :download, "/downloads/#{version.full_name}.gem"
end

def documentation_link(version, linkset)
return unless linkset.nil? || linkset.docs.blank?
link_to_page :docs, version.documentation_path
end

def badge_link(rubygem)
badge_url = "https://badge.fury.io/rb/#{rubygem.name}/install"
link_to t(".links.badge"), badge_url, class: "gem__link t-list__item", id: :badge
Expand Down
15 changes: 12 additions & 3 deletions app/models/linkset.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
class Linkset < ActiveRecord::Base
belongs_to :rubygem

LINKS = %w(home code docs wiki mail bugs).freeze
LINKS = {
'home' => 'homepage_uri',
'code' => 'source_code_uri',
'docs' => 'documentation_uri',
'wiki' => 'wiki_uri',
'mail' => 'mailing_list_uri',
'bugs' => 'bug_tracker_uri'
}.freeze

LINKS.each do |url|
LINKS.each do |url, aka|
validates_formatting_of url.to_sym,
using: :url,
allow_nil: true,
allow_blank: true,
message: "does not appear to be a valid URL"

alias_attribute aka.to_sym, url.to_sym
end

def empty?
LINKS.map { |link| attributes[link] }.all?(&:blank?)
LINKS.keys.map { |link| attributes[link] }.all?(&:blank?)
end

def update_attributes_from_gem_specification!(spec)
Expand Down
12 changes: 6 additions & 6 deletions app/models/rubygem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ def payload(version = versions.most_recent, protocol = Gemcutter::PROTOCOL, host
'sha' => version.sha256_hex,
'project_uri' => "#{protocol}://#{host_with_port}/gems/#{name}",
'gem_uri' => "#{protocol}://#{host_with_port}/gems/#{version.full_name}.gem",
'homepage_uri' => linkset.try(:home),
'wiki_uri' => linkset.try(:wiki),
'documentation_uri' => linkset.try(:docs).presence || version.documentation_path,
'mailing_list_uri' => linkset.try(:mail),
'source_code_uri' => linkset.try(:code),
'bug_tracker_uri' => linkset.try(:bugs),
'homepage_uri' => version.link_uri(:homepage_uri, linkset),
'wiki_uri' => version.link_uri(:wiki_uri, linkset),
'documentation_uri' => version.link_uri(:documentation_uri, linkset),
'mailing_list_uri' => version.link_uri(:mailing_list_uri, linkset),
'source_code_uri' => version.link_uri(:source_code_uri, linkset),
'bug_tracker_uri' => version.link_uri(:bug_tracker_uri, linkset),
'dependencies' => {
'development' => deps.select { |r| r.rubygem && 'development' == r.scope },
'runtime' => deps.select { |r| r.rubygem && 'runtime' == r.scope }
Expand Down
5 changes: 5 additions & 0 deletions app/models/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ def assign_required_rubygems_version!
update_column(:required_rubygems_version, required_rubygems_version.to_s)
end

def link_uri(name, linkset = nil)
metadata[name.to_s].presence || linkset.try(name).presence ||
(name.to_s == "documentation_uri" ? documentation_path : nil)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of this condition, can we add the documentation_uri method on the linkset?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to remove this.

linkset#documentation_uri has access to rubygem#name, but does not have access to version#number. Ideas?

  def documentation_path
    "http://www.rubydoc.info/gems/#{rubygem.name}/#{version.number}"
  end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed this in the alt solution. thanks for raising this concern

end

def documentation_path
"http://www.rubydoc.info/gems/#{rubygem.name}/#{number}"
end
Expand Down
9 changes: 3 additions & 6 deletions app/views/rubygems/_aside.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,13 @@
<%= link_to t('edit'), edit_rubygem_path(@rubygem), :class => "gem__link t-list__item", :id => "edit" if @rubygem.owned_by?(current_user) %>

<% if @latest_version.indexed %>
<% if @rubygem.linkset.present? %>
<%- Linkset::LINKS.each do |link| %>
<%= link_to_page link, @rubygem.linkset.public_send(link) %>
<%- end %>
<% end %>
<%- Linkset::LINKS.each do |link, attribute| %>
<%= link_to_page link, @latest_version.link_uri(attribute, @rubygem.linkset) %>
<%- end %>

<%= download_link(@latest_version) %>
<% end %>

<%= documentation_link(@latest_version, @rubygem.linkset) %>
<%= badge_link(@rubygem) %>
<%= subscribe_link(@rubygem) if @latest_version.indexed %>
<%= unsubscribe_link(@rubygem) %>
Expand Down
17 changes: 0 additions & 17 deletions test/unit/helpers/rubygems_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,6 @@ class RubygemsHelperTest < ActionView::TestCase
assert_equal "March 18, 2011", nice_date_for(time)
end

should "link to docs if no docs link is set" do
version = build(:version)
linkset = build(:linkset, docs: nil)

@virtual_path = "rubygems.show"
link = documentation_link(version, linkset)
assert link.include?(version.documentation_path)
end

should "not link to docs if docs link is set" do
version = build(:version)
linkset = build(:linkset)

link = documentation_link(version, linkset)
assert link.blank?
end

should "link to the badge" do
rubygem = create(:rubygem)
url = "https://badge.fury.io/rb/#{rubygem.name}/install"
Expand Down
12 changes: 11 additions & 1 deletion test/unit/linkset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@ class LinksetTest < ActiveSupport::TestCase
end

should "be empty with no links filled out" do
Linkset::LINKS.each do |link|
Linkset::LINKS.keys.each do |link|
@linkset.send("#{link}=", nil)
end
assert @linkset.empty?
end

should "have api uri getter" do
@linkset.docs = "http://example.com/docs"
assert_equal "http://example.com/docs", @linkset.documentation_uri
end

should "have api uri setter" do
@linkset.documentation_uri = "http://example.com/docs"
assert_equal "http://example.com/docs", @linkset.docs
end
end

context "with a Gem::Specification" do
Expand Down
38 changes: 38 additions & 0 deletions test/unit/rubygem_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,44 @@ class RubygemTest < ActiveSupport::TestCase
assert_equal run_dep.name, doc.at_css("dependencies runtime dependency name").content
end

context "with metadata" do
setup do
@rubygem = build(:rubygem)
@version = create(:version, rubygem: @rubygem)
end

should "prefer metadata over links in JSON" do
@version.update_attributes!(
metadata: {
"homepage_uri" => "http://example.com/home",
"wiki_uri" => "http://example.com/wiki",
"documentation_uri" => "http://example.com/docs",
"mailing_list_uri" => "http://example.com/mail",
"source_code_uri" => "http://example.com/code",
"bug_tracker_uri" => "http://example.com/bugs"
}
)

hash = MultiJson.load(@rubygem.to_json)

assert_equal "http://example.com/home", hash["homepage_uri"]
assert_equal "http://example.com/wiki", hash["wiki_uri"]
assert_equal "http://example.com/docs", hash["documentation_uri"]
assert_equal "http://example.com/mail", hash["mailing_list_uri"]
assert_equal "http://example.com/code", hash["source_code_uri"]
assert_equal "http://example.com/bugs", hash["bug_tracker_uri"]
end

should "return version documentation url if metadata and linkset docs is empty" do
@version.update_attributes!(metadata: {})
@rubygem.linkset.docs = ""
@rubygem.save
hash = JSON.load(@rubygem.to_json)

assert_equal @version.documentation_path, hash["documentation_uri"]
end
end

context "with a linkset" do
setup do
@rubygem = build(:rubygem)
Expand Down