Skip to content

Commit

Permalink
Display licenses for gem versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Lesperance & Matt Parker authored and moonmaster9000 committed Nov 16, 2012
1 parent c93e65c commit b49db33
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -18,3 +18,4 @@ pkg/
server/
tmp/
tags
.idea
8 changes: 8 additions & 0 deletions app/helpers/rubygems_helper.rb
@@ -1,4 +1,12 @@
module RubygemsHelper
def formatted_licenses(license_names)
if license_names.blank?
t(".no_licenses")
else
Array(license_names).join ", "
end
end

def link_to_page(text, url)
link_to(text, url, :rel => 'nofollow') if url.present?
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/version.rb
Expand Up @@ -7,6 +7,8 @@ class Version < ActiveRecord::Base
after_create :full_nameify!
after_save :reorder_versions

serialize :licenses

validates :number, :format => {:with => /\A#{Gem::Version::VERSION_PATTERN}\z/}
validates :platform, :format => {:with => Rubygem::NAME_PATTERN}

Expand Down Expand Up @@ -141,6 +143,7 @@ def update_attributes_from_gem_specification!(spec)
:authors => spec.authors,
:description => spec.description,
:summary => spec.summary,
:licenses => spec.licenses,
:built_at => spec.date,
:indexed => true
)
Expand Down Expand Up @@ -183,6 +186,7 @@ def payload
'summary' => summary,
'platform' => platform,
'prerelease' => prerelease,
'licenses' => licenses
}
end

Expand Down
8 changes: 6 additions & 2 deletions app/views/rubygems/show.html.erb
Expand Up @@ -31,7 +31,7 @@

<% if @latest_version.indexed %>
<div class="who">
<div class="authors">
<div class="authors info-item">
<% if @latest_version.authors.present? %>
<h5><%= t '.authors_header' %></h5>
<p><%= @latest_version.authors %></p>
Expand All @@ -50,12 +50,16 @@
<% end %>
<% if @rubygem.owners.present? %>
<div class="owners">
<div class="owners info-item">
<h5><%= t '.owners_header' %></h5>
<p><%= links_to_owners(@rubygem) %></p>
</div>
<% end %>

<div class="licenses info-item">
<h5><%= t '.licenses_header' %></h5>
<p><%= formatted_licenses @latest_version.licenses %></p>
</div>
</div>

<% if @rubygem.linkset.present? %>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Expand Up @@ -158,6 +158,8 @@ en:
versions_header: Versions
bundler_header: Gemfile
show_all_versions: "Show all versions (%{count} total)"
licenses_header: Licenses
no_licenses: N/A

searches:
show:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20120904155203_add_license_to_versions.rb
@@ -0,0 +1,5 @@
class AddLicenseToVersions < ActiveRecord::Migration
def change
add_column :versions, :licenses, :string
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Expand Up @@ -143,6 +143,7 @@
t.integer "position"
t.boolean "latest"
t.string "full_name"
t.string "licenses"
end

add_index "versions", ["built_at"], :name => "index_versions_on_built_at"
Expand Down
13 changes: 10 additions & 3 deletions features/gems_api.feature
Expand Up @@ -5,15 +5,15 @@ Feature: List gems API

Scenario: Anonymous user lists gems for owner
Given the following user exists:
| email | handle |
| email | handle |
| user@example.com | myhandle |
And the following version exists:
| rubygem | number |
| name: AGem | 1.0.0 |
| name: AGem | 1.0.0 |
And the following ownership exists:
| rubygem | user |
| name: AGem | email: user@example.com |
| name: BGem | |
| name: BGem | |
When I list the gems for owner "myhandle"
Then I should see "AGem"
And I should not see "BGem"
Expand All @@ -33,3 +33,10 @@ Feature: List gems API
| name: MyGem | email: original@owner.org |
When I list the gems with my API key
Then I should see "MyGem"

