Commit 72b7b10d authored by James Lopez's avatar James Lopez

add applications controller logic

parent bba020a5
......@@ -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
......
......@@ -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
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
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
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