Commit 87760a29 authored by Jeroen van Baarsen's avatar Jeroen van Baarsen

Added an UrlBuilder for building rails named routes

Signed-off-by: default avatarJeroen van Baarsen <jeroenvanbaarsen@gmail.com>
parent 45e1941f
module Issues module Issues
class BaseService < ::BaseService class BaseService < ::BaseService
include Rails.application.routes.url_helpers
private private
...@@ -10,9 +9,7 @@ module Issues ...@@ -10,9 +9,7 @@ module Issues
def execute_hooks(issue, action = 'open') def execute_hooks(issue, action = 'open')
issue_data = issue.to_hook_data issue_data = issue.to_hook_data
issue_url = project_issue_url(id: issue.iid, issue_url = Gitlab::UrlBuilder.new(:issue).build(issue.id)
project_id: issue.project,
host: Settings.gitlab['url'])
issue_data[:object_attributes].merge!(url: issue_url, action: action) issue_data[:object_attributes].merge!(url: issue_url, action: action)
issue.project.execute_hooks(issue_data, :issue_hooks) issue.project.execute_hooks(issue_data, :issue_hooks)
end end
......
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
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment