Using underscore.js with rails 3 - ruby-on-rails-3

I've been looking at underscore.js and wanted to add it to my rails project. I'm already using jQuery. Is there a best practice when it comes to using these two libraries together? Also, how do you deal with jQuery objects with underscore? Do you use jquery to select a bunch of elements, then run them through underscore?

You can either manually include the underscore js file in your assets, or you can use this gem:
https://github.com/rweng/underscore-rails
Underscore works well with Javascript collections and objects of any kind, so the functions do work well with jQuery objects.

I suggest you to use it without underscore-rails gem. It is very useless.
It produce nothing but new dependency for the project.
There is no reason to scare using the underscore.js directly.

Related

What is lodash library, is it required for QuillJS?

I see this library included, and not much of an explanation for what it is doing. Is it required to use the QuillJS library, and if so, what does it do?
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js"></script>
It is referenced in an external js file with
_ = Quill.require('lodash');
Isn't it implied it is already included with the script tags above?
From the Lodash website
Lodash makes JavaScript easier by taking the hassle out of working
with arrays, numbers, objects, strings, etc. Lodash’s modular methods
are great for:
Iterating arrays, objects, & strings,
Manipulating & testing values
Creating composite functions
The module provides very basic and necessary functionality which QuillJS presumably uses.
Lodash is a very useful library to have around. It has a lot of different methods built into it for working very efficiently with arrays of data, or objects or just about anything you can possibly imagine in the world of JavaScript.
https://lodash.com/docs
I have not seen anything in the Quilljs documentation that says Lodash is required or any steps installing Lodash beforehand.
https://quilljs.com/docs/quickstart/

Laravel 4 and dojo toolkit AMD implementation how to?

is anyone ever tried implementing the dojo toolkit AMD with laravel 4, or could anyone please point me to a simple sample.
just a simple AMD implemetation on laravel?
What asset manager or the default is ok. how to use it with dojo?
Please help. thanks
For 1. I suggest you may try this Laravel 4 bootstrap suite it gives you RequireJS implementation out of the box.
For 2. You can use dojo with any asset manager you want, or even without it (although it is not a good way) - just by putting its .js files in your /public directory and including them as you do in usual html from inside your view templates. If you are using Blade templates make sure the template syntax is not colliding with your js syntax. If it is, then use #include of .php file with your js code section in your .blade.php view template.
Asset manager gives you a more elegant and correct way of doing the same thing. It maybe extremely useful if you are dealing with LESS or Coffee things to be compiled into regular JS and styles.
If you want advanced asset manager I would suggest your to look at /CodeSleeve/asset-pipeline on github - it's one of many asset managers for Laravel, but one the few keeping alive (take a look at basset or laravel-grunt options on github for instance).
Asset Pipeline makes a good job making asset management similar to the one in Rails. Here is an article on how and why to use it: http://culttt.com/2013/11/04/add-asset-pipeline-laravel-4/

Safe templates (Ruby on Rails)

Is there any plugin allowing user to create there own templates? So I use smth like
Templates.find(5).render(:val1 => val1, :val2 => val2)
There is a good plugin named liquid however it doesn't seem to be safe (user can drop database and so on).
Thank you.
Liquid is a very popular templeting system and is considered safe. In fact it was one of the design goals. From the documentation of liquid:
Liquid is a template engine which was written with very specific requirements:
It has to have beautiful and simple markup. Template engines which don't produce good looking markup are no fun to use.
It needs to be non evaling and secure. Liquid templates are made so that users can edit them. You don't want to run code on your server which your users wrote.
It has to be stateless. Compile and render steps have to be seperate so that the expensive parsing and compiling can be done once and later on you can just render it passing in a hash with local variables and objects.
The locomotive CMS use a gem called liquid that claims to do that. Check it here http://rubygems.org/gems/liquid.

Compiling Sass with custom variables per request under Rails 3.1

In a Rails 3.1 app, one controller needs to have all its views compile whatever Sass stylesheets they might need per request using a set of custom variables. Ideally, the compilation must happen via the asset pipeline so that content-based asset names (ones that include an MD5 hash of the content) are generated. It is important for the solution to use pure Sass capabilities as opposed to resorting to, for example, ERB processing of Sass stylesheets.
From the research I've done here and elsewhere, the following seems like a possible approach:
Set up variable access
Create some type of variable accessor bridge using custom Sass functions, e.g., as described by Konstantin Haase here (gist). This seems pretty easy to do.
Configure all variable access via a Sass partial, e.g., in _base.sass which is the Compass way. The partial can use the custom functions defined above. Also easy.
Capture all asset references
Decorate the asset_path method of the view object. I have this working well.
Resolve references using a custom subclass of Sprockets::Environment. This is also working well.
Force asset recompilation, regardless of file modification times
I have not found a good solution for this yet.
I've seen examples of kicking off Sass processing manually by instantiating a new Sass::Engine and passing custom data that will be available in Sass::Script::Functions::EvaluationContext. The problem with this approach is that I'd have to manage file naming and paths myself and I'd always run the risk of possible deviation from what Sprockets does.
I wasn't able to find any examples of forcing Sprockets processing on a per-request basis, regardless of file mod times, that also allows for custom variable passing.
I'd appreciate comments on the general approach as well as any specific pointers/suggestions on how to best handle (3).
Sim.
It is possible. Look here SASS: Set variable at compile time
I wrote a solution to address it, I'll post soon and push it here, in case you or someone else still need it.
SASS is designed to be pre-compiled to CSS. Having Sprockets do this for every request for a view on a per request basis is not going to perform very well. Every request is going to have to wait for the compilation to be done, and it is not fast (from a serving-pages point of view).
The MD5 generation is within Sprockets, so if you are changing custom variables you are going to have to force a compilation on every single request to make sure that changes are seen because the view is (probably) not going to know.
It sounds as though this is not really in the sweet-spot of the asset-pipeline, and you should look at doing something more optimised for truly dynamic CSS.
Sorry. :-)

Which minified processing js should I use for runtime

http://processingjs.org/content/download/processing-js-1.2.1/processing-1.2.1.min.js
http://processingjs.org/content/download/processing-js-1.2.1/processing-1.2.1-api.min.js
Just observed that 2 javascript files are shipped in the distribution, what should I use for runtime functionality?
You could use either one, however the minified version is optimized for production because it saves bandwidth. If you plan on just using canned functionality from the script, the http://processingjs.org/content/download/processing-js-1.2.1/processing-1.2.1.min.js will do fine. However, if you wish to have the ability to change the code in the future, the api is what you want.
Jeffrey Kevin Pry
You want to use the non -api version. Looks like they added a seperate build which only includes the js API itself -- it doesn't include the parser which translates from processing into js.