How to use googlecharts library in rails? - ruby-on-rails

I want to use googlecharts in my rails App, and I decided use it directly without using wrapper library.
Currently I download jsapi file from https://www.google.com/jsapi and rename it to jsapi.js, then move the file to vendor/javascripts/google/jsqpi.js.
Then require the file in application.js.
Now I can use the library I want only to know if this is a right way to use the third party library in rails.
Is there better way than this way?

That is perfectly acceptable. vendor/assets/javascripts is the correct location for third-party javascript files.
Sometimes you can find gems that include third-party javascript libraries and integrate them into the asset pipeline for you (e.g. jquery-ui-rails gem). This is nice because it makes you not responsible for handling the actual files and is easier on your version control software (a single line in your Gemfile vs entire files). You can also update the files with bundler. However, this tends to only be practical for really popular libraries. Sometimes you can find gems for the javascript library you are looking for, but the gem hasn't been updated in ages. Of course, you could always roll your own gem that includes the libraries you want.

Have you taken a look at googlecharts gem
its full of examples

Related

How to add JavaScript framework in rails app without adding gems?

I just want to know how to include frameworks like angularjs, polymer js, bootstrap.... Into rails app without adding corresponding gems. Reason is the gems that are available are changing and some of them are given up. So instead of gem I want to download the library files given by the vendor directly. How to add them into rails app?
You can download the framework source files/folders, add them to your vendor folder and require those. Thats essentially what those gems do. If you're looking for a more advanced approach you can look into setting up a package manager like gulp and use the direct npm packages

Source maps in Ruby on Rails through sprockets

I'd like to add source map support on a rails 3.2 application I am working on. As far as I know, generating source maps is not supported by Sprockets and from its github page it looks like the feature is planned for 4.0. I am working with Sprockets 2.2 and I think monkey patching is the only way to go. The module Processing under the main Sprockets module gives access to the js_compressor function which can be patched to generate source map for a single file. But, I don't know how to add this when the JS files combine. I am using Uglifier 2.4 as the compressor.
The project has a mixture of CoffeeScript, JS and EJS files. So, I think this is how sprockets would be compiling them together. First, it would convert Coffeescript and EJS to JS, then use the js_compressor to compress individual files and later concatenate them in groups. Now, as the source map for multiple files combined to the same file is a single file. So, I will need to change the compilation process somewhat and make the js_compressor run over the files, once the concatenation is finished. So, can anyone help out with this? Even explaining the sprockets compilation process and the modules used and functions involved would be of great help. I don't care about making source map files for the CoffeeScript code at present. Even mapping to their converted JS files would do.
Also, would like to add if there is some gem which can help with this it would be most welcome.
Rails 4 does not have source maps either.
As far as I know, and as of today, this will only be part of rails 5.
A really nice approach to solve this right now is implemented in discourse by #SamSaffron and explained here:
https://github.com/discourse/discourse/blob/master/lib/tasks/assets.rake
The gist, add a "before" task to the sprockets precompile process, and hack into the compilation process to generate the sourcemapped files and directives.
The nice thing in this approach is that you don't lose stuff from files that are both js and erb (*.js.erb) which is something quite common in rails.
I think that patching the whole sprockets pipeline is a bit of an abuse and risky.

Rails 4 - Adding JS assets via gem

I am using a gem that somebody else wrote to serve the fabric javascript library. The gem is using an old version of the library (1.3) and I'd like to be using 1.4 (the latest version). I haven't found any other gems using this version. My question is, is this the best way to load assets, or is there a more preferred method? And, if so, how would I go about building this gem with the latest version of this library?
In my opinion, it is good to do so in most cases.
In your situation, depending on how much time you have, you may want to do one of the following:
1. Contribute to the gem
If the gem is open source, you may fork it, update to the newest version, and do a pull request.
By this way you also give contribution to the rails whole and the others who are facing the same problem as well.
Downside is this takes time. You have to wait for the author to accept the pull request and wait for the next version of the gem. But you can point your Gemfile to use your forked version until the new version is out ;)
2. Write your own gem
Writing a gem for rails providing assets is actually not difficult. You may follow other existing gem's structure and should be easy to understand.
A good example is https://github.com/rails/jquery-rails
Downside is you have to maintain the gem. Otherwise when fabric 1.5 is out, another one would ask the same question as yours again.
3. Put the assets in your vendor directory
Rails project by default do have a vendor directory. It's ok to put external assets here as well.
The above are my preferred way to manage external assets.

Best practice for including vender assets in Rails 4 Application

I can't find a good resource for explaining how to include a more complicated 3rd party package that includes js, css, and other types of assets within the same plugin. For example, the plupload plugin has a lot of different assets spread out in multiple folders. I have put it in the vendor/assets/plupload/ folder and then require the tree in my manifest file, but then it refers to other other files with a relative pathname that works in development, but the path changes in production. I can then change the references to use asset_path, but then I am modifying the vendor code which just seems wrong.
I know there is a gem out there for the plupload library, I am just using it as a case study to try and understand the best practice for including a more complicated 3rd party library than what the Rails Guides show.
Thanks!
You can place the contents related to any plugin or library in a single folder public folder. This will make all your assets available and there should not be any issue.

What's the best way to require source files when writing a gem

I'm converting the models folder of a rails app into a gem so more rails app can use the same domain model layer.
In the initial rails app, the loading of all model files is handled by activesupport so there no require statement everywhere. But in the gem version, it has to be done manually. I had a look at the code of popular gems such as rspec, factory_girl and state_machine and it looks like they all require all necessary source files in one file, usually named after the project.
The downside of this approach is that you need to maintain one file listing all the others and that seems a bit clumsy. And even though I have hit that problem yet, I can foresee cirular dependency issues.
Another way would be to have each source file requiring the files it needs. That would work in the standalone gem as well as in the rails app. But I haven't seen examples of gems using that technique so I'm wondering if there is a downside I'm not seeing?
thanks
You're talking about model files, if so, simply adopt the same structure as a standard Rails app and make your gem inherit from Engine. Everything will be included painlessly.

Resources