Is there a way to use polymer-dart with Ruby on rails? - ruby-on-rails

What's the easiest way to use polymer-dart with ruby on rails or on other frame works currently?
Using vulcanize and polymer-rails gem and some hack, I'm trying to take care of html imports.
But I'm having hard time resolving file urls.
GET http://localhost:3000/assets/paper_ripple_behavior.dart
What is happening seems some polymer scripts trying to fetch dart scripts using url relative to the location of self or expecting the dart server. Since it's vulcanized, and processed, and on RoR, it never finds what it wants(Maybe I should just clone the packages and rewrite all the links, easier).
Or if there are no duplicate names, I could make all files available at localhost:3000/assets/
Some funcitons also seems to be doing something like the below:
Exception: Uncaught Error: Unable to find library at http://localhost:3000/assets/shadow.dart.
Stack Trace:
#0 InitializationCrawler.InitializationCrawler (package:initialize/src/mirror_loader.dart:43:31)
#1 loadInitializers (package:initialize/src/mirror_loader.dart:16:14)
#2 run (package:initialize/initialize.dart:27:24)
#3 run.<run_async_body> (package:web_components/src/mirror_initializer.dart:26:11)
#4 Future.Future.microtask.<anonymous closure> (dart:async/future.dart:144)
#5 _microtaskLoop (dart:async/schedule_microtask.dart:43)
#6 _microtaskLoopEntry (dart:async/schedule_microtask.dart:52)
#7 _ScheduleImmediateHelper._handleMutation (dart:html:42567)
undefined:1 undefined
At this point, I'm considering to run RoR and dart on different ports and redirecting some request to dart Server, though I don't know if it's practical.
How would you let dart play with RoR?
Edit
After few days of tinkering, I came to a conclusion that Günter Zöchbauer is probably right for the time being.
While
rewriting embedded html import hrefs in the html files found under the packages directory
tinkering with html import urls (element.href found in initialize method in the file "web_components-0.12.0+4/lib/html_import_annotation.dart" was taking care of this)
seem to solve some of the html import url issues, embedded scripts are still attempting to load dart file directly under assets: "http://localhost:3000/assets/paper_button.dart ".
"initPolymer()" also attempts to load dependency files in a similar fashion, at least in "/components/packages/initialize/src/mirror_loader.dart".
These are discordant behavior with the other dart libraries, so hopefully will change in the future versions.
As things stand now, understanding and finding loader scripts is too much for me, although I believe more skilled programmers would find it a breeze.

For deployment you need to run pub build in the Dart package and serve the result (build/web) from your server. You can't serve Dart source.
If you use https://pub.dartlang.org/packages/dart_to_js_script_rewriter there shouldn't be any *.dart files be fetched.
For development use a server that redirects requests for Dart resources to pub serve.

Yay, I've found a way.
Purpose
Use dart-polymer tags and run an application written in dart on rails server.
Method
Through making necessary resources available at RailsAppRoot/pub and editing views/layout/application.html.erb
How to do
Create a simple application on webstorm or intellij that make use of polymer tags you would like to use in your rails application.
Modify the links in your app to fetch resources relative to the url root.
Copy the contents of the web folder, dart_app_project_name/build/web, into
rails_app_root/pub
You might also need to copy the lib folder
Copy the header and the scripts found in your entry point html file of your dart app into "application.html.erb" or use render partial.
Use the polymer tags anywhere.
For the step 2 you can use something like this:
require 'pry'
require 'pry-doc'
require 'pry-byebug'
require 'nokogiri'
def prepend_slash file
doc = Nokogiri::HTML(File.open(file).read())
doc.search("link[rel='import']").each do |node|
href = node['href']
next if href.nil?
node['href'] ="/"+ href if href[0] != "/"
end
doc.search("script").each do |n|
src = n['src']
next if src.nil?
n['src'] = "/"+src if src[0] != "/"
end
File.open(file,'w').write doc.to_html
end
target = 'path_to_your_app/app/views/layouts/_polymer_body.html.erb'
prepend_slash(target)
p "processed"
Your app and rails server can communicate using json or simply by embedding data in html document, so in theory dart-polymer and rails now can have fun together.
If Turbolinks is giving you troubles, try something like:
import 'dart:html';
import 'dart:js' as js;
void onReady(var callback){
var d = js.context.callMethod(r'$',[document]);
d.callMethod('on',['page:change']);
}

Related

Add 'library' directive to dart code generated using protoc

