Sha256: bf44c9da4a81cfacf551529f9395ba418a91db2eb94717f3749767105673a6a1

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

require 'active_support/json'

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 << ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(local_assigns)).map { |key, val| "\#{key} = \#{val.inspect};" }.join
          end

          if config.assign_instance_variables_in_templates?
            code << ActiveSupport::JSON.decode(ActiveSupport::JSON.encode(@_assigns)).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

4 entries across 4 versions & 1 rubygems

Version Path
opal-rails-2.0.3 lib/opal/rails/template_handler.rb
opal-rails-2.0.2 lib/opal/rails/template_handler.rb
opal-rails-2.0.1 lib/opal/rails/template_handler.rb
opal-rails-2.0.0 lib/opal/rails/template_handler.rb