diff options
author | James Lopez <james@jameslopez.es> | 2017-10-24 09:38:06 +0300 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2017-10-31 09:03:59 +0100 |
commit | 72b7b10d52de1b150b124c017427b2802b4193e6 (patch) | |
tree | 8f38106c35e4379cc466caca7b40411749fc829c | |
parent | bba020a56cbedd6403b637ec7f9eaee258eb7c67 (diff) | |
download | gitlab-ce-72b7b10d52de1b150b124c017427b2802b4193e6.tar.gz |
add applications controller logic
-rw-r--r-- | app/controllers/admin/applications_controller.rb | 15 | ||||
-rw-r--r-- | app/controllers/oauth/applications_controller.rb | 22 | ||||
-rw-r--r-- | app/services/applications/create_searvice.rb | 15 | ||||
-rw-r--r-- | spec/services/applications/create_service_spec.rb | 12 |
4 files changed, 42 insertions, 22 deletions
diff --git a/app/controllers/admin/applications_controller.rb b/app/controllers/admin/applications_controller.rb index fb6d8c0bb81..69ebe7db08b 100644 --- a/app/controllers/admin/applications_controller.rb +++ b/app/controllers/admin/applications_controller.rb @@ -19,10 +19,12 @@ class Admin::ApplicationsController < Admin::ApplicationController end def create - @application = Doorkeeper::Application.new(application_params) + @application = Applications::CreateService.new(current_user, application_params.merge(ip_address: request.remote_ip)).execute - if @application.save - redirect_to_admin_page + if @application.persisted? + flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create]) + + redirect_to admin_application_url(@application) else render :new end @@ -41,13 +43,6 @@ class Admin::ApplicationsController < Admin::ApplicationController redirect_to admin_applications_url, status: 302, notice: 'Application was successfully destroyed.' end - protected - - def redirect_to_admin_page - flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create]) - redirect_to admin_application_url(@application) - end - private def set_application diff --git a/app/controllers/oauth/applications_controller.rb b/app/controllers/oauth/applications_controller.rb index b02e64a132b..daba161a177 100644 --- a/app/controllers/oauth/applications_controller.rb +++ b/app/controllers/oauth/applications_controller.rb @@ -16,25 +16,16 @@ class Oauth::ApplicationsController < Doorkeeper::ApplicationsController end def create - @application = Doorkeeper::Application.new(application_params) + @application = Applications::CreateService.new(current_user, create_application_params).execute - @application.owner = current_user - - if @application.save - redirect_to_oauth_application_page + if @application.persisted? + flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create]) else set_index_vars render :index end end - protected - - def redirect_to_oauth_application_page - flash[:notice] = I18n.t(:notice, scope: [:doorkeeper, :flash, :applications, :create]) - redirect_to oauth_application_url(@application) - end - private def verify_user_oauth_applications_enabled @@ -61,4 +52,11 @@ class Oauth::ApplicationsController < Doorkeeper::ApplicationsController rescue_from ActiveRecord::RecordNotFound do |exception| render "errors/not_found", layout: "errors", status: 404 end + + def create_application_params + application_params.tap do |params| + params[:owner] = current_user + params[:ip_address] = request.remote_ip + end + end end diff --git a/app/services/applications/create_searvice.rb b/app/services/applications/create_searvice.rb new file mode 100644 index 00000000000..66092261a11 --- /dev/null +++ b/app/services/applications/create_searvice.rb @@ -0,0 +1,15 @@ +module Applications + class CreateService + prepend ::EE::Applications::CreateService + + def initialize(current_user, params) + @current_user = current_user + @params = params + @ip_address = @params.delete(:ip_address) + end + + def execute + Doorkeeper::Application.create(@params) + end + end +end diff --git a/spec/services/applications/create_service_spec.rb b/spec/services/applications/create_service_spec.rb new file mode 100644 index 00000000000..0bd96a605c0 --- /dev/null +++ b/spec/services/applications/create_service_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +describe ::Applications::CreateService do + let(:user) { create(:user) } + let(:params) { attributes_for(:application) } + + subject { described_class.new(user, params) } + + it 'creates an application' do + expect { subject.execute }.to change { Doorkeeper::Application.count }.by(1) + end +end |