From e294b3888c9d6a2f1adb40f75fa4dcbd37f6c0b8 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Tue, 26 Jan 2016 15:35:47 +0100 Subject: [PATCH] Refactor badge_config logic, fix static badges --- app/controllers/badges.rb | 21 ++++++++++++++++----- app/helpers/badges_helper.rb | 8 ++++---- app/views/badges/render.haml | 2 +- app/views/badges/static.haml | 27 ++++++++++++++------------- app/views/static/index.haml | 8 ++++---- models/badge.rb | 5 ++++- 6 files changed, 43 insertions(+), 28 deletions(-) diff --git a/app/controllers/badges.rb b/app/controllers/badges.rb index da4eef6..1f1c847 100644 --- a/app/controllers/badges.rb +++ b/app/controllers/badges.rb @@ -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 @@ -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 @@ -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 diff --git a/app/helpers/badges_helper.rb b/app/helpers/badges_helper.rb index 0896015..77c5550 100644 --- a/app/helpers/badges_helper.rb +++ b/app/helpers/badges_helper.rb @@ -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 @@ -24,7 +24,7 @@ def badge_config badge versionWidth: versionWidth, textPosition: textPosition, labelPosition: labelPosition, - badge: badge + params: params } end diff --git a/app/views/badges/render.haml b/app/views/badges/render.haml index cb481bb..d3bb701 100644 --- a/app/views/badges/render.haml +++ b/app/views/badges/render.haml @@ -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"} diff --git a/app/views/badges/static.haml b/app/views/badges/static.haml index 3d28a61..2d40942 100644 --- a/app/views/badges/static.haml +++ b/app/views/badges/static.haml @@ -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 diff --git a/app/views/static/index.haml b/app/views/static/index.haml index 9c35411..c20f7fa 100644 --- a/app/views/static/index.haml +++ b/app/views/static/index.haml @@ -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: diff --git a/models/badge.rb b/models/badge.rb index b0fd8d6..f922875 100644 --- a/models/badge.rb +++ b/models/badge.rb @@ -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 @@ -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