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.
Related
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.
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']);
}
I have one Rails application and all files are in erb format. Is there any quick way to convert whole application's erb file to haml.. without any conflict.
And also would like to know for the Reverse..
Thanks in advance. :)
For erb-to-haml
You can use from the command line html2haml
html2haml your_erb_file new_haml_file
If you want to convert all your files in one go, look at this article : http://shifteleven.com/articles/2008/06/08/converting-erb-to-haml-snippet
erb2haml gem will do the trick.. have a look to https://github.com/dhl/erb2haml
For haml-to-erb
I recommend you HAML2ERB service . It's really cool and generates valid ERB/HTML code! Tested on big HAML views (over 800 lines of markup) from the real production app. Project active :)
have a look to this also http://makandracards.com/makandra/544-convert-haml-to-erb
I implemented twitter-bootstrap 3 in my application with bootstrap-sass (https://github.com/thomas-mcdonald/bootstrap-sass) but I was not able to use a theme from http://bootswatch.com/ with this method (because it doesn't provide the css files).
Then, I finally managed to install the theme from bootswatch by removing the gem, and using only the CSS files downloaded directly from twitter-bootstrap website. (I followed this tutorial : http://www.erikminkel.com/2013/09/01/twitter-bootstrap-3-in-a-rails-4-application/ ). It worked great so far, but I came across this article http://ruby.bvision.com/blog/please-stop-embedding-bootstrap-classes-in-your-html and decided to improve my code by using the bootstrap mixins.
I found this approach very interesting and I would like to use it in my project :
article {
.makeRow();
section.main {
.makeColumn(10);
}
aside {
.makeColumn(2);
}
}
Considering what I said about using a theme without a gem, how could I use LESS or SASS mixins ?
EDIT
Is there no other way than manually compile LESS code ? (as Bass Jobsen suggested) because it's really not convenient..
You will have to compile your less code. Use the less files of your Twitter's Bootstrap 3 download.
Add your less code shown above to bootstraps.less, use a less compiler (some examples http://lesscss.org/#usage or https://github.com/twitter/recess) to compile this to bootstrap.css and copy this file to your vendor/assets/ directory.
Also see: How can I create multiple rows using semantic markup in Bootstrap 3? and notice makeRow and makeColumn in your example code are not valid mixins of the current Bootstrap release.
update
Your question was SASS or LESS, so i answerd LESS. I expect you could do the same with SASS. From reading this: https://github.com/thomas-mcdonald/bootstrap-sass#sass it think you should import bootstrap-and-overrides.css.scss into app/assets/stylesheets/bootstrap-custom.scss
I have situation where I generate stylesheets based on information in the database. This means I have a views/users/show.css.erb which contains code like:
a {
color: <%= #user.favorite_color %>;
}
Fair enough, works perfectly with a respond_to { |f| f.css } in my controller. However now I would like to use some of SASS's sassy functions like lighten, darken etc. in my CSS, so I would like to have SASS parse my view. However I cannot figure out how to register SASS as a template handler in my views without too much ugly hacks. I just don't get how it works. Has anyone ever tried this?
I have tried including SASS's Functions module but to no avail.
I was planning of doing the same for dynamic user themes some time ago. Back then, I found a mailing list answer to a similar question to yours, by Nathan Weizenbaum (author of SASS) which got me back on the straight and narrow:
On Mar 18, 11:46 am, Nathan Weizenbaum wrote:
There's a reason Sass doesn't allow dynamic access to Rails code - see
the Sass section ofhttp://haml.hamptoncatlin.com/docs/rdoc/files/FAQ.htmlfor
a brief discussion. Sass is also fairly slow, since it's only compiled once,
so this is probably far too slow for a production environment.
The link is not working anymore, but the SASS FAQ has only one section called Can I use a variable from my controller in my Sass file? which answer the dynamic question:
If you really, really need to compile Sass on each request, first make sure you have adequate caching set up. Then you can use Sass::Engine to render the code, using the :custom option to pass in data that can be accessed from your Sass functions.
So in general, SASS is not designed for dynamic css generation on each request. Rather it is intended to be used as a pre compilation solution to generate static css files.