Skip to content
Snippets Groups Projects
Commit 9ac0c76b authored by Lin Jen-Shin's avatar Lin Jen-Shin
Browse files

Use StrongMemoize and enable/disable cops properly

parent 0af35d7e
No related branches found
No related tags found
No related merge requests found
Showing
with 85 additions and 69 deletions
module CreatesCommit
extend ActiveSupport::Concern
include Gitlab::Utils::StrongMemoize
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def create_commit(service, success_path:, failure_path:, failure_view: nil, success_notice: nil)
Loading
Loading
@@ -94,15 +95,20 @@ module CreatesCommit
# rubocop:enable Cop/ModuleWithInstanceVariables
 
def existing_merge_request_path
project_merge_request_path(@project, @merge_request)
project_merge_request_path(@project, @merge_request) # rubocop:disable Cop/ModuleWithInstanceVariables
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def merge_request_exists?
return @merge_request if defined?(@merge_request)
@merge_request = MergeRequestsFinder.new(current_user, project_id: @project.id).execute.opened
.find_by(source_project_id: @project_to_commit_into, source_branch: @branch_name, target_branch: @start_branch)
strong_memoize(:merge_request) do
MergeRequestsFinder.new(current_user, project_id: @project.id)
.execute
.opened
.find_by(
source_project_id: @project_to_commit_into,
source_branch: @branch_name,
target_branch: @start_branch)
end
end
# rubocop:enable Cop/ModuleWithInstanceVariables
 
Loading
Loading
module GroupTree
# rubocop:disable Cop/ModuleWithInstanceVariables
def render_group_tree(groups)
@groups = if params[:filter].present?
Gitlab::GroupHierarchy.new(groups.search(params[:filter]))
Loading
Loading
@@ -20,5 +21,6 @@ module GroupTree
render json: serializer.represent(@groups)
end
end
# rubocop:enable Cop/ModuleWithInstanceVariables
end
end
Loading
Loading
@@ -142,6 +142,7 @@ module IssuableActions
@resource_name ||= controller_name.singularize
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def render_entity_json
if @issuable.valid?
render json: serializer.represent(@issuable)
Loading
Loading
@@ -149,6 +150,7 @@ module IssuableActions
render json: { errors: @issuable.errors.full_messages }, status: :unprocessable_entity
end
end
# rubocop:enable Cop/ModuleWithInstanceVariables
 
def serializer
raise NotImplementedError
Loading
Loading
@@ -159,6 +161,6 @@ module IssuableActions
end
 
def parent
@project || @group
@project || @group # rubocop:disable Cop/ModuleWithInstanceVariables
end
end
Loading
Loading
@@ -2,6 +2,7 @@ module IssuableCollections
extend ActiveSupport::Concern
include SortingHelper
include Gitlab::IssuableMetadata
include Gitlab::Utils::StrongMemoize
 
included do
helper_method :finder
Loading
Loading
@@ -43,7 +44,7 @@ module IssuableCollections
def redirect_out_of_range(total_pages)
return false if total_pages.zero?
 
out_of_range = @issuables.current_page > total_pages
out_of_range = @issuables.current_page > total_pages # rubocop:disable Cop/ModuleWithInstanceVariables
 
if out_of_range
redirect_to(url_for(params.merge(page: total_pages, only_path: true)))
Loading
Loading
@@ -53,7 +54,7 @@ module IssuableCollections
end
 
def issuable_page_count
page_count_for_relation(@issuables, finder.row_count)
page_count_for_relation(@issuables, finder.row_count) # rubocop:disable Cop/ModuleWithInstanceVariables
end
 
def page_count_for_relation(relation, row_count)
Loading
Loading
@@ -133,9 +134,9 @@ module IssuableCollections
end
 
def finder
return @finder if defined?(@finder)
@finder = issuable_finder_for(@finder_type)
strong_memoize(:finder) do
issuable_finder_for(@finder_type) # rubocop:disable Cop/ModuleWithInstanceVariables
end
end
 
