docbrown/app/models/badge.rb
Michael Kohl b57ced5466 CacheBuster refactoring (#4766)
* Turn CacheBuster into a module

This class used no internal state, so repeatedly creating short-lived objects seems wasteful.

* Consistently use string interpolation and parenthesis

* Destructure arrays into meaningful names, formatting

* Fix request spec for internal classified listings controller

Interestingly this works when asserting directly on the module, but not on a double.
Asserting directly in the module seems sufficient for this test so the indirection
was removed.

* Turn CacheBuster into a module

This class used no internal state, so repeatedly creating short-lived objects seems wasteful.

* Fix specs after rebasing
2019-11-13 10:51:23 -05:00

29 lines
577 B
Ruby

class Badge < ApplicationRecord
mount_uploader :badge_image, BadgeUploader
has_many :badge_achievements
has_many :tags
has_many :users, through: :badge_achievements
validates :title, presence: true, uniqueness: true
validates :description, presence: true
validates :badge_image, presence: true
before_validation :generate_slug
after_save :bust_path
def path
"/badge/#{slug}"
end
private
def generate_slug
self.slug = title.to_s.parameterize
end
def bust_path
CacheBuster.bust(path)
CacheBuster.bust("#{path}?i=i")
end
end