Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
Refactor badge_config logic, fix static badges
Browse files Browse the repository at this point in the history
  • Loading branch information
halfdan committed Jan 26, 2016
1 parent 9e02211 commit e294b38
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
21 changes: 16 additions & 5 deletions app/controllers/badges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

get :show, with: :id do
@badge = Badge.find_by_id(params[:id])
render 'show'
if @badge
render 'show'
else
status 404
end
end

# Renders badge based on GET-Params
Expand All @@ -23,7 +27,7 @@
expires 30
@badge = Badge.find_by_id(params[:id])

options = badge_config(@badge)
options = badge_config(@badge.definition)

content_type 'image/svg+xml'
render 'render', layout: false, locals: options
Expand All @@ -48,14 +52,21 @@
if @badge.user == current_user
render 'edit'
else
status 401
flash[:error] = "You are not allowed to edit this badge"
end
end

get :new do
@badge = Badge.new
@badge.badge_infos.build
render :new
if is_logged_in?
@badge = Badge.new
@badge.badge_infos.build
render :new
else
status 401
flash[:error] = "You need to log in to create a badge."
redirect(url(:static, :index))
end
end

delete :delete, with: :id do
Expand Down
8 changes: 4 additions & 4 deletions app/helpers/badges_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def badge_options()

end

def badge_config badge
versionString = version_string(badge.definition)
labelText = badge.label || 'ember-versions'
def badge_config params
versionString = version_string(params)
labelText = params[:label] || 'ember-versions'
separator = (labelText.length + 4) * 6
width = separator + (versionString.length + 4) * 6
versionWidth = width - separator
Expand All @@ -24,7 +24,7 @@ def badge_config badge
versionWidth: versionWidth,
textPosition: textPosition,
labelPosition: labelPosition,
badge: badge
params: params
}
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/badges/render.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
!!! XML
%svg{height: "20", width: width, xmlns: "http://www.w3.org/2000/svg", "xmlns:xlink" => "http://www.w3.org/1999/xlink"}
%a{"xlink:href"=> url(:badges, :show, id: badge.id), target: '_blank'}
%a{"xlink:href"=> url(:badges, :show, id: params[:id]), target: '_blank'}
%lineargradient#a{x2: "0", y2: "100%"}
%stop{offset: "0", "stop-color" => "#bbb", "stop-opacity" => ".1"}
%stop{offset: "1", "stop-opacity" => ".1"}
Expand Down
27 changes: 14 additions & 13 deletions app/views/badges/static.haml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
!!! XML
%svg{height: "20", width: width, xmlns: "http://www.w3.org/2000/svg"}
%lineargradient#a{x2: "0", y2: "100%"}
%stop{offset: "0", "stop-color" => "#bbb", "stop-opacity" => ".1"}
%stop{offset: "1", "stop-opacity" => ".1"}
%rect{fill: "#555", height: "20", rx: "3", width: width}
%rect{fill: "#fe845a", height: "20", rx: "3", width: versionWidth, x: separator}
%path{d: "M#{separator} 0h4v20h-4z", fill: "#fe845a"}
%rect{fill: "url(#a)", height: "20", rx: "3", width: width}
%g{fill: "#fff", "font-family" => "DejaVu Sans,Verdana,Geneva,sans-serif", "font-size" => "11", "text-anchor" => "middle"}
%text{fill: "#010101", "fill-opacity" => ".3", x: labelPosition, y: "15"}= labelText
%text{x: labelPosition, y: "14"}= labelText
%text{fill: "#010101", "fill-opacity" => ".3", x: textPosition, y: "15"}= versionString
%text{x: textPosition, y: "14"}= versionString
%svg{height: "20", width: width, xmlns: "http://www.w3.org/2000/svg", "xmlns:xlink" => "http://www.w3.org/1999/xlink"}
%a{"xlink:href"=> url(:static, :index), target: '_blank'}
%lineargradient#a{x2: "0", y2: "100%"}
%stop{offset: "0", "stop-color" => "#bbb", "stop-opacity" => ".1"}
%stop{offset: "1", "stop-opacity" => ".1"}
%rect{fill: "#555", height: "20", rx: "3", width: width}
%rect{fill: "#fe845a", height: "20", rx: "3", width: versionWidth, x: separator}
%path{d: "M#{separator} 0h4v20h-4z", fill: "#fe845a"}
%rect{fill: "url(#a)", height: "20", rx: "3", width: width}
%g{fill: "#fff", "font-family" => "DejaVu Sans,Verdana,Geneva,sans-serif", "font-size" => "11", "text-anchor" => "middle"}
%text{fill: "#010101", "fill-opacity" => ".3", x: labelPosition, y: "15"}= labelText
%text{x: labelPosition, y: "14"}= labelText
%text{fill: "#010101", "fill-opacity" => ".3", x: textPosition, y: "15"}= versionString
%text{x: textPosition, y: "14"}= versionString
8 changes: 4 additions & 4 deletions app/views/static/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
%img{:src => "/v1/badge.svg?start=1.13.0&end=2.0.0"}/
%dt Range
%dd
%a{:href => "/v1/badge.svg?range=^2.3.0"}
%img{:src => "/v1/badge.svg?range=^2.3.0"}/
%a{:href => "/v1/badge.svg?range=%5E2.3.0"}
%img{:src => "/v1/badge.svg?range=%5E2.3.0"}/
%dt Custom label
%dd
%a{:href => "/v1/badge.svg?label=ember-cli&start=1.13.13"}
%img{:src => "/v1/badge.svg?label=ember-cli&start=1.13.13"}/
%dt Custom label & range
%dd
%a{:href => "/v1/badge.svg?label=ember-data&range=^2.3.0"}
%img{:src => "/v1/badge.svg?label=ember-data&range=^2.3.0"}/
%a{:href => "/v1/badge.svg?label=ember-data&range=%5E2.3.0"}
%img{:src => "/v1/badge.svg?label=ember-data&range=%5E2.3.0"}/
%h3#browser Browser
%p In order to easily add embadge.io to your website you can include the following script in the body:
Expand Down
5 changes: 4 additions & 1 deletion models/badge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Badge < ActiveRecord::Base
# We want a title and url if the badge is to be publicly listed.
validates_presence_of :url
validates_presence_of :title
validates_presence_of :label

accepts_nested_attributes_for :badge_infos

Expand All @@ -14,7 +15,9 @@ def definition
{
start: current.version_start.blank? ? nil : current.version_start,
end: current.version_end.blank? ? nil : current.version_end,
range: current.version_range.blank? ? nil : current.version_range
range: current.version_range.blank? ? nil : current.version_range,
label: self.label,
id: self.id
}
end
end

0 comments on commit e294b38

Please sign in to comment.