tag.rb 777 B

123456789101112131415161718192021222324252627282930
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: tags
  5. #
  6. # id :integer not null, primary key
  7. # name :string default(""), not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. #
  11. class Tag < ApplicationRecord
  12. has_and_belongs_to_many :statuses
  13. HASHTAG_NAME_RE = '[[:word:]_]*[[:alpha:]_][[:word:]_]*'
  14. HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i
  15. validates :name, presence: true, uniqueness: true, format: { with: /\A#{HASHTAG_NAME_RE}\z/i }
  16. def to_param
  17. name
  18. end
  19. class << self
  20. def search_for(term, limit = 5)
  21. pattern = sanitize_sql_like(term) + '%'
  22. Tag.where('lower(name) like lower(?)', pattern).order(:name).limit(limit)
  23. end
  24. end
  25. end