Class: DocContract::ContractTemplate

Inherits:
ApplicationRecord show all
Defined in:
app/models/doc_contract/contract_template.rb

Constant Summary collapse

HANDLEBARS_REGEX =
/{{((#)?[a-z._ ]+)}}/
RANSACKABLE_ATTRIBUTES =
%w[name].freeze

Constants inherited from ApplicationRecord

ApplicationRecord::RANSACKABLE_ASSOCIATIONS

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes

Instance Method Details

#configObject



15
16
17
# File 'app/models/doc_contract/contract_template.rb', line 15

def config
  YAML.safe_load(config_yml.to_s, permitted_classes: [Symbol, Date]) || {}
end

#contract_templateObject

Do not fail when an instance trait tries to access fallback template properties



20
21
22
# File 'app/models/doc_contract/contract_template.rb', line 20

def contract_template
  self
end

#present_handlebars_expressionsObject



44
45
46
# File 'app/models/doc_contract/contract_template.rb', line 44

def present_handlebars_expressions
  markdown.to_s.scan(HANDLEBARS_REGEX).map(&:first).uniq
end

#processed_htmlObject



38
39
40
41
42
# File 'app/models/doc_contract/contract_template.rb', line 38

def processed_html
  result = processed_markdown.sub(/.*\n...\n\n/m, '')
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, tables: true)
  markdown.render result
end

#processed_markdown(config_addittions = {}) ⇒ Object



24
25
26
27
28
# File 'app/models/doc_contract/contract_template.rb', line 24

def processed_markdown(config_addittions = {})
  config = self.config
  config.merge! config_addittions unless config_addittions.blank?
  DocContract::Handlebars.compile(markdown, config)
end

#template_markdown(config_addittions = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'app/models/doc_contract/contract_template.rb', line 30

def template_markdown(config_addittions = {})
  config = self.config
  config.merge! config_addittions unless config_addittions.blank?
  result = DocContract::Handlebars.compile(markdown, config)
  # prefix the markdown with the config (eisvogel pandoc)
  "#{config.to_yaml}...\n\n#{result}"
end