Class: CategoryHashtagDataSource

Inherits:
Object
  • Object
show all
Defined in:
app/services/category_hashtag_data_source.rb

Overview

Used as a data source via HashtagAutocompleteService to provide category results when looking up a category slug via markdown or searching for categories via the # autocomplete character.

Class Method Summary collapse

Class Method Details

.category_to_hashtag_item(category) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/category_hashtag_data_source.rb', line 19

def self.category_to_hashtag_item(category)
  HashtagAutocompleteService::HashtagItem.new.tap do |item|
    item.text = category.name
    item.slug = category.slug
    item.description = category.description_text
    item.icon = icon
    item.colors = [category.parent_category&.color, category.color].compact
    item.relative_url = category.url
    item.id = category.id

    # Single-level category hierarchy should be enough to distinguish between
    # categories here.
    item.ref = category.slug_ref
  end
end

.enabled?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'app/services/category_hashtag_data_source.rb', line 7

def self.enabled?
  true
end

.find_by_ids(guardian, ids) ⇒ Object



35
36
37
# File 'app/services/category_hashtag_data_source.rb', line 35

def self.find_by_ids(guardian, ids)
  Category.secured(guardian).where(id: ids).map { |category| category_to_hashtag_item(category) }
end

.iconObject



11
12
13
# File 'app/services/category_hashtag_data_source.rb', line 11

def self.icon
  "folder"
end

.lookup(guardian, slugs) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'app/services/category_hashtag_data_source.rb', line 39

def self.lookup(guardian, slugs)
  user_categories =
    Category
      .secured(guardian)
      .includes(:parent_category)
      .order("parent_category_id ASC NULLS FIRST, id ASC")
  Category
    .query_loaded_from_slugs(slugs, user_categories)
    .map { |category| category_to_hashtag_item(category) }
end

.search(guardian, term, limit, condition = ) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/category_hashtag_data_source.rb', line 50

def self.search(
  guardian,
  term,
  limit,
  condition = HashtagAutocompleteService.search_conditions[:contains]
)
  base_search =
    Category
      .secured(guardian)
      .select(:id, :parent_category_id, :slug, :name, :description, :color)
      .includes(:parent_category)

  if condition == HashtagAutocompleteService.search_conditions[:starts_with]
    base_search = base_search.where("starts_with(LOWER(slug), LOWER(:term))", term: term)
  elsif condition == HashtagAutocompleteService.search_conditions[:contains]
    base_search =
      base_search.where(
        "position(LOWER(:term) IN LOWER(name)) <> 0 OR position(LOWER(:term) IN LOWER(slug)) <> 0",
        term: term,
      )
  else
    raise Discourse::InvalidParameters.new("Unknown search condition: #{condition}")
  end

  base_search.take(limit).map { |category| category_to_hashtag_item(category) }
end

.search_sort(search_results, term) ⇒ Object



77
78
79
80
81
82
83
# File 'app/services/category_hashtag_data_source.rb', line 77

def self.search_sort(search_results, term)
  if term.present?
    search_results.sort_by { |item| [item.slug == term ? 0 : 1, item.text.downcase] }
  else
    search_results.sort_by { |item| item.text.downcase }
  end
end

.search_without_term(guardian, limit) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/services/category_hashtag_data_source.rb', line 85

def self.search_without_term(guardian, limit)
  Category
    .includes(:parent_category)
    .secured(guardian)
    .where(
      "categories.id NOT IN (#{
        CategoryUser
          .muted_category_ids_query(guardian.user, include_direct: true)
          .select("categories.id")
          .to_sql
      })",
    )
    .order(topic_count: :desc)
    .take(limit)
    .map { |category| category_to_hashtag_item(category) }
end

.typeObject



15
16
17
# File 'app/services/category_hashtag_data_source.rb', line 15

def self.type
  "category"
end