def collection_type
Loading
Loading
# rubocop:disable Cop/ModuleWithInstanceVariables
module NotesActions
include RendersNotes
include Gitlab::Utils::StrongMemoize
extend ActiveSupport::Concern
 
included do
Loading
Loading
@@ -31,6 +31,7 @@ module NotesActions
render json: notes_json
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def create
create_params = note_params.merge(
merge_request_diff_head_sha: params[:merge_request_diff_head_sha],
Loading
Loading
@@ -48,7 +49,9 @@ module NotesActions
format.html { redirect_back_or_default }
end
end
# rubocop:enable Cop/ModuleWithInstanceVariables
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def update
@note = Notes::UpdateService.new(project, current_user, note_params).execute(note)
 
Loading
Loading
@@ -61,6 +64,7 @@ module NotesActions
format.html { redirect_back_or_default }
end
end
# rubocop:enable Cop/ModuleWithInstanceVariables
 
def destroy
if note.editable?
Loading
Loading
@@ -139,7 +143,7 @@ module NotesActions
end
else
template = "discussions/_diff_discussion"
@fresh_discussion = true
@fresh_discussion = true # rubocop:disable Cop/ModuleWithInstanceVariables
 
locals = { discussions: [discussion], on_image: on_image }
end
Loading
Loading
@@ -192,7 +196,7 @@ module NotesActions
end
 
def noteable
@noteable ||= notes_finder.target || @note&.noteable
@noteable ||= notes_finder.target || @note&.noteable # rubocop:disable Cop/ModuleWithInstanceVariables
end
 
def require_noteable!
Loading
Loading
@@ -212,20 +216,21 @@ module NotesActions
end
 
def note_project
return @note_project if defined?(@note_project)
return nil unless project
strong_memoize(:note_project) do
return nil unless project
 
note_project_id = params[:note_project_id]
note_project_id = params[:note_project_id]
 
@note_project =
if note_project_id.present?
Project.find(note_project_id)
else
project
end
the_project =
if note_project_id.present?
Project.find(note_project_id)
else
project
end
 
return access_denied! unless can?(current_user, :create_note, @note_project)
return access_denied! unless can?(current_user, :create_note, the_project)
 
@note_project
the_project
end
end
end
module PreviewMarkdown
extend ActiveSupport::Concern
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def preview_markdown
result = PreviewMarkdownService.new(@project, current_user, params).execute
 
Loading
Loading
@@ -19,4 +20,5 @@ module PreviewMarkdown
}
}
end
# rubocop:enable Cop/ModuleWithInstanceVariables
end
module RendersCommits
# rubocop:disable Cop/ModuleWithInstanceVariables
def prepare_commits_for_rendering(commits)
Banzai::CommitRenderer.render(commits, @project, current_user)
Banzai::CommitRenderer.render(commits, @project, current_user) # rubocop:disable Cop/ModuleWithInstanceVariables
 
commits
end
Loading
Loading
Loading
Loading
@@ -8,6 +8,7 @@ module RendersNotes
 
notes
end
# rubocop:enable Cop/ModuleWithInstanceVariables
 
private
 
Loading
Loading
Loading
Loading
@@ -65,9 +65,8 @@ module ServiceParams
# Parameters to ignore if no value is specified
FILTER_BLANK_PARAMS = [:password].freeze
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def service_params
dynamic_params = @service.event_channel_names + @service.event_names
dynamic_params = @service.event_channel_names + @service.event_names # rubocop:disable Cop/ModuleWithInstanceVariables
service_params = params.permit(:id, service: ALLOWED_PARAMS_CE + dynamic_params)
 
if service_params[:service].is_a?(Hash)
Loading
Loading
Loading
Loading
@@ -15,6 +15,7 @@ module SnippetsActions
filename: @snippet.sanitized_file_name
)
end
# rubocop:enable Cop/ModuleWithInstanceVariables
 
private
 
Loading
Loading
Loading
Loading
@@ -2,6 +2,7 @@ module SpammableActions
extend ActiveSupport::Concern
 
