Class: Tag
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Tag
- Includes:
- CommunityRelated
- Defined in:
- /var/apps/qpixel/app/models/tag.rb
Direct Known Subclasses
Class Method Summary collapse
-
.find_or_create_synonymized(name:, tag_set:) ⇒ Array(Tag, String)
Find or create a tag within a given tag set, considering synonyms.
-
.search(term) ⇒ ActiveRecord::Relation<Tag>
Fuzzy-searches tags by name, excerpt, and synonym name.
Instance Method Summary collapse
Methods inherited from ApplicationRecord
#attributes_print, fuzzy_search, match_search, #match_search, sanitize_for_search, sanitize_name, sanitize_sql_in, useful_err_msg, with_lax_group_rules
Class Method Details
.find_or_create_synonymized(name:, tag_set:) ⇒ Array(Tag, String)
Find or create a tag within a given tag set, considering synonyms. If a synonym is given as name then the primary tag for it is returned instead.
76 77 78 79 80 81 82 83 84 85 |
# File '/var/apps/qpixel/app/models/tag.rb', line 76 def self.find_or_create_synonymized(name:, tag_set:) existing = Tag.find_by(name: name, tag_set: tag_set) if existing.nil? synonyms = TagSynonym.joins(:tag).where(name: name, tags: { tag_set: tag_set }) synonymized_name = synonyms.exists? ? synonyms.first.tag.name : name [Tag.find_or_create_by(name: synonymized_name, tag_set: tag_set), synonymized_name] else [existing, name] end end |
.search(term) ⇒ ActiveRecord::Relation<Tag>
Fuzzy-searches tags by name, excerpt, and synonym name
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File '/var/apps/qpixel/app/models/tag.rb', line 37 def self.search(term) value = "%#{sanitize_sql_like(term.strip)}%" # Query to search on tags, the name is used for sorting. q1 = where('tags.name LIKE ?', value) .or(where('tags.excerpt LIKE ?', value)) .select('tags.*') # Query to search on synonyms, the synonym name is used for sorting. q2 = joins(:tag_synonyms) .where('tag_synonyms.name LIKE ?', value) .select('tags.*') # Select from the union of the above queries, select only the tag columns such that we can distinct them from(Arel.sql("((#{q1.to_sql}) UNION (#{q2.to_sql})) tags")) .order(Arel.sql(sanitize_sql_array(['name LIKE ? DESC, char_length(name), name', value]))) .select(Tag.column_names.map { |c| "tags.#{c}" }) .distinct end |
Instance Method Details
#all_children ⇒ Object
87 88 89 90 91 |
# File '/var/apps/qpixel/app/models/tag.rb', line 87 def all_children query = File.read(Rails.root.join('db/scripts/tag_children.sql')) query = query.gsub('$ParentId', id.to_s) ActiveRecord::Base.connection.execute(query).to_a.map(&:first) end |
#parent_chain ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File '/var/apps/qpixel/app/models/tag.rb', line 93 def parent_chain Enumerator.new do |enum| parent_group = group until parent_group.nil? enum.yield parent_group parent_group = parent_group.group end end end |