docbrown/app/models/event.rb
Lito 2ebd37aba2 Improved article slug generation (#2218)
* Improved article slug generation

Ruby on Rails has a built-in slug generator since Rails 2.2

This generator fix the slug generation when non english characters are used on title improving SEO. For example: https://dev.to/lito_ordes/redimensionando-imgenes-en-tiempo-de-ejecucin-con-packer-57c6

* Ruby on Rails has a built-in slug generator since Rails 2.2

This generator fix the slug generation when non english characters are used on title improving SEO. For example: https://dev.to/lito_ordes/redimensionando-imgenes-en-tiempo-de-ejecucin-con-packer-57c6

* Ruby on Rails has a built-in slug generator since Rails 2.2 

This generator fix the slug generation when non english characters are used on title improving SEO. For example: https://dev.to/lito_ordes/redimensionando-imgenes-en-tiempo-de-ejecucin-con-packer-57c6

* Ruby on Rails has a built-in slug generator since Rails 2.2

This generator fix the slug generation when non english characters are used on title improving SEO. For example: https://dev.to/lito_ordes/redimensionando-imgenes-en-tiempo-de-ejecucin-con-packer-57c6

* Ruby on Rails has a built-in slug generator since Rails 2.2 

This generator fix the slug generation when non english characters are used on title improving SEO. For example: https://dev.to/lito_ordes/redimensionando-imgenes-en-tiempo-de-ejecucin-con-packer-57c6

* Fix downcase transform on event model
2019-03-26 13:39:35 -04:00

52 lines
1.4 KiB
Ruby

class Event < ApplicationRecord
mount_uploader :cover_image, CoverImageUploader
mount_uploader :profile_image, ProfileImageUploader
validates :title, length: { maximum: 90 }
validates :location_url, url: { allow_blank: true, schemes: %w[https http] }
validate :end_time_after_start
validates :slug, presence: { if: :published? }, format: /\A[0-9a-z-]*\z/
after_save :bust_cache
before_validation :create_slug
before_validation :evaluate_markdown
scope :in_the_future_and_published, lambda {
where("starts_at > ?", Time.current).
where(published: true)
}
scope :in_the_past_and_published, lambda {
where("starts_at < ?", Time.current).
where(published: true)
}
private
def evaluate_markdown
self.description_html = MarkdownParser.new(description_markdown).evaluate_markdown
end
def end_time_after_start
if ends_at.nil? || starts_at.nil?
errors.add(:starts_at, "and ends_at must not be nil")
elsif ends_at < starts_at
errors.add(:ends_at, "must be after start date")
end
end
def create_slug
self.slug = title_to_slug if slug.blank? && title.present? && published
end
def title_to_slug
downcase = (id.to_s + "-" + category + "-" + title).to_s
downcase.parameterize + "-" + starts_at.strftime("%m-%d-%Y")
end
def bust_cache
cache_buster = CacheBuster.new
cache_buster.bust("/events")
cache_buster.bust("/events?i=i")
end
end