include Recaptcha::Verify
include Gitlab::Utils::StrongMemoize
 
included do
before_action :authorize_submit_spammable!, only: :mark_as_spam
Loading
Loading
@@ -17,11 +18,10 @@ module SpammableActions
 
private
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def ensure_spam_config_loaded!
return @spam_config_loaded if defined?(@spam_config_loaded)
@spam_config_loaded = Gitlab::Recaptcha.load_configurations!
strong_memoize(:spam_config_loaded) do
Gitlab::Recaptcha.load_configurations!
end
end
 
def recaptcha_check_with_fallback(&fallback)
Loading
Loading
Loading
Loading
@@ -11,9 +11,8 @@ module ToggleSubscriptionAction
 
private
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def subscribable_project
@project || raise(NotImplementedError)
@project ||= raise(NotImplementedError)
end
 
def subscribable_resource
Loading
Loading
Loading
Loading
@@ -43,15 +43,12 @@ module Mentionable
self
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def all_references(current_user = nil, extractor: nil)
@extractors ||= {}
# Use custom extractor if it's passed in the function parameters.
if extractor
@extractors[current_user] = extractor
extractors[current_user] = extractor
else
extractor = @extractors[current_user] ||= Gitlab::ReferenceExtractor.new(project, current_user)
extractor = extractors[current_user] ||= Gitlab::ReferenceExtractor.new(project, current_user)
 
extractor.reset_memoized_values
end
Loading
Loading
@@ -70,6 +67,10 @@ module Mentionable
extractor
end
 
def extractors
@extractors ||= {}
end
def mentioned_users(current_user = nil)
all_references(current_user).users
end
Loading
Loading
Loading
Loading
@@ -102,11 +102,12 @@ module Milestoneish
end
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def memoize_per_user(user, method_name)
@memoized ||= {}
@memoized[method_name] ||= {}
@memoized[method_name][user&.id] ||= yield
memoized_users[method_name][user&.id] ||= yield
end
def memoized_users
@memoized_users ||= Hash.new { |h, k| h[k] = {} }
end
 
# override in a class that includes this module to get a faster query
Loading
Loading
Loading
Loading
@@ -55,6 +55,7 @@ module Noteable
discussion_notes.resolvable.discussions(self)
end
end
# rubocop:enable Cop/ModuleWithInstanceVariables
 
def discussions_resolvable?
resolvable_discussions.any?(&:resolvable?)
Loading
Loading
Loading
Loading
@@ -44,7 +44,6 @@ module RelativePositioning
next_pos
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def move_between(before, after)
return move_after(before) unless after
return move_before(after) unless before
Loading
Loading
@@ -53,13 +52,12 @@ module RelativePositioning
# to its predecessor. This process will recursively move all the predecessors until we have a place
if (after.relative_position - before.relative_position) < 2
before.move_before
@positionable_neighbours = [before]
@positionable_neighbours = [before] # rubocop:disable Cop/ModuleWithInstanceVariables
end
 
self.relative_position = position_between(before.relative_position, after.relative_position)
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def move_after(before = self)
pos_before = before.relative_position
pos_after = before.next_relative_position
Loading
Loading
@@ -67,7 +65,7 @@ module RelativePositioning
if before.shift_after?
issue_to_move = self.class.in_projects(project_ids).find_by!(relative_position: pos_after)
issue_to_move.move_after
@positionable_neighbours = [issue_to_move]
@positionable_neighbours = [issue_to_move] # rubocop:disable Cop/ModuleWithInstanceVariables
 
pos_after = issue_to_move.relative_position
end
Loading
Loading
@@ -75,7 +73,6 @@ module RelativePositioning
self.relative_position = position_between(pos_before, pos_after)
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def move_before(after = self)
pos_after = after.relative_position
pos_before = after.prev_relative_position
Loading
Loading
@@ -83,7 +80,7 @@ module RelativePositioning
if after.shift_before?
issue_to_move = self.class.in_projects(project_ids).find_by!(relative_position: pos_before)
issue_to_move.move_before
@positionable_neighbours = [issue_to_move]
@positionable_neighbours = [issue_to_move] # rubocop:disable Cop/ModuleWithInstanceVariables
 
