Sha256: 4a9c3c90a0a5db33d6b70e861c1825d14b8fe29cbbbd8e7b2984f9f7addc1a33

Contents?: true

Size: 839 Bytes

Versions: 16

Compression:

Stored size: 839 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.parse('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

16 entries across 16 versions & 1 rubygems

Version Path
opal-rails-0.5.2 lib/opal/rails/template_handler.rb
opal-rails-0.5.1 lib/opal/rails/template_handler.rb
opal-rails-0.5.0 lib/opal/rails/template_handler.rb
opal-rails-0.5.0.pre.2 lib/opal/rails/template_handler.rb
opal-rails-0.5.0.pre lib/opal/rails/template_handler.rb
opal-rails-0.4.0 lib/opal/rails/template_handler.rb
opal-rails-0.3.8 lib/opal/rails/template_handler.rb
opal-rails-0.3.7 lib/opal/rails/template_handler.rb
opal-rails-0.3.6 lib/opal/rails/template_handler.rb
opal-rails-0.3.5 lib/opal/rails/template_handler.rb
opal-rails-0.3.4 lib/opal/rails/template_handler.rb
opal-rails-0.3.3 lib/opal/rails/template_handler.rb
opal-rails-0.3.2 lib/opal/rails/template_handler.rb
opal-rails-0.3.1 lib/opal/rails/template_handler.rb
opal-rails-0.3.0 lib/opal/rails/template_handler.rb
opal-rails-0.2.1 lib/opal/rails/template_handler.rb