Class: CategoryHashtagDataSource
- Inherits:
-
Object
- Object
- CategoryHashtagDataSource
- 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
- .category_to_hashtag_item(category) ⇒ Object
- .enabled? ⇒ Boolean
- .icon ⇒ Object
- .lookup(guardian, slugs) ⇒ Object
- .search(guardian, term, limit, condition = HashtagAutocompleteService.search_conditions[:contains]) ⇒ Object
- .search_sort(search_results, term) ⇒ Object
- .search_without_term(guardian, limit) ⇒ Object
- .type ⇒ Object
Class Method Details
.category_to_hashtag_item(category) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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.relative_url = category.url item.id = category.id # Single-level category heirarchy should be enough to distinguish between # categories here. item.ref = category.slug_ref end end |
.enabled? ⇒ Boolean
7 8 9 |
# File 'app/services/category_hashtag_data_source.rb', line 7 def self.enabled? true end |
.icon ⇒ Object
11 12 13 |
# File 'app/services/category_hashtag_data_source.rb', line 11 def self.icon "folder" end |
.lookup(guardian, slugs) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'app/services/category_hashtag_data_source.rb', line 34 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 = HashtagAutocompleteService.search_conditions[:contains]) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'app/services/category_hashtag_data_source.rb', line 45 def self.search( guardian, term, limit, condition = HashtagAutocompleteService.search_conditions[:contains] ) base_search = Category .secured(guardian) .select(:id, :parent_category_id, :slug, :name, :description) .includes(:parent_category) if condition == HashtagAutocompleteService.search_conditions[:starts_with] base_search = base_search.where("LOWER(slug) LIKE :term", term: "#{term}%") elsif condition == HashtagAutocompleteService.search_conditions[:contains] base_search = base_search.where("LOWER(name) LIKE :term OR LOWER(slug) LIKE :term", 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
69 70 71 72 73 74 75 |
# File 'app/services/category_hashtag_data_source.rb', line 69 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/services/category_hashtag_data_source.rb', line 77 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 |
.type ⇒ Object
15 16 17 |
# File 'app/services/category_hashtag_data_source.rb', line 15 def self.type "category" end |