diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-09-19 17:16:16 -0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-10-19 14:57:15 -0200 |
commit | d5a595b59702489c0b1026a0951157b8cfd3d65c (patch) | |
tree | 783c4eeb5223fc5590f69fc089841de38a183d4e /app/finders | |
parent | 52e0c3b565b7b177abbf8ea3bc573651060179a2 (diff) | |
download | gitlab-ce-d5a595b59702489c0b1026a0951157b8cfd3d65c.tar.gz |
Add LabelsFinder
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/labels_finder.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/app/finders/labels_finder.rb b/app/finders/labels_finder.rb new file mode 100644 index 00000000000..cf7018cf8a2 --- /dev/null +++ b/app/finders/labels_finder.rb @@ -0,0 +1,58 @@ +class LabelsFinder + def initialize(current_user, params = {}) + @current_user = current_user + @params = params + end + + def execute + items = init_collection + items = with_title(items) + sort(items) + end + + private + + attr_reader :current_user, :params + + def init_collection + label_ids = [] + label_ids << Label.where(group_id: projects.where.not(group: nil).select(:namespace_id)).select(:id) + label_ids << Label.where(project_id: projects).select(:id) + + union = Gitlab::SQL::Union.new(label_ids) + + Label.where("labels.id IN (#{union.to_sql})") + .reorder(title: :asc) + end + + def with_title(items) + items = items.where(title: title) if title.present? + items + end + + def sort(items) + items.reorder(title: :asc) + end + + def project_id + params[:project_id].presence + end + + def title + params[:title].presence + end + + def projects + return @projects if defined?(@projects) + + if project_id + @projects = ProjectsFinder.new.execute(current_user) + .where(id: project_id) + .reorder(nil) + else + @projects = Project.none + end + + @projects + end +end |