Sha256: 582dafde7e1764b1139ce9e6040542056a57962c533d082ef21aef19b10b8b70

Contents?: true

Size: 1.22 KB

Versions: 6

Compression:

Stored size: 1.22 KB

Contents

module Kms
  module CompileTemplates
    extend ActiveSupport::Concern

    included do
      validate :compiled_templates
      # on validation step object has no id,
      # that's why we need additional compile if validation pass
      after_commit :compile_templates, on: [:create, :update]
    end

    protected

    def compiled_templates
      Kms.template_manager.diagnostics.clear
      register_liquor_template
      return if Kms.template_manager.compile
      Kms.template_manager.errors.each do |error|
        splitted_error = error.message.split(':')
        errors.add(:content, [Kms.template_manager.decorate(error), splitted_error].flatten.join('<br>').gsub(/\s/, '&nbsp;'))
      end
    end

    def register_liquor_template
      if is_a?(Kms::Template)
        Kms.template_manager.register_layout(register_id, content, Kms::ExternalsRegistry.externals.keys)
      elsif is_a?(Kms::Page)
        Kms.template_manager.register_template(register_id, content, Kms::ExternalsRegistry.externals.keys)
      elsif is_a?(Kms::Snippet)
        Kms.template_manager.register_partial(register_id, content)
      end
    end

    def compile_templates
      register_liquor_template
      Kms.template_manager.compile
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kms-0.7.0 app/models/concerns/kms/compile_templates.rb
kms-0.6.0 app/models/concerns/kms/compile_templates.rb
kms-0.5.0 app/models/concerns/kms/compile_templates.rb
kms-0.4.2 app/models/concerns/kms/compile_templates.rb
kms-0.4.1 app/models/concerns/kms/compile_templates.rb
kms-0.4.0 app/models/concerns/kms/compile_templates.rb