Scenario: Gem versions include all gem version data
Given the following version exists:
| rubygem | number | licenses |
| name: testgemwithlicense | 1.0.0 | MIT |
When I GET "/api/v1/versions/testgemwithlicense.json"
Then the JSON response should include all of the gem version metadata
23 changes: 21 additions & 2 deletions features/push.feature
Expand Up @@ -3,14 +3,33 @@ Feature: Push Gems
A rubygem developer
Should be able to push gems up to Gemcutter

Scenario: User pushes new gem
Scenario: User pushes new gem and sees metadata
Given I am signed up as "email@person.com"
And I have a gem "RGem" with version "1.2.3"
And I have a gem "RGem" with version "1.2.3" and the following attributes:
| authors | description | license |
| John Doe | The best gem | MIT |
And I have an API key for "email@person.com/password"
When I push the gem "RGem-1.2.3.gem" with my API key
And I visit the gem page for "RGem"
Then I should see "RGem"
And I should see "1.2.3"
And I should see "John Doe"
And I should see "The best gem"
And I should see "MIT"

Scenario: User pushes new gem and sees metadata
Given I am signed up as "email@person.com"
And I have a gem "RGem" with version "1.2.3" and the following attributes:
| authors | description |
| John Doe | The best gem |
And I have an API key for "email@person.com/password"
When I push the gem "RGem-1.2.3.gem" with my API key
And I visit the gem page for "RGem"
Then I should see "RGem"
And I should see "1.2.3"
And I should see "John Doe"
And I should see "The best gem"
And I should see "N/A"

Scenario: User pushes existing version of existing gem
Given I am signed up as "email@person.com"
Expand Down
15 changes: 15 additions & 0 deletions features/step_definitions/api_steps.rb
Expand Up @@ -11,6 +11,20 @@
page.driver.post api_v1_rubygems_path, File.read(path), {"CONTENT_TYPE" => "application/octet-stream"}
end

When /^I GET "(.*?)"$/ do |url|
get url
end

Then /^the JSON response should include all of the gem version metadata$/ do
response = JSON.parse last_response.body

version = Version.first

version.payload.each do |attribute, value|
assert_equal value, response.first[attribute] unless attribute == "built_at"
end
end

When /^I push the gem "([^\"]*)" with my API key$/ do |name|
api_key_header
path = File.join(TEST_DIR, name)
Expand Down Expand Up @@ -81,3 +95,4 @@
Then /the response should contain "([^"]+)"/ do |text|
assert_match text, page.source
end

8 changes: 8 additions & 0 deletions features/step_definitions/gem_steps.rb
Expand Up @@ -25,6 +25,14 @@
build_gemspec(gemspec)
end

Given /^I have a gem "([^\"]*)" with version "([^\"]*)" and the following attributes:$/ do |name, version, table|
gemspec = new_gemspec(name, version, "Gemcutter", "ruby")
table.hashes.first.each do |key, value|
gemspec.send("#{key}=", value)
end
build_gemspec(gemspec)
end

Given /^I have a bad gem "([^\"]*)" with version "([^\"]*)"$/ do |name, version|
gemspec = new_gemspec(name, version, "Bad Gem", "ruby")
gemspec.name = eval(name)
Expand Down
1 change: 1 addition & 0 deletions features/support/gem_helper.rb
Expand Up @@ -24,6 +24,7 @@ def new_gemspec(name, version, summary, platform)
s.rubygems_version = %q{1.3.5}
s.summary = "#{summary}"
s.test_files = []
s.licenses = []
end

def gemspec.validate
Expand Down
17 changes: 11 additions & 6 deletions public/stylesheets/screen.css
Expand Up @@ -641,19 +641,24 @@ table {
border-bottom: 1px solid #B4AC99;
}

