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



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

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
22
23
# 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
        options = { autolink: true, tables: true }
        options.merge!(self.markdown_options) if self.try(:markdown_options)
        markdown = Redcarpet::Markdown.new(Tomify::Markdown::HTML, options)
        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