Can someone tell me how to get protoc to generate dart files with a leading library directive?
I'm using the dart-protoc-plugin (v0.10.2) to generate my dart, c++, c#, js and java models from proto files. I was under the impression there was no way to get protoc to add a 'library' directive to the generated dart files, until I noticed the directive appearing in another project (see date.pb.dart).
If I take the same file (date.proto) I cannot get protoc to generate a dart file containing a 'library' directive.
In short: I want to take a .proto file with the following content
syntax = "proto3";
package another.proj.nspace;
message MyObj {
...
}
and produce a .dart file with a leading 'library' directive similar to the following snippet
///
// Generated code. Do not modify.
///
// ignore_for_file: non_constant_identifier_names,library_prefixes
library another.proj.nspace;
...
NOTE: I don't care about the actual value of the directive since I can restructure my code to get the desired result. I just need a way for protoc to add the library directive...
The basic command I'm using to generate the dart files is
protoc --proto_path=./ --dart_out="./" ./another/proj/nspace/date.proto
Unfortunately the dart-protoc-plugin's README isn't very helpful and I had to go through the source to find out which options are available; and currently it seems like the only dart-specific option is related to grpc.
I've tried options from the other languages (e.g. 'library', and 'basepath') without any success.
It would simplify my workflow quite a bit if this is possible, but I'm starting to get the impression that the library directive in date.pb.dart is added after the code was generated...
After asking around a little bit, it seems that the library directive was removed from the protoc plugin at some stage (see pull request), thus it is no longer supported.

Rails: Accessing JS module methods from files served by webpacker

Context
I try to move assets in our application to webpack using Webpacker gem. Application is very big, so I need to do it partially.
What did so far...
I successfully managed to call the script using javascript_pack_tag
I export a super simple module:
# javascript/src/javascript/test.js'
const Direction = {
log_to_console: function(){
console.log('test');
}
};
export default Direction;
Then import it in the application entry point
# javascript/packs/application.js
import Test from '../src/javascript/test.js'
Test.log_to_console();
Finally rendering it in the layout:
# app/views/application.slim
= javascript_include_tag 'application'
The result is: "Test" string visible in the browser console.
The problem
Currently in the whole application we use Modules in views like this:
# app/views/assets/javascripts/test.coffee
log_to_console = ->
console.log('test');
#Test = { log_to_console }
# app/views/some/template.slim
javascript:
Test.log_to_console()
But after moving module to webpack I can't access the Test module anymore.
So my question is:
How to configure webpacker gem OR refactor the code above to make the log_to_console() method available in views/browser inspector?
Ideally it would be if we could also access them in old javascript files compiled by sprockets.
I figured out that solution for now. May be helpful for anyone that encounters that problem.
If anyone finds better solution, I would be glad to see it here ;).
For now all our modules/libraries that are needed to be visible in views/other coffee files, I just set as globally available via the window object.
import Foo from '../src/javascript/foo.js
window.Foo = Foo
I know it's ugly anti pattern, but works well as temporary solution until we update our scripts behave more like independent packs.

ngAnimate in Rails, I just want to hook it up

why is it always so hard to hook things up to AngularJS?!?!?!
here's the error I get for anything new I try to install to Angularjs:
Error: [$injector:unpr] http://errors.angularjs.org/1.2.8/$injector/unpr?p0=%24%24asyncCallbackProvider%20%3C-%20%24%24asyncCallback%20%3C-%20%24animate%20%3C-%20%24compile
Everything works fine when I don't include ngAnimate in:
WriterSarah = angular.module("WriterSarah", ["restangular", "ui.router", "ngAnimate" ])
I put my angular-animate.min.js file in the SAME place that I do all my other Angularjs files, and I required it in my Application.js file in the SAME way that I did for restangular and ui.router
I restarted my server a bunch of times as well. what am I missing here?
The problem is likely that the Angular version does not match to the angular-animate version. Check to see if the angular versions are the same between both files.

how does the gem backbone-on-rails generate its templates

I'm trying to understand how the gem backbone-on-rails works under the hood.
My problem right now is that I don't know who is in charge of generating the code for the templates it provides.
After installing and setting it up (I'm using the plain js route, not coffeescript, but the question is the same), if I write a template file in /app/assets/templates/hello.jst, it gets "somehow" translated to the following javascript inside application.js:
(function() {
this.JST || (this.JST = {});
this.JST["hello"] = function(obj){ <ugly js here> };
);
But who actually does generate that code? I've browsed the sourcecode of backbone-on-rails, and could not find anything that pointed to template compilation. Is the asset pipeline capable of doing that out of the box?
Ok, I think I found my answer.
The functionality is provided by sprokets, there is a good explanation on its readme.
Will close this in two days.

Evergreen + jasmine-jquery

I want to make use of the new json fixtures ability of jasmine-jquery, but can't get it to work. I have a Rails 3.2 app with evergreen for JS testing. As the readme of https://github.com/velesin/jasmine-jquery said, putting the jasmine-jquery.js inside the spec/javascripts/helpers dir would load jasmine-jquery automatically.
I put jasmine-jquery (and other libraries like jasmine-ajax) into
spec/javascripts/helpers dir (so they are automatically loaded) and
fixtures into spec/javascripts/fixtures dir.
But it doesn't. I tried to load the file inside the spec_helper.js, but the require only points to the public dir.
I also found this answer: https://groups.google.com/forum/?fromgroups=#!topic/ruby-evergreen/0Tma5RKqZB8
But the solutions aren't working in my case.
So, where can I tell evergreen / jasmine to load the jasmine-jquery.js file, so that I can use the loadFixtures methods...?

Resources