Commit 6b4ddf2c authored by Marin Jankovski's avatar Marin Jankovski

Add admin services templates.

parent 09d3d351
class Admin::ServicesController < Admin::ApplicationController
before_filter :service, only: [:edit, :update]
def index
@services = services_templates
end
def edit
unless service.present?
redirect_to admin_application_settings_services_path,
alert: "Service is unknown or it doesn't exist"
end
end
def update
if service.update_attributes(application_services_params[:service])
redirect_to admin_application_settings_services_path,
notice: 'Application settings saved successfully'
else
render :edit
end
end
private
def services_templates
templates = []
allowed_templates.each do |service|
service_template = service.constantize
templates << service_template.where(template: true).first_or_create
end
templates
end
def allowed_templates
%w( JiraService RedmineService CustomIssueTrackerService )
end
def service
@service ||= Service.where(id: params[:id], template: true).first
end
def application_services_params
params.permit(:id,
service: [
:title, :project_url, :description, :issues_url, :new_issue_url
])
end
end
......@@ -353,14 +353,29 @@ class Project < ActiveRecord::Base
end
def build_missing_services
services_templates = Service.where(template: true)
available_services_names.each do |service_name|
service = services.find { |service| service.to_param == service_name }
service = find_service(services, service_name)
# If service is available but missing in db
# we should create an instance. Ex `create_gitlab_ci_service`
service = self.send :"create_#{service_name}_service" if service.nil?
if service.nil?
# We should check if template for the service exists
template = find_service(services_templates, service_name)
if template.nil?
# If no template, we should create an instance. Ex `create_gitlab_ci_service`
service = self.send :"create_#{service_name}_service"
else
Service.create_from_template(self.id, template)
end
end
end
end
def find_service(list, name)
list.find { |service| service.to_param == name }
end
def available_services_names
%w(gitlab_ci campfire hipchat pivotaltracker flowdock assembla asana
......
......@@ -77,12 +77,14 @@ class IssueTrackerService < Service
end
def set_project_url
if self.project
id = self.project.issues_tracker_id
if id
issues_tracker['project_url'].gsub(":issues_tracker_id", id)
else
issues_tracker['project_url']
end
end
issues_tracker['project_url']
end
end
......@@ -25,7 +25,7 @@ class Service < ActiveRecord::Base
belongs_to :project
has_one :service_hook
validates :project_id, presence: true
validates :project_id, presence: true, unless: Proc.new { |service| service.template? }
scope :visible, -> { where.not(type: 'GitlabIssueTrackerService') }
......@@ -33,6 +33,10 @@ class Service < ActiveRecord::Base
active
end
def template?
template
end
def category
:common
end
......@@ -94,7 +98,10 @@ class Service < ActiveRecord::Base
self.category == :issue_tracker
end
def self.issue_tracker_service_list
Service.select(&:issue_tracker?).map{ |s| s.to_param }
def self.create_from_template(project_id, template)
service = template.dup
service.template = false
service.project_id = project_id
service if service.save
end
end
- service.fields.each do |field|
TOPD
/ - name = field[:name]
/ - value = "V"#@service.send(name) unless field[:type] == 'password'
/ - type = field[:type]
/ - placeholder = field[:placeholder]
/ - choices = field[:choices]
/ - default_choice = field[:default_choice]
/ .form-group
/ = f.label name, class: "control-label"
/ .col-sm-10
/ - if type == 'text'
/ = f.text_field name, class: "form-control", placeholder: placeholder
/ - elsif type == 'textarea'
/ = f.text_area name, rows: 5, class: "form-control", placeholder: placeholder
/ - elsif type == 'checkbox'
/ = f.check_box name
/ - elsif type == 'select'
/ = f.select name, options_for_select(choices, value ? value : default_choice), {}, { class: "form-control" }
/ - elsif type == 'password'
/ = f.password_field name, class: 'form-control'
%h3.page-title
= @service.title
= boolean_to_icon @service.activated?
%p #{@service.description} template
= form_for :service, url: admin_application_settings_service_path, method: :put, html: { class: 'form-horizontal fieldset-form' } do |f|
- if @service.errors.any?
#error_explanation
.alert.alert-danger
- @service.errors.full_messages.each do |msg|
%p= msg
- @service.fields.each do |field|
- name = field[:name]
- type = field[:type]
- placeholder = field[:placeholder]
.form-group
= f.label name, class: "control-label"
.col-sm-10
= f.text_field name, class: "form-control", placeholder: placeholder
.form-actions
= f.submit 'Save', class: 'btn btn-save'
%h3.page-title Service templates
%p.light Service template allows you to set default values for project services
%table.table
%thead
%tr
%th
%th Service
%th Desription
%th Last edit
- @services.sort_by(&:title).each do |service|
%tr
%td
= icon("copy", class: 'clgray')
%td
= link_to edit_admin_application_settings_service_path(service.id) do
%strong= service.title
%td
= service.description
%td.light
= time_ago_in_words service.updated_at
ago
......@@ -131,7 +131,9 @@ Gitlab::Application.routes.draw do
end
end
resource :application_settings, only: [:show, :update]
resource :application_settings, only: [:show, :update] do
resources :services
end
root to: 'dashboard#index'
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