Sha256: a233e6b24055c97fa85282fba4319469cc855247a90a1a536da7bb7d1946bbfc

Contents?: true

Size: 841 Bytes

Versions: 11

Compression:

Stored size: 841 Bytes

Contents

module Opal
  module Rails
    class TemplateHandler

      def self.call(template)
        new.call(template)
      end


      def call(template)
        escaped = template.source.gsub(':', '\:')
        string = '%q:' + escaped + ':'
        "Opal.compile('Object.new.instance_eval {' << #{assigns} << #{local_assigns} << #{string} << '}')"
      end

      private

      def local_assigns
        <<-'RUBY'.strip
          JSON.parse(local_assigns.to_json).map { |key, val| "#{key} = #{val.inspect};" }.join
        RUBY
      end

      def assigns
        <<-'RUBY'.strip
          JSON.parse(@_assigns.to_json).map { |key, val| "@#{key} = #{val.inspect};" }.join
        RUBY
      end
    end
  end
end

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
opal-rails-0.9.0 lib/opal/rails/template_handler.rb
opal-rails-0.8.1 lib/opal/rails/template_handler.rb
opal-rails-0.8.0 lib/opal/rails/template_handler.rb
opal-rails-0.8.0.rc1 lib/opal/rails/template_handler.rb
opal-rails-0.7.0 lib/opal/rails/template_handler.rb
opal-rails-0.7.0.beta2 lib/opal/rails/template_handler.rb
opal-rails-0.7.0.beta1 lib/opal/rails/template_handler.rb
opal-rails-0.6.3 lib/opal/rails/template_handler.rb
opal-rails-0.6.2 lib/opal/rails/template_handler.rb
opal-rails-0.6.1 lib/opal/rails/template_handler.rb
opal-rails-0.6.0 lib/opal/rails/template_handler.rb