You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.1 KiB

7 years ago
7 years ago
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: tags
  5. #
  6. # id :bigint(8) 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. def history
  20. days = []
  21. 7.times do |i|
  22. day = i.days.ago.beginning_of_day.to_i
  23. days << {
  24. day: day.to_s,
  25. uses: Redis.current.get("activity:tags:#{id}:#{day}") || '0',
  26. accounts: Redis.current.pfcount("activity:tags:#{id}:#{day}:accounts").to_s,
  27. }
  28. end
  29. days
  30. end
  31. class << self
  32. def search_for(term, limit = 5)
  33. pattern = sanitize_sql_like(term.strip) + '%'
  34. Tag.where('lower(name) like lower(?)', pattern).order(:name).limit(limit)
  35. end
  36. end
  37. end