Added an UrlBuilder for building rails named routes

Signed-off-by: Jeroen van Baarsen <jeroenvanbaarsen@gmail.com>
This commit is contained in:
Jeroen van Baarsen 2014-06-13 12:34:15 +02:00
parent 45e1941fb3
commit 87760a290e
3 changed files with 37 additions and 4 deletions

View file

@ -1,6 +1,5 @@
module Issues
class BaseService < ::BaseService
include Rails.application.routes.url_helpers
private
@ -10,9 +9,7 @@ module Issues
def execute_hooks(issue, action = 'open')
issue_data = issue.to_hook_data
issue_url = project_issue_url(id: issue.iid,
project_id: issue.project,
host: Settings.gitlab['url'])
issue_url = Gitlab::UrlBuilder.new(:issue).build(issue.id)
issue_data[:object_attributes].merge!(url: issue_url, action: action)
issue.project.execute_hooks(issue_data, :issue_hooks)
end

25
lib/gitlab/url_builder.rb Normal file
View file

@ -0,0 +1,25 @@
module Gitlab
class UrlBuilder
include Rails.application.routes.url_helpers
def initialize(type)
@type = type
end
def build(id)
case @type
when :issue
issue_url(id)
end
end
private
def issue_url(id)
issue = Issue.find(id)
project_issue_url(id: issue.iid,
project_id: issue.project,
host: Settings.gitlab['url'])
end
end
end

View file

@ -0,0 +1,11 @@
require 'spec_helper'
describe Gitlab::UrlBuilder do
describe 'When asking for an issue' do
it 'returns the issue url' do
issue = create(:issue)
url = Gitlab::UrlBuilder.new(:issue).build(issue.id)
expect(url).to eq "#{Settings.gitlab['url']}/namespace1/gitlabhq/issues/#{issue.iid}"
end
end
end