Skip to content

Commit

Permalink
Improve performance by stopping loading everything in current_user
Browse files Browse the repository at this point in the history
Each controller must be in charge of managing its own payload.
  • Loading branch information
x4d3 committed Nov 9, 2017
1 parent e0a5926 commit 6576408
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController
extend ActiveSupport::Concern

INCLUDED_DEPENDENCIES = %i(deletion_requests organizations orga_relations dashboards teams orga_relations.user orga_relations.organization sub_tenant)

#==================================================================
# Included methods
#==================================================================
Expand All @@ -17,55 +19,52 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController
#==================================================================
# GET /mnoe/jpi/v1/current_user
def show
@user = current_user || MnoEnterprise::User.new(id: nil)
@user = current_user&.load_required(*INCLUDED_DEPENDENCIES) || MnoEnterprise::User.new(id: nil)
end

# PUT /mnoe/jpi/v1/current_user
def update
@user = current_user
@user.attributes = user_params
current_user.attributes = user_params
changed_attributes = @user.changed_attributes
@user.save!
current_user.save!
current_user.refresh_user_cache
MnoEnterprise::EventLogger.info('user_update', current_user.id, 'User update', @user, changed_attributes)
@user = @user.load_required_dependencies
@user = current_user.load_required(*INCLUDED_DEPENDENCIES)
render :show
current_user.refresh_user_cache
end

# PUT /mnoe/jpi/v1/current_user/register_developer
def register_developer
@user = current_user
@user = @user.create_api_credentials!
current_user.create_api_credentials!
MnoEnterprise::EventLogger.info('register_developer', current_user.id, 'Developer registration', @user)
@user = @user.load_required_dependencies
@user = current_user.load_required(*INCLUDED_DEPENDENCIES)
render :show

end

# PUT /mnoe/jpi/v1/current_user/update_password
def update_password
@user = current_user
@user = @user.update_password!(data: {attributes: password_params})
current_user.update_password!(data: { attributes: password_params })
MnoEnterprise::EventLogger.info('user_update_password', current_user.id, 'User password change', @user)
@user = @user.load_required_dependencies
@user = current_user.load_required(*INCLUDED_DEPENDENCIES)
sign_in @user, bypass: true
render :show
end

private

def user_params
params.require(:user).permit(
:name, :surname, :email, :company, :phone, :website, :phone_country_code, :current_password, :password, :password_confirmation,
settings: [:locale]
)
end
def user_params
params.require(:user).permit(
:name, :surname, :email, :company, :phone, :website, :phone_country_code, :current_password, :password, :password_confirmation,
settings: [:locale]
)
end

def password_params
params.require(:user).permit(:current_password, :password, :password_confirmation)
end
def password_params
params.require(:user).permit(:current_password, :password, :password_confirmation)
end

def user_management_enabled?
return head :forbidden unless Settings.dashboard.user_management.enabled
end
def user_management_enabled?
return head :forbidden unless Settings.dashboard.user_management.enabled
end
end
5 changes: 5 additions & 0 deletions core/app/models/mno_enterprise/base_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ def relations
@relations ||= ActiveSupport::HashWithIndifferentAccess.new
end

# define if the relationship has been loaded (for example by calling include)
def relation_loaded?(relation)
relationships && relationships.has_attribute?(relation) && relationships[relation].key?('data')
end

def process_custom_result(result)
collect_errors(result.errors)
raise_if_errors
Expand Down
8 changes: 5 additions & 3 deletions core/app/models/mno_enterprise/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class User < BaseResource
include ActiveModel::AttributeMethods
include ActiveModel::Validations

INCLUDED_DEPENDENCIES = %i(deletion_requests organizations orga_relations dashboards teams sub_tenant)

# ids
property :id
property :uid, type: :string
Expand Down Expand Up @@ -125,7 +123,11 @@ def role_from_id(organization_id)
end

def orga_relation_from_id(organization_id)
MnoEnterprise::OrgaRelation.where('user.id': id, 'organization.id': organization_id).first
if relation_loaded?(:orga_relations)
self.orga_relations.find { |r| r.organization.id == organization_id }
else
MnoEnterprise::OrgaRelation.where('user.id': id, 'organization.id': organization_id).first
end
end

def create_deletion_request!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def column_names

# @see OrmAdapter::Base#get!
def get!(id)
res = klass.includes(*klass::INCLUDED_DEPENDENCIES).find(wrap_key(id)).first
res = klass.find(wrap_key(id)).first
raise JsonApiClient::Errors::ResourceNotFound, "resource not found" unless res
res
end

# @see OrmAdapter::Base#get
def get(id)
res = klass.includes(*klass::INCLUDED_DEPENDENCIES).find(wrap_key(id))
res = klass.find(wrap_key(id))
error = res&.errors&.first
if (error && error.code != '404')
raise error.detail
Expand Down

0 comments on commit 6576408

Please sign in to comment.