pos_before = issue_to_move.relative_position
end
Loading
Loading
@@ -144,4 +141,5 @@ module RelativePositioning
 
status
end
# rubocop:enable Cop/ModuleWithInstanceVariables
end
Loading
Loading
@@ -84,7 +84,6 @@ module ResolvableDiscussion
 
private
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def update
# Do not select `Note.resolvable`, so that system notes remain in the collection
notes_relation = Note.where(id: notes.map(&:id))
Loading
Loading
Loading
Loading
@@ -86,10 +86,9 @@ module Routable
end
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def full_name
if route && route.name.present?
@full_name ||= route.name
@full_name ||= route.name # rubocop:disable Cop/ModuleWithInstanceVariables
else
update_route if persisted?
 
Loading
Loading
@@ -113,7 +112,7 @@ module Routable
 
def expires_full_path_cache
RequestStore.delete(full_path_key) if RequestStore.active?
@full_path = nil
@full_path = nil # rubocop:disable Cop/ModuleWithInstanceVariables
end
 
def build_full_path
Loading
Loading
@@ -126,10 +125,9 @@ module Routable
 
private
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def uncached_full_path
if route && route.path.present?
@full_path ||= route.path
@full_path ||= route.path # rubocop:disable Cop/ModuleWithInstanceVariables
else
update_route if persisted?
 
Loading
Loading
@@ -164,12 +162,11 @@ module Routable
route.save
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def prepare_route
route || build_route(source: self)
route.path = build_full_path
route.name = build_full_name
@full_path = nil
@full_name = nil
@full_path = nil # rubocop:disable Cop/ModuleWithInstanceVariables
@full_name = nil # rubocop:disable Cop/ModuleWithInstanceVariables
end
end
Loading
Loading
@@ -36,11 +36,10 @@ module Taskable
end
 
# Called by `TaskList::Summary`
# rubocop:disable Cop/ModuleWithInstanceVariables
def task_list_items
return [] if description.blank?
 
@task_list_items ||= Taskable.get_tasks(description)
@task_list_items ||= Taskable.get_tasks(description) # rubocop:disable Cop/ModuleWithInstanceVariables
end
 
def tasks
Loading
Loading
Loading
Loading
@@ -36,6 +36,7 @@ module TimeTrackable
end
end
alias_method :spend_time=, :spend_time
# rubocop:enable Cop/ModuleWithInstanceVariables
 
def total_time_spent
timelogs.sum(:time_spent)
Loading
Loading
@@ -51,11 +52,11 @@ module TimeTrackable
 
private
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def reset_spent_time
timelogs.new(time_spent: total_time_spent * -1, user: @time_spent_user)
timelogs.new(time_spent: total_time_spent * -1, user: @time_spent_user) # rubocop:disable Cop/ModuleWithInstanceVariables
end
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def add_or_subtract_spent_time
timelogs.new(
time_spent: time_spent,
Loading
Loading
@@ -63,17 +64,19 @@ module TimeTrackable
spent_at: @spent_at
)
end
# rubocop:enable Cop/ModuleWithInstanceVariables
 
# rubocop:disable Cop/ModuleWithInstanceVariables
def check_negative_time_spent
return if time_spent.nil? || time_spent == :reset
 
# we need to cache the total time spent so multiple calls to #valid?
# doesn't give a false error
@original_total_time_spent ||= total_time_spent
if time_spent < 0 && (time_spent.abs > @original_total_time_spent)
if time_spent < 0 && (time_spent.abs > original_total_time_spent)
errors.add(:time_spent, 'Time to subtract exceeds the total time spent')
end
end
# we need to cache the total time spent so multiple calls to #valid?
# doesn't give a false error
def original_total_time_spent
@original_total_time_spent ||= total_time_spent
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment