Class: Newsly::LiquidModel

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

Direct Known Subclasses

Newsletter, Template

Instance Method Summary collapse

Instance Method Details

#after_validationObject

Puts the parse error from Liquid on the error list if parsing failed



9
10
11
# File 'app/models/newsly/liquid_model.rb', line 9

def after_validation
  errors.add :internal_template, @syntax_error unless @syntax_error.nil?
end

#body=(text) ⇒ Object

body contains the raw internal_template. When updating this attribute, the template parses the internal_template and stores the serialized object for quicker rendering.



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

def body=(text)
  self[:body] = text
  begin
    @internal_template = Liquid::Template.parse(text)
    self[:internal_template] = [Marshal.dump(@internal_template)].pack('m*')
  rescue Liquid::SyntaxError => error
    @syntax_error = error.message
  end
end

#render(options = {}) ⇒ Object

Methods

Renders body as Liquid Markup template



37
38
39
# File 'app/models/newsly/liquid_model.rb', line 37

def render(options = {})
  internal_template.render options
end