Coder Social home page Coder Social logo

opal / opal-rails Goto Github PK

View Code? Open in Web Editor NEW
484.0 16.0 64.0 641 KB

Bringing Ruby to Rails · Rails bindings for Opal

Home Page: http://opalrb.com/#getting-started-rails

Ruby 96.93% Shell 2.96% JavaScript 0.11%
opal rails ruby opal-rails sprockets rails-bindings

opal-rails's Introduction


Opal
Opal-Ruby 💛 JavaScript

Opal is a Ruby to JavaScript source-to-source compiler.
It also has an implementation of the Ruby corelib and stdlib.

Community:
Stack Overflow Backers on Open Collective Sponsors on Open Collective Slack Documentation
Code:
Gem Version Build Status Code Climate Coverage Status
Sponsors:
Nebulab: Open Source Fridays

Usage

See the website for more detailed instructions and guides for Rails, jQuery, Sinatra, rack, CDN, etc. https://opalrb.com.

Compiling Ruby code with the CLI (Command Line Interface)

Contents of app.rb:

puts 'Hello world!'

Then from the terminal

$ opal --compile app.rb > app.js # The Opal runtime is included by default
                                 # but can be skipped with the --no-opal flag

The resulting JavaScript file can be used normally from an HTML page:

<script src="app.js"></script>

Be sure to set the page encoding to UTF-8 inside your <head> tag as follows:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="app.js"></script></head>
  <body></body>
</html>

Just open this page in a browser and check the JavaScript console.

Compiling Ruby code from Ruby

Opal.compile is a simple interface to just compile a string of Ruby into a string of JavaScript code.

Opal.compile("puts 'wow'")  # => "(function() { ... self.$puts("wow"); ... })()"

Running this by itself is not enough; you need the opal runtime/corelib.

Using Opal::Builder

Opal::Builder can be used to build the runtime/corelib into a string.

Opal::Builder.build('opal') #=> "(function() { ... })()"

or to build an entire app including dependencies declared with require:

builder = Opal::Builder.new
builder.build_str('require "opal"; puts "wow"', '(inline)')
File.binwrite 'app.js', builder.to_s # must use binary mode for writing

Compiling Ruby code from HTML (or using it as you would with inline JavaScript)

opal-parser allows you to eval Ruby code directly from your HTML (and from Opal) files without needing any other building process.

So you can create a file like the one below, and start writing ruby for your web applications.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://cdn.opalrb.com/opal/current/opal.js"></script>
    <script src="https://cdn.opalrb.com/opal/current/opal-parser.js" onload="Opal.load('opal-parser')"></script>

    <script type="text/ruby">
      puts "hi"
    </script>

  </head>
  <body>
  </body>
</html>

Just open this page and check the JavaScript console.

NOTE: Although this is possible, this is not really recommended for production and should only be used as a quick way to get your hands on opal.

Running tests

Setup the project:

$ bin/setup

The test suite can be run using:

$ bundle exec rake

This command will run all RSpec and MSpec examples in sequence.

MSpec

MSpec tests can be run with:

$ rake mspec

Alternatively, you can just load up a rack instance using rackup, and visit http://localhost:9292/ in any web browser.

RSpec

RSpec tests can be run with:

$ rake rspec

Automated runs

A Guardfile with decent mappings between specs and lib/corelib/stdlib files is in place. Run bundle exec guard -i to start guard.

Code Overview

What code is supposed to run where?

  • lib/ code runs inside your Ruby env. It compiles Ruby to JavaScript.
  • opal/ is the runtime+corelib for our implementation (runs in browser).
  • stdlib/ is our implementation of Ruby's stdlib. It is optional (runs in browser).

lib/

The lib directory holds the Opal parser/compiler used to compile Ruby into JavaScript. It is also built ready for the browser into opal-parser.js to allow compilation in any JavaScript environment.

opal/

This directory holds the Opal runtime and corelib implemented in Ruby and JavaScript.

stdlib/

Holds the stdlib currently supported by Opal. This includes Observable, StringScanner, Date, etc.

Browser support

  • Internet Explorer 11
  • Firefox (Current - 1) or Current
  • Chrome (Current - 1) or Current
  • Safari (Current - 1) or Current
  • Opera (Current - 1) or Current

Any problems encountered using the browsers listed above should be reported as bugs.

(Current - 1) or Current denotes that we support the current stable version of the browser and the version that preceded it. For example, if the current version of a browser is 24.x, we support the 24.x and 23.x versions.

12.1x or (Current - 1) or Current denotes that we support Opera 12.1x as well as the last 2 versions of Opera. For example, if the current Opera version is 20.x, then we support Opera 12.1x, 19.x and 20.x but not Opera 15.x through 18.x.

Contributors

This project exists thanks to all the people who contribute. contributors

Versioning

Opal will broadly follow semver as a version policy, trying to bump the major version when introducing breaking changes. Being a language implementation we're also aware that there's a fine line between what can be considered breaking and what is expected to be "safe" or just "additive". Moving forward we'll attempt to better clarify what interfaces are meant to be public and what should be considered private.

Backers

Thank you to all our backers! 🙏 [Become a backer]

Become a Backer Button

Sponsors

Donations

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

Become a Sponsor Button

Sponsored Contributions

Nebulab Logo

License

(The MIT License)

