Class: Iconly::ProjectIcon

Inherits:
ApplicationRecord show all
Defined in:
app/models/iconly/project_icon.rb

Class Method Summary collapse

Class Method Details

.all_projects(user_id, term = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/iconly/project_icon.rb', line 6

def self.all_projects(user_id, term = nil)
  user_projects = Project.owned_by(user_id)
  icons = includes(:icon, :project)
          .joins(:icon, :project)
          .merge(user_projects)
  icons = icons.merge(Icon.search(term)) if term.present?
  project_icons = icons
                  .order('iconly_projects.name')
                  .group_by(&:project)

  if term.blank?
    projects_without_icons = user_projects.without_icons
    projects_without_icons.each { |p| project_icons[p] = [] }
  end
  project_icons
end