diff options
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/README.md | 22 | ||||
-rw-r--r-- | app/finders/groups_finder.rb | 38 | ||||
-rw-r--r-- | app/finders/issuable_finder.rb | 160 | ||||
-rw-r--r-- | app/finders/issues_finder.rb | 22 | ||||
-rw-r--r-- | app/finders/merge_requests_finder.rb | 22 | ||||
-rw-r--r-- | app/finders/notes_finder.rb | 27 | ||||
-rw-r--r-- | app/finders/projects_finder.rb | 89 | ||||
-rw-r--r-- | app/finders/snippets_finder.rb | 63 | ||||
-rw-r--r-- | app/finders/trending_projects_finder.rb | 19 |
9 files changed, 0 insertions, 462 deletions
diff --git a/app/finders/README.md b/app/finders/README.md deleted file mode 100644 index 1f46518d230..00000000000 --- a/app/finders/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# Finders - -This type of classes responsible for collection items based on different conditions. -To prevent lookup methods in models like this: - -```ruby -class Project - def issues_for_user_filtered_by(user, filter) - # A lot of logic not related to project model itself - end -end - -issues = project.issues_for_user_filtered_by(user, params) -``` - -Better use this: - -```ruby -issues = IssuesFinder.new.execute(project, user, filter) -``` - -It will help keep models thiner. diff --git a/app/finders/groups_finder.rb b/app/finders/groups_finder.rb deleted file mode 100644 index d3597ef0901..00000000000 --- a/app/finders/groups_finder.rb +++ /dev/null @@ -1,38 +0,0 @@ -class GroupsFinder - def execute(current_user, options = {}) - all_groups(current_user) - end - - private - - def all_groups(current_user) - if current_user - if current_user.authorized_groups.any? - # User has access to groups - # - # Return only: - # groups with public projects - # groups with internal projects - # groups with joined projects - # - group_ids = Project.public_and_internal_only.pluck(:namespace_id) + - current_user.authorized_groups.pluck(:id) - Group.where(id: group_ids) - else - # User has no group membership - # - # Return only: - # groups with public projects - # groups with internal projects - # - Group.where(id: Project.public_and_internal_only.pluck(:namespace_id)) - end - else - # Not authenticated - # - # Return only: - # groups with public projects - Group.where(id: Project.public_only.pluck(:namespace_id)) - end - end -end diff --git a/app/finders/issuable_finder.rb b/app/finders/issuable_finder.rb deleted file mode 100644 index 2c0702073d4..00000000000 --- a/app/finders/issuable_finder.rb +++ /dev/null @@ -1,160 +0,0 @@ -# IssuableFinder -# -# Used to filter Issues and MergeRequests collections by set of params -# -# Arguments: -# klass - actual class like Issue or MergeRequest -# current_user - which user use -# params: -# scope: 'created-by-me' or 'assigned-to-me' or 'all' -# state: 'open' or 'closed' or 'all' -# group_id: integer -# project_id: integer -# milestone_id: integer -# assignee_id: integer -# search: string -# label_name: string -# sort: string -# -require_relative 'projects_finder' - -class IssuableFinder - NONE = '0' - - attr_accessor :current_user, :params - - def execute(current_user, params) - @current_user = current_user - @params = params - - items = init_collection - items = by_scope(items) - items = by_state(items) - items = by_group(items) - items = by_project(items) - items = by_search(items) - items = by_milestone(items) - items = by_assignee(items) - items = by_author(items) - items = by_label(items) - items = sort(items) - end - - private - - def init_collection - table_name = klass.table_name - - if project - if Ability.abilities.allowed?(current_user, :read_project, project) - project.send(table_name) - else - [] - end - elsif current_user && params[:authorized_only].presence && !current_user_related? - klass.of_projects(current_user.authorized_projects).references(:project) - else - klass.of_projects(ProjectsFinder.new.execute(current_user)).references(:project) - end - end - - def by_scope(items) - case params[:scope] - when 'created-by-me', 'authored' then - items.where(author_id: current_user.id) - when 'all' then - items - when 'assigned-to-me' then - items.where(assignee_id: current_user.id) - else - raise 'You must specify default scope' - end - end - - def by_state(items) - case params[:state] - when 'closed' - items.closed - when 'all' - items - when 'opened' - items.opened - else - raise 'You must specify default state' - end - end - - def by_group(items) - if params[:group_id].present? - items = items.of_group(Group.find(params[:group_id])) - end - - items - end - - def by_project(items) - if params[:project_id].present? - items = items.of_projects(params[:project_id]) - end - - items - end - - def by_search(items) - if params[:search].present? - items = items.search(params[:search]) - end - - items - end - - def sort(items) - items.sort(params[:sort]) - end - - def by_milestone(items) - if params[:milestone_id].present? - items = items.where(milestone_id: (params[:milestone_id] == NONE ? nil : params[:milestone_id])) - end - - items - end - - def by_assignee(items) - if params[:assignee_id].present? - items = items.where(assignee_id: (params[:assignee_id] == NONE ? nil : params[:assignee_id])) - end - - items - end - - def by_author(items) - if params[:author_id].present? - items = items.where(author_id: (params[:author_id] == NONE ? nil : params[:author_id])) - end - - items - end - - def by_label(items) - if params[:label_name].present? - label_names = params[:label_name].split(",") - - item_ids = LabelLink.joins(:label). - where('labels.title in (?)', label_names). - where(target_type: klass.name).pluck(:target_id) - - items = items.where(id: item_ids) - end - - items - end - - def project - Project.where(id: params[:project_id]).first if params[:project_id].present? - end - - def current_user_related? - params[:scope] == 'created-by-me' || params[:scope] == 'authored' || params[:scope] == 'assigned-to-me' - end -end diff --git a/app/finders/issues_finder.rb b/app/finders/issues_finder.rb deleted file mode 100644 index 20a2b0ce8f0..00000000000 --- a/app/finders/issues_finder.rb +++ /dev/null @@ -1,22 +0,0 @@ -# Finders::Issues class -# -# Used to filter Issues collections by set of params -# -# Arguments: -# current_user - which user use -# params: -# scope: 'created-by-me' or 'assigned-to-me' or 'all' -# state: 'open' or 'closed' or 'all' -# group_id: integer -# project_id: integer -# milestone_id: integer -# assignee_id: integer -# search: string -# label_name: string -# sort: string -# -class IssuesFinder < IssuableFinder - def klass - Issue - end -end diff --git a/app/finders/merge_requests_finder.rb b/app/finders/merge_requests_finder.rb deleted file mode 100644 index b258216d0d4..00000000000 --- a/app/finders/merge_requests_finder.rb +++ /dev/null @@ -1,22 +0,0 @@ -# Finders::MergeRequest class -# -# Used to filter MergeRequests collections by set of params -# -# Arguments: -# current_user - which user use -# params: -# scope: 'created-by-me' or 'assigned-to-me' or 'all' -# state: 'open' or 'closed' or 'all' -# group_id: integer -# project_id: integer -# milestone_id: integer -# assignee_id: integer -# search: string -# label_name: string -# sort: string -# -class MergeRequestsFinder < IssuableFinder - def klass - MergeRequest - end -end diff --git a/app/finders/notes_finder.rb b/app/finders/notes_finder.rb deleted file mode 100644 index ab252821b52..00000000000 --- a/app/finders/notes_finder.rb +++ /dev/null @@ -1,27 +0,0 @@ -class NotesFinder - FETCH_OVERLAP = 5.seconds - - def execute(project, current_user, params) - target_type = params[:target_type] - target_id = params[:target_id] - # Default to 0 to remain compatible with old clients - last_fetched_at = Time.at(params.fetch(:last_fetched_at, 0).to_i) - - notes = - case target_type - when "commit" - project.notes.for_commit_id(target_id).not_inline - when "issue" - project.issues.find(target_id).notes.inc_author - when "merge_request" - project.merge_requests.find(target_id).mr_and_commit_notes.inc_author - when "snippet", "project_snippet" - project.snippets.find(target_id).notes - else - raise 'invalid target_type' - end - - # Use overlapping intervals to avoid worrying about race conditions - notes.where('updated_at > ?', last_fetched_at - FETCH_OVERLAP).fresh - end -end diff --git a/app/finders/projects_finder.rb b/app/finders/projects_finder.rb deleted file mode 100644 index c81bb51583a..00000000000 --- a/app/finders/projects_finder.rb +++ /dev/null @@ -1,89 +0,0 @@ -class ProjectsFinder - def execute(current_user, options = {}) - group = options[:group] - - if group - group_projects(current_user, group) - else - all_projects(current_user) - end - end - - private - - def group_projects(current_user, group) - if current_user - if group.users.include?(current_user) - # User is group member - # - # Return ALL group projects - group.projects - else - projects_members = ProjectMember.in_projects(group.projects). - with_user(current_user) - - if projects_members.any? - # User is a project member - # - # Return only: - # public projects - # internal projects - # joined projects - # - group.projects.where( - "projects.id IN (?) OR projects.visibility_level IN (?)", - projects_members.pluck(:source_id), - Project.public_and_internal_levels - ) - else - # User has no access to group or group projects - # - # Return only: - # public projects - # internal projects - # - group.projects.public_and_internal_only - end - end - else - # Not authenticated - # - # Return only: - # public projects - group.projects.public_only - end - end - - def all_projects(current_user) - if current_user - if current_user.authorized_projects.any? - # User has access to private projects - # - # Return only: - # public projects - # internal projects - # joined projects - # - Project.where( - "projects.id IN (?) OR projects.visibility_level IN (?)", - current_user.authorized_projects.pluck(:id), - Project.public_and_internal_levels - ) - else - # User has no access to private projects - # - # Return only: - # public projects - # internal projects - # - Project.public_and_internal_only - end - else - # Not authenticated - # - # Return only: - # public projects - Project.public_only - end - end -end diff --git a/app/finders/snippets_finder.rb b/app/finders/snippets_finder.rb deleted file mode 100644 index 07b5759443b..00000000000 --- a/app/finders/snippets_finder.rb +++ /dev/null @@ -1,63 +0,0 @@ -class SnippetsFinder - def execute(current_user, params = {}) - filter = params[:filter] - - case filter - when :all then - snippets(current_user).fresh.non_expired - when :by_user then - by_user(current_user, params[:user], params[:scope]) - when :by_project - by_project(current_user, params[:project]) - end - end - - private - - def snippets(current_user) - if current_user - Snippet.public_and_internal - else - # Not authenticated - # - # Return only: - # public snippets - Snippet.are_public - end - end - - def by_user(current_user, user, scope) - snippets = user.snippets.fresh.non_expired - - return snippets.are_public unless current_user - - if user == current_user - case scope - when 'are_internal' then - snippets.are_internal - when 'are_private' then - snippets.are_private - when 'are_public' then - snippets.are_public - else - snippets - end - else - snippets.public_and_internal - end - end - - def by_project(current_user, project) - snippets = project.snippets.fresh.non_expired - - if current_user - if project.team.member?(current_user.id) - snippets - else - snippets.public_and_internal - end - else - snippets.are_public - end - end -end diff --git a/app/finders/trending_projects_finder.rb b/app/finders/trending_projects_finder.rb deleted file mode 100644 index a79bd47d986..00000000000 --- a/app/finders/trending_projects_finder.rb +++ /dev/null @@ -1,19 +0,0 @@ -class TrendingProjectsFinder - def execute(current_user, start_date = nil) - start_date ||= Date.today - 1.month - - projects = projects_for(current_user) - - # Determine trending projects based on comments count - # for period of time - ex. month - projects.joins(:notes).where('notes.created_at > ?', start_date). - select("projects.*, count(notes.id) as ncount"). - group("projects.id").reorder("ncount DESC") - end - - private - - def projects_for(current_user) - ProjectsFinder.new.execute(current_user) - end -end |