.main .info .meta .authors {
display: inline-block;
width: 50%;
.main .info .meta .info-item {
width: 100%;
margin-top: 13px;
}

.main .info .meta .owners h5 {
margin-top: 13px;
.main .info .meta .info-item:first-child {
margin-top: 0;
}

.main .info .meta .owners p {
.main .info .meta .info-item p {
width: 100%;
}

.main .info .meta .authors {
display: inline-block;
width: 50%;
}

.main .info .meta .owners img {
margin-right: 5px;
}
Expand Down
1 change: 1 addition & 0 deletions test/factories.rb
Expand Up @@ -80,6 +80,7 @@
indexed true
number
platform "ruby"
licenses "MIT"
rubygem
end

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Expand Up @@ -77,6 +77,7 @@ def gem_spec(opts = {})
s.authors = ["Joe User"]
s.description = %q{This is my awesome gem.}
s.email = %q{joe@user.com}
s.licenses = %w(MIT BSD)
s.files = [
"README.textile",
"Rakefile",
Expand Down
16 changes: 9 additions & 7 deletions test/unit/version_test.rb
Expand Up @@ -11,7 +11,7 @@ class VersionTest < ActiveSupport::TestCase

should "only have relevant API fields" do
json = @version.as_json
assert_equal %w[number built_at summary description authors platform prerelease downloads_count].map(&:to_s).sort, json.keys.sort
assert_equal %w[number built_at summary description authors platform prerelease downloads_count licenses].map(&:to_s).sort, json.keys.sort
assert_equal @version.authors, json["authors"]
assert_equal @version.built_at, json["built_at"]
assert_equal @version.description, json["description"]
Expand All @@ -20,6 +20,7 @@ class VersionTest < ActiveSupport::TestCase
assert_equal @version.platform, json["platform"]
assert_equal @version.prerelease, json["prerelease"]
assert_equal @version.summary, json["summary"]
assert_equal @version.licenses, json["licenses"]
end
end

Expand All @@ -30,7 +31,7 @@ class VersionTest < ActiveSupport::TestCase

should "only have relevant API fields" do
xml = Nokogiri.parse(@version.to_xml)
assert_equal %w[number built-at summary description authors platform prerelease downloads-count].map(&:to_s).sort, xml.root.children.map{|a| a.name}.reject{|t| t == "text"}.sort
assert_equal %w[number built-at summary description authors platform prerelease downloads-count licenses].map(&:to_s).sort, xml.root.children.map{|a| a.name}.reject{|t| t == "text"}.sort
assert_equal @version.authors, xml.at_css("authors").content
assert_equal @version.built_at.to_i, xml.at_css("built-at").content.to_time.to_i
assert_equal @version.description, xml.at_css("description").content
Expand All @@ -39,6 +40,7 @@ class VersionTest < ActiveSupport::TestCase
assert_equal @version.platform, xml.at_css("platform").content
assert_equal @version.prerelease.to_s, xml.at_css("prerelease").content
assert_equal @version.summary.to_s, xml.at_css("summary").content
assert_equal @version.licenses, xml.at_css("licenses").content
end
end

Expand Down Expand Up @@ -459,7 +461,7 @@ class VersionTest < ActiveSupport::TestCase

context "with a Gem::Specification" do
setup do
@spec = gem_specification_from_gem_fixture('test-0.0.0')
@spec = gem_spec
@version = build(:version)
end

Expand All @@ -476,10 +478,10 @@ class VersionTest < ActiveSupport::TestCase
@version.update_attributes_from_gem_specification!(@spec)

assert @version.indexed
assert_equal @spec.authors.join(', '), @version.authors
assert_equal @spec.description, @version.description
assert_equal @spec.summary, @version.summary
assert_equal @spec.date, @version.built_at
assert_equal @spec.authors.join(', '), @version.authors
assert_equal @spec.description, @version.description
assert_equal @spec.summary, @version.summary
assert_equal @spec.date, @version.built_at
end
end

Expand Down

0 comments on commit b49db33

Please sign in to comment.