Sha256: 6f91ce0d5f35a55ecf8048462263ef23bf50a4006485523c543ed4cdf8296bdf

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

require_dependency 'landable/liquid'

module Landable
  module HasTemplates
    extend ActiveSupport::Concern

    included do
      has_and_belongs_to_many :templates, class_name: 'Landable::Template', join_table: templates_join_table_name

      before_save :save_templates!

      def template_names
        # sticking with what we know about liquid, rather than doing regex.
        # (though regex may be faster, should we need that optimization later.)
        @template_names ||= begin
          template = ::Liquid::Template.parse(body)
          template_names_for_node template.root
        end
      end

      def templates
        Landable::Template.where(slug: template_names)
      end

      # passthrough for body=; clears the template_names cache in the process
      def body=(body_val)
        @template_slug = nil
        @asset_names = nil
        self[:body] = body_val
      end

      # this looks weird; I swear it works
      def save_templates!
        self.templates = templates
      end

      private

      def template_names_for_node(node, names = [])
        # set up a recursing function to search for template tags
        if node.is_a? Landable::Liquid::TemplateTag
          names << node.template_slug unless names.include? node.template_slug
        end

        if node.respond_to?(:nodelist) && node.nodelist
          node.nodelist.each { |n| template_names_for_node n, names }
        end

        names
      end
    end

    module ClassMethods
      def templates_join_table_name
        "#{Landable.configuration.database_schema_prefix}landable.#{name.underscore.split('/').last}_templates"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
landable-1.14.0 app/models/concerns/landable/has_templates.rb
landable-1.13.2 app/models/concerns/landable/has_templates.rb