services_controller.rb 1.21 KB
Newer Older
1
class Projects::ServicesController < Projects::ApplicationController
2
  include ServiceParams
3

4
  # Authorize
5 6
  before_action :authorize_admin_project!
  before_action :service, only: [:edit, :update, :test]
7 8 9

  respond_to :html

10 11
  layout "project_settings"

12 13 14 15
  def edit
  end

  def update
16 17
    @service.attributes = service_params[:service]

18
    if @service.save(context: :manual_change)
19
      redirect_to(project_settings_integrations_path(@project), notice: success_message)
20 21 22 23 24 25
    else
      render 'edit'
    end
  end

  def test
26
    message = {}
27

28
    if @service.can_test? && @service.update_attributes(service_params[:service])
29 30
      data = @service.test_data(project, current_user)
      outcome = @service.test(data)
31

32 33 34 35 36 37 38
      unless outcome[:success]
        message = { error: true, message: 'Test failed.', service_response: outcome[:result].to_s }
      end

      status = :ok
    else
      status = :not_found
39
    end
40

41
    render json: message, status: status
42
  end
43 44 45

  private

46 47 48 49 50 51 52 53
  def success_message
    if @service.active?
      "#{@service.title} activated."
    else
      "#{@service.title} settings saved, but not activated."
    end
  end

54
  def service
55
    @service ||= @project.find_or_initialize_service(params[:id])
56
  end
57
end