Class: TomifyRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/tomify_record.rb

Class Method Summary collapse

Class Method Details

.date_from(*attrs) ⇒ Object



23
24
25
26
27
28
29
30
# File 'app/models/tomify_record.rb', line 23

def self.date_from(*attrs)
  attrs.each do |attr|
    define_method("#{attr}_date") do
      value = read_attribute attr
      value && value.strftime("%m/%d/%y %I:%M %p")
    end
  end
end

.to_html(*attrs) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/tomify_record.rb', line 4

def self.to_html(*attrs)
  attrs.each do |attr|
    define_method("#{attr}_to_html") do
      Rails.cache.fetch("#{self.class.name.downcase}-#{id}-#{attr}") do
        markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
        markdown.render(self[attr] || "").html_safe
      end
    end
  end

  define_method("bust_html_cache") do
    attrs.each do |attr|
      Rails.cache.delete("#{self.class.name.downcase}-#{id}-#{attr}")
    end
  end

  after_save :bust_html_cache
end