Module: Issuable::ClassMethods

Defined in:
app/models/concerns/issuable.rb

Instance Method Summary collapse

Instance Method Details

#full_search(query) ⇒ Object

Searches for records with a matching title or description.

This method uses ILIKE on PostgreSQL and LIKE on MySQL.

query - The search query as a String

Returns an ActiveRecord::Relation.


85
86
87
88
89
90
# File 'app/models/concerns/issuable.rb', line 85

def full_search(query)
  t = arel_table
  pattern = "%#{query}%"

  where(t[:title].matches(pattern).or(t[:description].matches(pattern)))
end

#order_downvotes_descObject


103
104
105
# File 'app/models/concerns/issuable.rb', line 103

def order_downvotes_desc
  order_votes_desc('thumbsdown')
end

#order_upvotes_descObject


107
108
109
# File 'app/models/concerns/issuable.rb', line 107

def order_upvotes_desc
  order_votes_desc('thumbsup')
end

#order_votes_desc(award_emoji_name) ⇒ Object


111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/concerns/issuable.rb', line 111

def order_votes_desc(award_emoji_name)
  issuable_table = self.arel_table
  note_table = Note.arel_table

  join_clause = issuable_table.join(note_table, Arel::Nodes::OuterJoin).on(
    note_table[:noteable_id].eq(issuable_table[:id]).and(
      note_table[:noteable_type].eq(self.name).and(
        note_table[:is_award].eq(true).and(note_table[:note].eq(award_emoji_name))
      )
    )
  ).join_sources

  joins(join_clause).group(issuable_table[:id]).reorder("COUNT(notes.id) DESC")
end

#search(query) ⇒ Object

Searches for records with a matching title.

This method uses ILIKE on PostgreSQL and LIKE on MySQL.

query - The search query as a String

Returns an ActiveRecord::Relation.


74
75
76
# File 'app/models/concerns/issuable.rb', line 74

def search(query)
  where(arel_table[:title].matches("%#{query}%"))
end

#sort(method) ⇒ Object


92
93
94
95
96
97
98
99
100
101
# File 'app/models/concerns/issuable.rb', line 92

def sort(method)
  case method.to_s
  when 'milestone_due_asc' then order_milestone_due_asc
  when 'milestone_due_desc' then order_milestone_due_desc
  when 'downvotes_desc' then order_downvotes_desc
  when 'upvotes_desc' then order_upvotes_desc
  else
    order_by(method)
  end
end

#with_label(title) ⇒ Object


126
127
128
129
130
131
132
# File 'app/models/concerns/issuable.rb', line 126

def with_label(title)
  if title.is_a?(Array) && title.count > 1
    joins(:labels).where(labels: { title: title }).group('issues.id').having("count(distinct labels.title) = #{title.count}")
  else
    joins(:labels).where(labels: { title: title })
  end
end