Sha256: 161f51f07078ce8a3ba8f5dc99331c0e32b41beb0b2c1f2bc9296cdc7d30d51f

Contents?: true

Size: 1.51 KB

Versions: 21

Compression:

Stored size: 1.51 KB

Contents

# Example usage:
#
#   $ echo gem 'ruby2js', require: 'ruby2js/rails' > Gemfile
#   $ bundle update
#   $ rails generate controller Say hello
#   $ echo 'alert "Hello world!"' > app/views/say/hello.js.rb
#   $ rails server
#   $ curl http://localhost:3000/say/hello.js
#
# Using an optional filter:
#
#   $ echo 'require "ruby2js/filter/functions"' > config/initializers/ruby2js.rb
#
# Asset Pipeline:
#
#  Ruby2JS registers ".rbs" (RuBy Script) extension.
#  You can add "ruby_thing.js.rbs" to your javascript folder
#  and '= require ruby_thing' from other js sources.
#
#  (options are not yet supported, but by requiring the appropriate files
#   as shown above, you can configure proejct wide.)
require 'ruby2js'

module Ruby2JS
  module Rails
    class Template
      cattr_accessor :default_format
      self.default_format = Mime[:js]
      def self.call(template)
        "Ruby2JS.convert(#{template.source.inspect}).to_s"
      end
    end

    ActionView::Template.register_template_handler :rb, Template

    class SprocketProcessor
      def initialize( file)
        @file = file
      end
      def render(context , _)
        context = context.instance_eval { binding } unless context.is_a? Binding
        Ruby2JS.convert(File.read(@file), binding: context).to_s
      end
    end

    class Engine < ::Rails::Engine
      engine_name "ruby2js"

      config.assets.configure do |env|
        env.register_engine '.rbs', SprocketProcessor, mime_type: 'text/javascript', silence_deprecation: true
      end

    end

  end

end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
ruby2js-3.5.2 lib/ruby2js/rails.rb
ruby2js-3.5.1 lib/ruby2js/rails.rb
ruby2js-3.5.0 lib/ruby2js/rails.rb
ruby2js-3.4.0 lib/ruby2js/rails.rb
ruby2js-3.3.6 lib/ruby2js/rails.rb
ruby2js-3.3.5 lib/ruby2js/rails.rb
ruby2js-3.3.4 lib/ruby2js/rails.rb
ruby2js-3.3.3 lib/ruby2js/rails.rb
ruby2js-3.3.2 lib/ruby2js/rails.rb
ruby2js-3.3.1 lib/ruby2js/rails.rb
ruby2js-3.3.0 lib/ruby2js/rails.rb
ruby2js-3.2.0 lib/ruby2js/rails.rb
ruby2js-3.1.2 lib/ruby2js/rails.rb
ruby2js-3.1.1 lib/ruby2js/rails.rb
ruby2js-3.1.0 lib/ruby2js/rails.rb
ruby2js-3.0.15 lib/ruby2js/rails.rb
ruby2js-3.0.14 lib/ruby2js/rails.rb
ruby2js-3.0.13 lib/ruby2js/rails.rb
ruby2js-3.0.12 lib/ruby2js/rails.rb
ruby2js-3.0.11 lib/ruby2js/rails.rb