Copyright (C) 2013-2021 by Adam Beynon and the Opal contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

opal-rails's People

Contributors

adambeynon avatar ajjahn avatar andriusch avatar andyobtiva avatar benjamintanweihao avatar brandondrew avatar cantonic avatar catmando avatar dfyx avatar elia avatar fkchang avatar hmdne avatar hubertlepicki avatar jgaskins avatar mdh avatar mibamur avatar patleb avatar rainchen avatar ruralocity avatar seand7565 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opal-rails's Issues

Custom asset directory

I created a custom asset directory (lib/assets).

I added it to the rails asset paths in my application.rb.

config.assets.paths << "#{Rails.root}/lib/"

I have a class in that custom asset directory (users.js.rb).

This asset does not get included (users.js.rb) when serving up my pages.

It does get included when the asset is located in app/assets/javascripts.

Any idea as to why my user.js.rb class in my custom asset directory isn't being included?

Spec's run two times via rake tasks

Converted to 0.8 from 0.7. Specs run once via /opal_spec but 2x via rake task. There are 13 examples, and the run looks like this

HEDGMAC18:spike1 fkchang$ rake opal:spec
2015-10-14 22:38:14.730 phantomjs[68288:d07] CoreText performance note: Client called CTFontCreateWithName() using name "Times New Roman" and got font with PostScript name "TimesNewRomanPSMT". For best performance, only use PostScript names when calling this API.
2015-10-14 22:38:14.731 phantomjs[68288:d07] CoreText performance note: Set a breakpoint on CTFontLogSuboptimalRequest to debug.

Finished


13 examples, 0 failures (time taken: 0.036)

Finished


26 examples, 0 failures (time taken: 0.022)

Add support for JST

Javascript templates are great to create rich DOM structures client-side. Integrating them into opal-rails could turn this example:

# app/views/posts/create.js.opal

post = Element.find('.post')
post.find('.title').html    = @post[:title]
post.find('.body').html     = @post[:body]
post.find('.comments').html = comments_html

into this:

