From 87760a290eddbc35f4e89b129da6ec842a70f609 Mon Sep 17 00:00:00 2001 From: Jeroen van Baarsen Date: Fri, 13 Jun 2014 12:34:15 +0200 Subject: [PATCH] Added an UrlBuilder for building rails named routes Signed-off-by: Jeroen van Baarsen --- app/services/issues/base_service.rb | 5 +---- lib/gitlab/url_builder.rb | 25 +++++++++++++++++++++++++ spec/lib/gitlab/url_builder_spec.rb | 11 +++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 lib/gitlab/url_builder.rb create mode 100644 spec/lib/gitlab/url_builder_spec.rb diff --git a/app/services/issues/base_service.rb b/app/services/issues/base_service.rb index 41fbe1ae08f..71b9ffc3489 100644 --- a/app/services/issues/base_service.rb +++ b/app/services/issues/base_service.rb @@ -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 diff --git a/lib/gitlab/url_builder.rb b/lib/gitlab/url_builder.rb new file mode 100644 index 00000000000..de7e0404086 --- /dev/null +++ b/lib/gitlab/url_builder.rb @@ -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 diff --git a/spec/lib/gitlab/url_builder_spec.rb b/spec/lib/gitlab/url_builder_spec.rb new file mode 100644 index 00000000000..1bc77e3d424 --- /dev/null +++ b/spec/lib/gitlab/url_builder_spec.rb @@ -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