docbrown/app/models/tag.rb
Ben Halpern 2771a2e866
Add Algolia to comments, modify button styles, add comments tab to reading list area (#32)
* Add Algolia to search and add Only My Posts filter

* Actually fix Algolia index possible issue

* Fix search issue

* Make minor adjustments to sponsorship sidebar

* Make submission rules headsup html allowed and remove devise trackable

* Remove devise_trackable from application_controller

* Adjust login CTA for /new

* Adjust string in test to reflect changes

* Quick fix for internal navigatioon draft caching issue

* Add ID to internal/articles

* Fix auth with Twitter in two places

* Added comments to algolia and modified design

* Update sidebar styles and make other small adjustments

* Clean up tag styling and other small improvements

* Finalize design adjustments

* Add indexing condition for comments

* Fix Algolia typo

* Fix Algolia indexing on comment
2018-03-04 22:25:08 -05:00

55 lines
1.6 KiB
Ruby

class Tag < ActsAsTaggableOn::Tag
acts_as_followable
mount_uploader :profile_image, ProfileImageUploader
mount_uploader :social_image, ProfileImageUploader
validates :text_color_hex, format: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/, allow_blank: true
validates :bg_color_hex, format: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/, allow_blank: true
validate :validate_alias
before_validation :evaluate_markdown
before_validation :pound_it
before_save :calculate_hotness_score
after_save :bust_cache
def submission_template_customized(param_0 = nil)
submission_template.gsub("PARAM_0", param_0)
end
private
def evaluate_markdown
self.rules_html = MarkdownParser.new(rules_markdown).evaluate_markdown
self.wiki_body_html = MarkdownParser.new(wiki_body_markdown).evaluate_markdown
end
def calculate_hotness_score
self.hotness_score = Article.tagged_with(name).
where("articles.featured_number > ?", 7.days.ago.to_i).
map do |a|
(a.comments_count * 14) + (a.reactions_count * 4) + rand(6) + ((taggings_count + 1 ) / 2)
end.
sum
end
def bust_cache
CacheBuster.new.bust("/t/#{name}")
CacheBuster.new.bust("/t/#{name}?i=i")
CacheBuster.new.bust("/t/#{name}/?i=i")
CacheBuster.new.bust("/t/#{name}/")
CacheBuster.new.bust("/tags")
end
def validate_alias
if alias_for.present? && !Tag.find_by_name(alias_for)
errors.add(:tag, "alias_for must refer to existing tag")
end
end
def pound_it
text_color_hex&.prepend("#") unless text_color_hex&.starts_with?("#")
bg_color_hex&.prepend("#") unless bg_color_hex&.starts_with?("#")
end
end