# app/views/posts/create.js.opal
post = Element.find('.posts')
post << render partial: 'post', { locals: { comments_html: comments_html }
# app/assets/javascripts/templates/posts/_post.jst.skim
.post
   .title = @post['title']
   .body = @post['body']
   .comments = comments_html

The latter file could be EJS, Skim or whatever, or even better straight up ERB, Haml or Slim (pending opal-erb and opal-slim implementation) thanks to Opal.

Basically this amounts to implementing:

  • a template function that resolves the path to the template
  • a render function that calls JST[template(name)](locals)

For reference, here is an excerpt of actual CoffeeScript code code that I have in a project:

#= require_tree ./templates/bootstrap

jQuery ($) ->
    templates_root = 'templates/bootstrap'

    template = (name) -> "#{templates_root}/#{name}"

    $.extend bootstrap: {}
    $.extend $.bootstrap,
        dropdown: (title, items) ->
            JST[template 'dropdown'](title: title, items: items)

    $(document).ready ->
        $('.tooltip-trigger[data-title]').tooltip
            trigger: 'hover'

Integrate `Opal.use_gem` in some way

Opal now has a great feature to use gems in the front-end. From the command line, this can be done by writing opal -g gemname your_source.js.rb > output.js

When using the builder, one can use Opal.load_gem "gemname". There does not seems to be a way to do this with opal-rails directly yet.

I think the cleanest way would be to add a config option that accepts a list of gem names. Something like:

config.opal.included_gems = %w(foo bar baz)

problem with non-stupid-digest-assets gem and javascript_include_tag

I think I have found a problem with the javascript_include_tag helper method
it seems that the opal gem adds some code to it:

(redacted the src output with DOMAIN AND FILE for brevity)

"<script src=\"//[DOMAIN]/[FILE]-abd887fabfed639d6475aada7eef0724e9056f7bb0d519560ab17a925d48cd1.js\"></script><script>\n//<![CDATA[\n      if (typeof(Opal) !== 'undefined') {\n        Opal.mark_as_loaded(\"opal\");\nOpal.mark_as_loaded(\"widget/widget-agenda\");\nOpal.mark_as_loaded(\"rspec/matchers/built_in/have\");\nOpal.mark_as_loaded(\"diff/lcs\");\nOpal.mark_as_loaded(\"diff/lcs/hunk\");\nOpal.mark_as_loaded(\"fileutils\");\nOpal.mark_as_loaded(\"test/unit/assertions\");\nOpal.mark_as_loaded(\"coderay\");\nOpal.mark_as_loaded(\"optparse\");\nOpal.mark_as_loaded(\"shellwords\");\nOpal.mark_as_loaded(\"socket\");\nOpal.mark_as_loaded(\"uri\");\nOpal.mark_as_loaded(\"drb/drb\");\n        \n      }\n\n//]]>\n</script>"

And I think it conflicts with the non-stupid-digest-assets gem.
Cause it now uses the digest file name instead of the non-digest file name that non-stupid-digest-assets normally generates.

I haven't been able to debug it further yet.
But i hope someone knows what's going on, thanks.

Some rails model to client side model converter.

Im writing a pure json based app, which needs to send a model down to the client. Writing the client with JS is easy, as we can use #to_json. But, wanting to use opal on the client, this is tricky and currently im having to generate code using the full opal parser which is really slow.

Some ActiveRecord::Base#to_opal method could be useful, where we assume the client side model has the same name, and it can generate ruby along the lines of:

# running on client
MyModel.load(first_name: "Adam", last_name: "Beynon, ...)

Of course, the #to_opal method would generate javascript, not ruby as we dont want the opal parser to be included. As we are dealing with simple hashes, and a simple method call, this can be "hand coded".

Just a thought for a problem im facing..

require Haml template file generates js Errors

I am having some problems getting templates to work.
When I require a template file the opal parser finds the file(as long as the extension is .haml and not .html.haml)
But it generates the following errors in js:

developer_tools_-_http___localhost_3000_activiteiten_nieuw

following code in the initializer:

Rails.application.config.assets.paths << Rails.root.join('app', 'views', 'shared').to_s

code in the haml file:

= link_to icon('bold'), '#', :class => 'btn btn-small toggle_bold'
= link_to icon('italic'), '#', :class => 'btn btn-small toggle_italic'
= link_to icon('link'), '#', :class => 'btn btn-small toggle_link'

required in application.js.rb:

...
require 'opal'
require 'opal-haml'
require 'opal-jquery'
require '_textile_buttons'
...

Not really sure what i am missing here, that it's not working

Collapse Key Dependencies via Sprockets?

Not if this has been thought of or is feasible but most of the time, when people are developing with Rails, etc., they may not need each Opal (or for that matter RSpec) file supplied to their browser as an individual file. That creates a lot of requests, which even with caching/304 responses, slows down the browser during development (production is not a problem because everything gets precompiled).

Might it be possible to tweak Sprockets to make this happen? It would be kind of an asset precompile on the fly but only for certain assets.

I know it's easy to programmatically compile using the Sprockets::Manifest class but that's the easy part. The hard part is changing the behavior as assets are required/served. It can't be done in Sprockets::Server because that's too late in the cycle.

Object [object Object] has no method '$hide'

Hi,

I wonder, why when I try to call hide method on Element object it returns:

Uncaught TypeError: Object [object Object] has no method '$hide'

as far as I see this wrapper is implemented in opal-jquery 0.0.1 and opal-rails use it ...

I write something like this

element = Document.id('hide_me')
element.hide

Using opal-jquery in project

Hi, I am trying to include opal-jquery into my rails application but it is not working for me. Is there a standard way to include it? Thanks!

I have 'opal-jquery' in my Gemfile and I am calling //= require opal-jquery in my application.js file.

Wrong content-type set for Opal responses when submitting a form remotely in rails

I was trying a variation of this:

https://github.com/opal/opal-rails#as-a-template

to create a chat app example. When I submit a form 'remotely' using rails I get a response with the content-type set to 'text/html'. If I force the response type to be 'application/javascript' the app works as expected, i.e. from the example change this:

    render type: :js, locals: { messages_html: render_to_string(@message) }

to this:

render type: :js, content_type: 'application/javascript', locals: { messages_html: render_to_string(@message) }

I've created an example app https://github.com/rainkinz/messaging-with-faye that demonstrates the issue.

ERB opal partial is not working when using `locales`

I am rendering an Opal partial in my index.html using erb and locales variables.
index.html.erb:

<div id="element3-opal">From a .opal partial, with ERB `locals`, is NOT working!</div>
<script>
    <%= raw render :partial => "opal_partial2o.opal", formats: [:js],  handlers: [:erb],
                               locals: {localvar: "LOCAL VAR - opal"} %>
</script>

opal_partial2o.opal:

Document.ready? do
  Element.find("#element3-opal").text("ERB and opal. localvar = `<%= localvar %>`")
end

RESULT:

<div id="element3-opal">ERB and opal. localvar = `&lt;%= localvar %&gt;`</div>

But the .opal file is not processed by ERB (the locales variable are not inserted into the script).
I tried all solutions I could think of (renamed the files to .js .opal .erb ... , changed handlers: on the render, etc but nothing worked. I tried with Opal 0.7.1 and 0.8.

I made the same example using coffee + erb + locales and all worked fine:

<div id="element3-coffee">ERB and coffee. localvar = `LOCAL VAR - coffee`</div>

I also made the same example using Opal + haml + locales and all worked fine:

<div id="element3-opal-haml">HAML and opal. localvar = `LOCAL VAR - opal_haml`</div>

For my full example please refer to this PR:
https://github.com/razorcd/opal-rails-example/tree/adds_opal_test_details

Is this an Opal + ERB bug?

TypeError: undefined is not a function

When I tried to run the following code:

result = ''
10.times { result << "test " }
result

I got this error:

TypeError: undefined is not a function
TypeError: undefined is not a function
    at $a._p.TMP_1 (eval at <anonymous> (http://opalrb.org/javascripts/try.js:18:31863), <anonymous>:9:23)
    at Number.d.$times._ [as $times] (http://opalrb.org/javascripts/try.js:8:15686)
    at eval (eval at <anonymous> (http://opalrb.org/javascripts/try.js:18:31863), <anonymous>:9:64)
    at $TryOpal.eval (eval at <anonymous> (http://opalrb.org/javascripts/try.js:18:31863), <anonymous>:11:3)
    at $TryOpal.def.$eval_code (http://opalrb.org/javascripts/try.js:18:31858)
    at $TryOpal.def.$run_code (http://opalrb.org/javascripts/try.js:18:31752)
    at e._p.o (http://opalrb.org/javascripts/try.js:18:31173)
    at HTMLAnchorElement.s (http://opalrb.org/javascripts/try.js:18:11768)
    at HTMLAnchorElement.K.event.dispatch (http://opalrb.org/javascripts/application.js:2:4882)
    at HTMLAnchorElement.s.handle.a (http://opalrb.org/javascripts/application.js:2:782)

Opal version: 0.6.2
Ruby version: 2.1.3

Ps.: the same happening on http://opalrb.org/try/.

Breaks existing JS.

Hi, nice gem and I'd really like to use it in my work's rails app
but
we have a lot of JS already developed and when I added and required the gem, it broke existing JS functions used in the site.
If require "opal", etc on page-specific js.rb files the existing JS files elsewhere in the application are loaded and work proper on their pages but once you navigate to a page requiring "opal" the older JS functions stop working.
I did find that refreshing the page or opening a link in a new tab works for obvious reasons but that's not really a solution.

Do you think I'm doing something wrong in my installation or can Opal not play nice with regular js files?

Thanks so much!

require executes module code

I Noticed that when I require a module that it already executes the code inside it

module LittleHelper
  puts 'this is inside a module'
end
require 'little_helper'

class ExampleClass
end

I wouldn't expect a message but it already displays the puts, even though I didn't do a include in the class yet.

eval method not working

eval method is not working, throws Uncaught NoMethodError: undefined methodeval' for main`, tried bulding new apps, and on different machines with different versions.
edit:
e.g.
eval "alert 'FOO!'"
<====== excetion raised
eval "alert 'BAR'" <======this and further not executed
everything else works fine.

Rack 2 broken back-comp

Rails 5 requries rack 2, however rack changed libraries naming.
Additional problem, that only way to known VERSION (rack calls it RELEASE) of rack is to require 'rack.rb' itself.

The problem is in main opal repo, lib/opal/sprockets/server.rb:7 (no found in rack ~> 2.0)

using opal in HAML filter not working

when trying to use opal in a HAML filter like...

#content
  Foobar

:opal
  Document.ready? do
    puts 'yay'
  end

... throws error in javascript console: Uncaught ReferenceError: Opal is not defined

in my application.js opal is required at the top.

using

  • opal-rails 0.7.1
  • rails 4.1.9
  • ruby 2.2.0

instance variables in Opal JS templates lead to error

# app/controllers/posts_controller.rb
class PostsController < ApplicationController
  def show
    @post = Post.find(params[:id])
  end
end

When you make a javascript call to PostsControler#show, @post should be available in app/views/posts/show.js.opal, but for me it leads to a Stack level too deep error.

Transcripts from https://gitter.im/opal/opal:

cantonic: is the stack level too deep error happening because the template handler tries to transform the instance variables and all their methods etc into json? If so, could we change the template handler to perform to_json only on the instances attributes? or maybe even totally remove the instances from automatically being available and instead pass local variables to the js.opal file if necessary?

elia: simplest solution would be to set those vars only when needed by the view, but that's probably my hate for passing data via ivars that's talking here
otherwise we could try to detect if they're used in the view
hackish but probably do-able
last option–which i don't like– would be to make it a config flag

cantonic: I like the first solution. saves resources, even though one could expect view templates to have access to ivars since they are also available in html or js.erb template views.
elia: for the first solution the only problem would be detecting locals, but probably if locals are set we can trust it's done intentionally

From my logs:

SystemStackError - stack level too deep:
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `block in as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `each'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `map'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:50:in `as_json'
  ... repeating hundreds of times...
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `block in as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `each'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `map'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:50:in `as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:140:in `block in as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:140:in `map'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:140:in `as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `block in as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `each'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `map'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:50:in `as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:140:in `block in as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:140:in `map'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:140:in `as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `block in as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `each'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `map'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:50:in `as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `block in as_json'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `each'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `map'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:159:in `as_json'
  activesupport (4.1.9) lib/active_support/json/encoding.rb:34:in `encode'
  activesupport (4.1.9) lib/active_support/json/encoding.rb:21:in `encode'
  activesupport (4.1.9) lib/active_support/core_ext/object/json.rb:37:in `to_json_with_active_support_encoder'
  app/views/drive/directories/create.js.opal:1:in `_app_views_drive_directories_create_js_opal___368000449424496302_70114515301600'
  actionview (4.1.9) lib/action_view/template.rb:145:in `block in render'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `instrument'
  actionview (4.1.9) lib/action_view/template.rb:339:in `instrument'
  actionview (4.1.9) lib/action_view/template.rb:143:in `render'
  xray-rails (0.1.14) lib/xray/engine.rb:33:in `render_with_xray'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:55:in `block (2 levels) in render_template'
  actionview (4.1.9) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `instrument'
  actionview (4.1.9) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:54:in `block in render_template'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:62:in `render_with_layout'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:53:in `render_template'
  actionview (4.1.9) lib/action_view/renderer/template_renderer.rb:17:in `render'
  actionview (4.1.9) lib/action_view/renderer/renderer.rb:42:in `render_template'
  actionview (4.1.9) lib/action_view/renderer/renderer.rb:23:in `render'
  actionview (4.1.9) lib/action_view/rendering.rb:99:in `_render_template'
  actionpack (4.1.9) lib/action_controller/metal/streaming.rb:217:in `_render_template'
  actionview (4.1.9) lib/action_view/rendering.rb:82:in `render_to_body'
  actionpack (4.1.9) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
  actionpack (4.1.9) lib/action_controller/metal/renderers.rb:32:in `render_to_body'
  actionpack (4.1.9) lib/abstract_controller/rendering.rb:25:in `render'
  actionpack (4.1.9) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
  activesupport (4.1.9) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
  /Users/cantonic/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
  activesupport (4.1.9) lib/active_support/core_ext/benchmark.rb:12:in `ms'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
  activerecord (4.1.9) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:40:in `render'
  app/controllers/drive/directories_controller.rb:31:in `create'
  actionpack (4.1.9) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.1.9) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.1.9) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.1.9) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.1.9) lib/active_support/callbacks.rb:113:in `call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:113:in `call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.9) lib/active_support/callbacks.rb:149:in `call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.9) lib/active_support/callbacks.rb:229:in `call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.9) lib/active_support/callbacks.rb:229:in `call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.9) lib/active_support/callbacks.rb:86:in `call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:86:in `run_callbacks'
  actionpack (4.1.9) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.1.9) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.9) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.9) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.1.9) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.1.9) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.1.9) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.1.9) lib/abstract_controller/base.rb:136:in `process'
  actionview (4.1.9) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.1.9) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.1.9) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.1.9) lib/action_controller/metal.rb:232:in `block in action'
  actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:82:in `call'
  actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
  actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:50:in `call'
  actionpack (4.1.9) lib/action_dispatch/journey/router.rb:73:in `block in call'
  actionpack (4.1.9) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.1.9) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.9) lib/action_dispatch/routing/route_set.rb:685:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  xray-rails (0.1.14) lib/xray/middleware.rb:37:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  jquery-fileupload-rails (0.4.4) lib/jquery/fileupload/rails/middleware.rb:14:in `_call'
  jquery-fileupload-rails (0.4.4) lib/jquery/fileupload/rails/middleware.rb:10:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/rack/agent_hooks.rb:26:in `traced_call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/rack/browser_monitoring.rb:23:in `traced_call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/rack/developer_mode.rb:48:in `traced_call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:55:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `catch'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/flash.rb:254:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/cookies.rb:562:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.1.9) lib/active_record/query_cache.rb:36:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.1.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activerecord (4.1.9) lib/active_record/migration.rb:380:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.9) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  railties (4.1.9) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.9) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.9) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.9) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.9) lib/rails/rack/logger.rb:20:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  request_store (1.1.0) lib/request_store/middleware.rb:8:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  activesupport (4.1.9) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  actionpack (4.1.9) lib/action_dispatch/middleware/static.rb:84:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  railties (4.1.9) lib/rails/engine.rb:514:in `call'
  railties (4.1.9) lib/rails/application.rb:144:in `call'
  newrelic_rpm (3.9.6.257) lib/new_relic/agent/instrumentation/middleware_tracing.rb:57:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /Users/cantonic/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /Users/cantonic/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /Users/cantonic/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'

Update dependencies

I want to use:

  • activesupport 3.2.16 (instead of 3.2.13)
  • builder 3.2.2 (instead of 3.0.0)

But bundler tells me opal-rails needs these old dependencies.

Spring Support

Once all of the work going on with opal-rspec 0.5 is complete, it might be useful to integrate spring support in. That way the Rake task can start up and the Rack process can stay loaded, allowing quick re-runs of specs using the Rake task.

unreachable code after return statement

When I try to run the example in my pre-existing Rails site I get the following error:

unreachable code after return statement

I click the link next to the error (edit: warning) and get the last line in this function highlighted:

def.$alias_method = function(newname, oldname) {
      var self = this;


      var newjsid = '$' + newname,
          body    = self.$$proto['$' + oldname];

      if (self.$$is_singleton) {
        self.$$proto[newjsid] = body;
      }
      else {
        Opal.defn(self, newjsid, body);
      }

      return self;

      return self;
    };

Using opal-rails 0.8.1 and Rails 4.2.5

Is locked into particular versions of opal

Is there any reason that opal-rails is locked to particular versions of opal? I was wanting to use opal-rspec and this caused me to fork this just to get my app running again (because of incompatible dependencies). Once I allowed newer versions of opal, it seemed to run fine.

Document finder seems not work

Hello!

# brand.js.rb

class Category
  attr_accessor :id

  def initialize
    @id = 'id'
  end
end

Document.ready? do
  cat = Category.new
  puts cat.id
  puts Document['.container']
end
// application.js
//= require opal
//= require jquery
//= require jquery_ujs
//= require opal-jquery
//= require twitter/bootstrap
//= require bootstrap-datepicker/core
//= require bootstrap-datepicker/locales/bootstrap-datepicker.ru.js
//= require_tree .
//= require_self

Chrome.console

id
Uncaught TypeError: Cannot read property '$to_s' of undefined kernel.js:388

$('.container')

[<div class=​"container">​…​</div>​, <div class=​"container nav-collapse">​…​</div>​, <div class=​"container">​…​</div>​, <div class=​"container">​…​</div>​]

what am i doing wrong?

using import instead of sprockets statements does not work

After creating a new rails project with

rails new o --javascript=opal

(after adding a new test controller and related route)

and renaming application.js to application.js.rb with the following content

#= require opal
#= require opal_ujs
#= require turbolinks
#= require_tree .

puts "test" 

everything just works.

But if I replace (as advised in the README) the application.js.rb with this

require 'opal'
require 'opal_ujs'
require 'turbolinks'
require_tree '.'

puts "test"

I get a RuntimeError

An error occurred while compiling: turbolinks

parse error on value "BEFORE_CHANGE" (tLABEL) :turbolinks:14
Source: turbolinks:14:16
  BEFORE_CHANGE:  'page:before-change'
~~~~~~~~~~~~~~~^

at the javascript_include_tag line

  <title>O</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

am I doing something wrong or is this a bug?

Unable to resolve opal-rails dependencies

In my Rails 4 application Gemfile I have the following :

gem 'opal-rails', github: 'opal/opal-rails'
gem 'opal-haml',  github: 'opal/opal-haml'
gem 'vienna',     github: 'opal/vienna'

Dependency resolution fails with the following message:

Could not find gem 'opal (~> 0.6.0) ruby', which is required by gem 'opal-rails (>= 0) ruby', in any of the sources.

Once I add opal to my Gemfile, as follows :

gem 'opal',       github: 'opal/opal'
gem 'opal-rails', github: 'opal/opal-rails'
gem 'opal-haml',  github: 'opal/opal-haml'
gem 'vienna',     github: 'opal/vienna'

Dependency resolution fails with the message:

Bundler could not find compatible versions for gem "opal":
  In Gemfile:
    opal-rails (>= 0) ruby depends on
      opal (~> 0.5.0) ruby

    opal (0.6.0)

When I try to use a specific version tag of opal :

gem 'opal',       github: 'opal/opal', tag: 'v0.5.0'
gem 'opal-rails', github: 'opal/opal-rails'
gem 'opal-haml',  github: 'opal/opal-haml'
gem 'vienna',     github: 'opal/vienna'

Dependency resolution fails with the message:

Bundler could not find compatible versions for gem "opal":
  In Gemfile:
    opal-rails (>= 0) ruby depends on
      opal (~> 0.6.0) ruby

    opal (0.5.0)

Inspecting the Gemfile of opal/opal-rails it seems to simply depend on opal/opal
So why am I getting these errors and how do I resolve them ? Thank you for any help in advance.

I am using MRI Ruby with RVM on Ubuntu :

ruby -v 
ruby 2.0.0p353 (2013-11-22 revision 43784) [i686-linux]

the example looks broken in README.md for Opal 0.8.0

FYI, I'm using rails 4.2.2.
I followed the instruction in README.md, using the example codes as:

# app/assets/javascripts/greeter.js.rb

puts "G'day world!" # check the console!

# Dom manipulation
require 'opal-jquery'

Document.ready? do
  Element.find('body > header').html = '<h1>Hi there!</h1>'
end

these will be output as:

/* Generated by Opal 0.8.0 */
Opal.modules["greeter"] = function(Opal) {
  Opal.dynamic_require_severity = "ignore";
  var $a, $b, TMP_1, self = Opal.top, $scope = Opal, nil = Opal.nil, $breaker = Opal.breaker, $slice = Opal.slice;

  Opal.add_stubs(['$puts', '$require', '$ready?', '$html=', '$find']);
  self.$puts("G'day world!");
  self.$require("opal-jquery");
  return ($a = ($b = $scope.get('Document'))['$ready?'], $a.$$p = (TMP_1 = function(){var self = TMP_1.$$s || this, $a, $b;

  return (($a = ["<h1>Hi there!</h1>"]), $b = $scope.get('Element').$find("body > header"), $b['$html='].apply($b, $a), $a[$a.length-1])}, TMP_1.$$s = self, TMP_1), $a).call($b);
};

and the html of layout is:

<!-- other scripts -->
<script src="/assets/application.self-a03a22d638ad9cc4cad6350ae4b348cb82212146c1ff8800e513ea41b4e80317.js?body=1" data-turbolinks-track="true"></script><script>
//<![CDATA[
      if (typeof(Opal) !== 'undefined') {
        Opal.mark_as_loaded("opal");
Opal.mark_as_loaded("application");
Opal.mark_as_loaded("corelib/runtime.self");
Opal.mark_as_loaded("jquery.self");
Opal.mark_as_loaded("jquery_ujs.self");
Opal.mark_as_loaded("turbolinks.self");
Opal.mark_as_loaded("rspec/matchers/built_in/have");
Opal.mark_as_loaded("diff/lcs");
Opal.mark_as_loaded("diff/lcs/hunk");
Opal.mark_as_loaded("fileutils");
Opal.mark_as_loaded("test/unit/assertions");
Opal.mark_as_loaded("coderay");
Opal.mark_as_loaded("optparse");
Opal.mark_as_loaded("shellwords");
Opal.mark_as_loaded("socket");
Opal.mark_as_loaded("uri");
Opal.mark_as_loaded("drb/drb");

      }

//]]>
</script>
  <meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="VEr94Vmz3Grxv0qcmePwGRT2arfB8gQJWao3BfOusT5+U0EyziSOZjbuiLMG9YMOhwNSOaDG5h5aZ3T1T/X49g==" />
</head>
<body class="controller-colors">

<h1>Hello Opal</h1>
<p>This is simple Opal application</p>



</body>
</html>

But nothing goes to the Chrome console and nothing changed for the page.
It just looks like these codes do not get executed at all.
Any clue for this?

No route matches [GET] "/javascripts/opal_spec_runner_xxx.js"

https://travis-ci.org/opal/opal-rails/jobs/56539509#L177-L180

This spec only fails on Travis:

  1) In-browser specs runner runs single spec file
     Failure/Error: Unable to find matching line from backtrace
     ActionController::RoutingError:
       No route matches [GET] "/javascripts/opal_spec_runner_b1ba865074f563388c5fbcd5c5dcc04276ab1dfd.js"

Some context
The asset is built on the fly with a name based on its contents (just like sprockets digest), the contents varies depending on the specs required for a particular run and it's done to avoid two open spec pages trying to write the same file. the file's dirname is added to sprockets at rails init time.

@josh any idea on why it doesn't find the assets and sets /javascripts/ as the prefix?

Can't seem to get Opal working in Rails

I've created a barebones test project here: https://github.com/erlingur/opal-test

I've followed all of the instructions as best as I can see and where it falls down is the puts "G'day world!" line you have in the example of app/assets/javascripts/greeter.js.rb. My version just doesn't output anything to the console.

https://github.com/erlingur/opal-test/blob/master/app/assets/javascripts/home.js.rb

It's most likely just me misunderstanding the role Opal plays but I thought, according to the opal-rails docs, that this should work.

Any clues?

'rails generate controller' fails at Opal::Generators::AssetsGenerator

rails 4.1.7
opal-rails 0.7.0.beta2
ruby 2.1.5

➜  opalapp git:(calendar) ✗ rails g controller Calendars index
Setting up the application without customisations (NO presentation mode).
skipping fog configuration
      create  app/controllers/calendars_controller.rb
       route  get 'calendars/index'
      invoke  haml
      create    app/views/calendars
      create    app/views/calendars/index.html.haml
      invoke  rspec
      create    spec/controllers/calendars_controller_spec.rb
      create    spec/views/calendars
      create    spec/views/calendars/index.html.haml_spec.rb
      invoke  helper
      create    app/helpers/calendars_helper.rb
      invoke    rspec
      create      spec/helpers/calendars_helper_spec.rb
      invoke  assets
      invoke    opal
/Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/opal-rails-0.7.0.beta2/lib/rails/generators/opal/assets/assets_generator.rb:11:in `initialize': uninitialized constant Rails::Generators::ModelHelpers (NameError)
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:226:in `new'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:226:in `dispatch'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:115:in `invoke'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/railties-4.1.7/lib/rails/generators/rails/assets/assets_generator.rb:17:in `block in <class:AssetsGenerator>'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:274:in `instance_exec'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:274:in `block in _invoke_for_class_method'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/shell.rb:68:in `with_padding'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:266:in `_invoke_for_class_method'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:133:in `_invoke_from_option_javascript_engine'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:115:in `invoke'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:277:in `block in _invoke_for_class_method'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/shell.rb:68:in `with_padding'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:266:in `_invoke_for_class_method'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:133:in `_invoke_from_option_assets'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `block in invoke_all'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `each'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `map'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/invocation.rb:133:in `invoke_all'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/group.rb:232:in `dispatch'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/railties-4.1.7/lib/rails/generators.rb:157:in `invoke'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/railties-4.1.7/lib/rails/commands/generate.rb:11:in `<top (required)>'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:135:in `generate_or_destroy'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:51:in `generate'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/railties-4.1.7/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
  from /Users/cantonic/.rvm/gems/ruby-2.1.5@opalapp_new/gems/railties-4.1.7/lib/rails/commands.rb:17:in `<top (required)>'
  from /Users/cantonic/Rails/opalapp_project/opalapp/bin/rails:8:in `<top (required)>'
  from /Users/cantonic/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
  from /Users/cantonic/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
  from -e:1:in `<main>'

duplicated from opal/opal - source maps not working with clean rails 4x install

Started GET "/assets/spec/models/opal_spec.rb" for 67.240.177.41 at 2015-03-04 01:10:58 +0000

Sprockets::FileNotFound (couldn't find file 'spec/models/opal_spec.rb'):
sprockets (2.12.3) lib/sprockets/base.rb:153:in resolve' opal (0.7.0) lib/opal/sprockets/server.rb:48:incall'
rack (1.5.2) lib/rack/cascade.rb:33:in block in call' rack (1.5.2) lib/rack/cascade.rb:24:ineach'
rack (1.5.2) lib/rack/cascade.rb:24:in `call'

Element.find('foo') or Element['foo'] finds only the first element on the page

So it is not possible to make all elements handle some event.
Same for default generated example:

  def all_links
    @all_links ||= element.find('a')
  end

  def say_hello_when_a_link_is_clicked
    all_links.on :click do |event|
      # Use prevent_default to stop default behavior (as you would do in jQuery)
      # event.prevent_default

      puts "Hello! (You just clicked on a link: #{event.current_target.text})"
    end
  end

does not work at all

opal specs not working

Hi there,

running rake opal:spec results in this:

WARNING: Cannot handle dynamic require -- rspec/core/configuration:
WARNING: Cannot handle dynamic require -- rspec/core/configuration:533
WARNING: Cannot handle dynamic require -- rspec/core/configuration:857
WARNING: Cannot handle dynamic require -- rspec/core/configuration:1095
WARNING: Cannot handle dynamic require -- rspec/core/configuration:1095
WARNING: Cannot handle dynamic require -- rspec/core:
WARNING: Cannot handle dynamic require -- rspec/core:200
WARNING: Cannot handle dynamic require -- rspec/core:200
ReferenceError: Can't find variable: jQuery

  http://localhost:9999/assets/opal-rspec20150107-17829-1hvhbdt.js:34806
  http://localhost:9999/assets/opal-rspec20150107-17829-1hvhbdt.js:43185
Specs timed out

this is the file I am trying to test:

require "fullcalendar"
require "events_new"

class CalendarsIndex
  def initialize(collection, selector='body')
    @collection = collection
    @element = Element.find(selector)
    render_full_calendar
    add_events
  end

  attr_reader :element, :collection

  def render_full_calendar
    fc_options = {
      header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,basicWeek,basicDay'},
      selectable: true,
      selectHelper: true,
      select: -> (starts, ends) { show_new_form(starts, ends) }
    }

    Element.expose :fullCalendar
    element.fullCalendar(fc_options.to_n)
  end

  def add_events
    element.fullCalendar('addEventSource', collection)
  end  

  def show_new_form(starts, ends)
    event_form = EventsNew.new(starts, ends)
  end
end

Document.ready? do
  events = Native(`window.gon.events`)
  CalendarsIndex.new(events, "#fullcalendar")
end

I found out that fullcalendar is causing the problem, so I removed the require statement from the top and required fullcalendar in ´app/assets/javascripts/application.js.rb`. Now I get this error:

WARNING: Cannot handle dynamic require -- rspec/core/configuration:
WARNING: Cannot handle dynamic require -- rspec/core/configuration:533
WARNING: Cannot handle dynamic require -- rspec/core/configuration:857
WARNING: Cannot handle dynamic require -- rspec/core/configuration:1095
WARNING: Cannot handle dynamic require -- rspec/core/configuration:1095
WARNING: Cannot handle dynamic require -- rspec/core:
WARNING: Cannot handle dynamic require -- rspec/core:200
WARNING: Cannot handle dynamic require -- rspec/core:200
NameError: uninitialized constant Object::Document

  http://localhost:9999/assets/opal-rspec20150107-18079-1qg5hua.js:2804
  http://localhost:9999/assets/opal-rspec20150107-18079-1qg5hua.js:1665
  http://localhost:9999/assets/opal-rspec20150107-18079-1qg5hua.js:51
  http://localhost:9999/assets/opal-rspec20150107-18079-1qg5hua.js:34790
  http://localhost:9999/assets/opal-rspec20150107-18079-1qg5hua.js:1114 in load
  http://localhost:9999/assets/opal-rspec20150107-18079-1qg5hua.js:1136 in require
  http://localhost:9999/assets/opal-rspec20150107-18079-1qg5hua.js:2856
  http://localhost:9999/assets/opal-rspec20150107-18079-1qg5hua.js:34982
  http://localhost:9999/assets/opal-rspec20150107-18079-1qg5hua.js:34984
Specs timed out

Any idea what I might be missing?

Slim filter

There's already a Haml filter, Slim filter would be awesome!

ujs with opal

Is it possible to use ujs with opal?
I required 'opal_ujs', but it's not working:
e.g.
= link_to "search_pages", search_pages_path, onClick: "puts 'hello world!'" <== not working
= link_to "search_pages", search_pages_path, onClick: "alert('hello world!')" <== everything ok

Source Maps Errors

Getting a ton of these on a new Rails 4.0 w/ opal-rails v. 0.6.1. Opal still runs though.

ActionController::RoutingError (No route matches [GET] "/__opal_source_maps__/test.js.map"):
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'

Make specs for opal-rspec configurable

@elia I have been trying to work out the best way for configuring where specs should be loaded from for the opal_spec route. What about a simple config option which the controller could use?

Standard config

# config/initializers/opal.rb
Rails.application.config.opal.rspec_path = Rails.root.join('app', 'assets', 'javascripts', 'spec')

Of course, the given path would already have to be in the sprockets/assets path.

Custom/additional config

My main use for this is that I now share quite a lot of lib code between opal and rails, and I want to run the specs on it both client and server side. So maybe an array would be a better option:

# config/initializers/opal.rb
Rails.application.config.opal.rspec_path # default path => "app/assets/javascripts/spec"
Rails.application.config.opal.rspec_path += Rails.root.join('spec', 'shared')

Again, any customization would require that given path to also be added to the sprockets pipeline.

Rails.application.config.assets.paths << Rails.root.join('app', 'shared')
Rails.application.config.assets.paths << Rails.root.join('spec', 'shared')

I can implement this @elia, was just looking for your thoughts on it.

Dependency problems

Bundler could not find compatible versions for gem "opal":
  In Gemfile:
    opal-rails (~> 0.7.0) ruby depends on
      opal-rspec (~> 0.4.0) ruby depends on
        opal (= 0.7.0) ruby

    opal (0.7.1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.