Sha256: 85041634f5e04109420d2532a4f6ccdc62ee8c88edac4792635196e8d076ab52

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

module Opal
  module Rails
    class TemplateHandler

      def self.call(template, source = template.source)
        new.call(template, source)
      end

      def call(template, source = template.source)
        escaped = source.gsub(':', '\:')
        string = '%q:' + escaped + ':'

        <<-RUBY
          config = ::Rails.application.config.opal

          code = []
          code << 'Object.new.instance_eval {'

          if config.assign_locals_in_templates?
            code << JSON.parse(local_assigns.to_json).map { |key, val| "\#{key} = \#{val.inspect};" }.join
          end

          if config.assign_instance_variables_in_templates?
            code << JSON.parse(@_assigns.to_json).map { |key, val| "@\#{key} = \#{val.inspect};" }.join
          end

          code << #{string}
          code << '}'
          Opal.compile(code.join("\n"))
        RUBY
      end
    end
  end
end

ActiveSupport.on_load(:action_view) do
  ActionView::Template.register_template_handler :opal, Opal::Rails::TemplateHandler
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
opal-rails-1.1.2 lib/opal/rails/template_handler.rb
opal-rails-1.1.1 lib/opal/rails/template_handler.rb
opal-rails-1.1.0 lib/opal/rails/template_handler.rb