Google Polymer with rails 4 - ruby-on-rails

I have build a Ruby on rails app.I want to use polymer with my ruby on rails app. Cam anybody suggest some good resources to learn polymer with rails ?
Is it efficient to use polymer with ruby on rails ?
Please also suggest other better options than polymer, if any ?

I am using bower, so I expect you already have bower setup on your system.
(brew install node && npm install bower)
Set up you gems
gem 'bower-rails'
gem 'emcee'
bundle install
rails g bower_rails:initialize
rails g emcee:install
Setup your bower file. Below is what mine looks like:
asset 'angular'
asset 'angular-route'
asset 'angular-resource'
asset 'angular-mocks'
asset 'webcomponentsjs'
asset 'polymer', github: 'Polymer/polymer'
asset 'polymer-core-elements', github: 'Polymer/core-elements'
asset 'polymer-paper-elements', github: 'Polymer/paper-elements'
asset 'platform'
# Voice Synthesis and Recognition
asset 'voice-elements'
Figure out the ones you need and delete the rest.
rake bower:install
To download the components but take note that the files will be saved in vendor/assets/bower_components and not vendor/assets/components expected by emcee. So you will need to make symbolic link.
Setup symbolic links
first remove components i.e. rm -rf vendor/assets/components
then set up the symlink i.e. ln -s vendor/assets/bower_components vendor/assets/components
Now setup your assets
# app/assets/components/application.html
*= require polymer/polymer
# app/assets/javascripts/application.js
//= require angular/angular
//= require angular-route/angular-route
//= require angular-resource/angular-resource
//= require webcomponentsjs/webcomponents
//= require platform/platform
(Please note that I only included all I am using for testing. As stated before, remove those you don't need).
Just some house cleaning you jump off to start trying things out. Include Bower components in rake assets path.
# config/application.rb
config.assets.paths << Rails.root.join("vendor","assets","bower_components")
And that is about it for setup. Jump on it and start using it.
Include html import tag in your layout file
= html_import_tag "application", "data-turbolinks-track" => true
And call a sample polymer in your view
# sample.html.haml
================
%paper-tabs{:selected => "0", :scrollable => ""}
%paper-tab Skills
%paper-tab Experiences
%paper-tab Education
Ref
http://angular-rails.com/bootstrap.html
http://www.akitaonrails.com/2014/06/29/usando-polymer-com-rails#.VHcoW2SsV81

On deep searching on Google. I have found that 'emcee' is the best to use polymer with rails.
following are the options :-
nevir/polymer-rails
alchapone/polymer-rails
ahuth/emcee
also check- http://joshhuckabee.com/getting-started-polymer-ruby-rails

I've built a bower package for dealing with rails forms with polymer. It my come in handy, especially if you're going with to be building any kind of ajax forms or using nested attributes.
https://github.com/hobberwickey/polymer-rails-forms
To answer your more general question though, I've been working with Polymer and both Rails / Sinatra for a while now and it's a great tool for building out the front end of your application without relying heavily on erb / haml or other server-side templating.

You can use polymer-rails and polymer-elements-rails.
https://github.com/alchapone/polymer-elements-rails
https://github.com/alchapone/polymer-rails

I was first trying to use web components with bower-rails and emcee.
But I noticed that bower-rails isn't necessary, you just need to configure .bowerrc and bower.json then run bower install.
Also, emcee is no longer active which forces you to use an older version of sprockets.
It turns out polymer-rails has the same functionality as emcee.
Here is my current setup
Gemfile
gem 'polymer-rails'
install and initialize
bundle install
rails g polymer:install
app/views/layouts/application.html.erb
<html>
<head>
...
<%= html_import_tag 'application'%>
...
Download the bower.json file from elements.polymer-project.org and put it in the root of your app then run the command
bower install
Now your components should be in vendor/assets/components. Find all the .html files to include in the manifest. Some of the .html files are not the same name as the directory so it is a little tedious.
app/assets/components/application.html.erb
//= require polymer/polymer
//= require paper-scroll-header-panel/paper-scroll-header-panel
//= require paper-toolbar/paper-toolbar
//= require paper-tabs/paper-tabs
//= require paper-drawer-panel/paper-drawer-panel
//= require paper-icon-button/paper-icon-button
//= require iron-icons/iron-icons
//= require paper-card/paper-card
//= require paper-button/paper-button
...
Now make sure sprockets loads the assets
config/initializers/assets.rb
Rails.application.config.assets.paths << Rails.root.join('vendor', 'assets', 'components', '*')
Here is an example of how to use a component
<paper-card heading="Paper Card Example" image="<%= image_path 'example.jpg' %>">
<div class="card-content">This is an example of cards with polymer</div>
<div class="card-actions">
<paper-button raised>OK</paper-button>
</div>
</paper-card>
You can use polymer-elements-rails which has a lot of the components already and you can skip the bower.json, bower install, and load assets steps. The only problem is I had a hard time finding out what the require paths were and some of the components were missing (like google-apis). Hope this helps anyone trying to use polymer with rails!

Related

FullCalendar Rails Assets

I am using the fullcalendar rails gem. I just downloaded the zip file for the calendar which contains the js and css files for the fullcalendar plugin. For all the tutorials on the fullcalendar gem I have not seen anyone doenload the zip file and use these files in their code (they use the CDN links but those don't appear to be working) where is the best folder to place these zipped assets into so that I can get the desired results on my calendar?
If you use the gem you do not need download any file.
You have to:
install gem
add in the application.js file the instruction //= require fullcalendar
And to can use it.
Read the README of the gem https://github.com/bokmann/fullcalendar-rails
Because you also need moment.js
Don't install from the zip file.
Follow the instructions at https://github.com/bokmann/fullcalendar-rails#installation for installation; you will have an easy time managing the dependencies.
The steps as specified there are:
Add gem 'fullcalendar-rails' to Gemfile
Add gem 'momentjs-rails' to Gemfile if you want to use 2.1.1.0 or higher version of fullcalendar-rails gem.
Run bundle install to install the gems.
Add //= require fullcalendar (and //= require moment) in application.js
Add *= require full calendar in application.css

Adding angular-datepicker to rails with bower-rails

Bower-rails looks like it should be turn key, and is for very popular libraries like angular and angular-resource, but how can I add another less popular, but bower enabled, library?
e.g. - https://github.com/g00fy-/angular-datepicker, https://angular-ui.github.io/angular-google-maps/#!/
To my Bowerfile I have added
asset 'angular-datepicker', github: "g00fy-/angular-datepicker"
and run rake bower:install. This installs the files to my vendor/assets/bower_components folder with the other libraries that do work (angular, angular-resource, etc).
But when I add the path to my application.js file, it claims not to be able to find the file.
//= require angular-datepicker
I've already added the path to the bower_components to my application.rb file
config.assets.paths << Rails.root.join('vendor', 'assets', 'bower_components')
How can I add these bower libraries to rails?
Seems like you have to //= require angular-datepicker/angular-datepicker
Please checkout https://github.com/rharriso/bower-rails/issues/37.

How to use typeahead.js as a Bower component with Rails 4

I'm trying to use typeahead.js in my Rails 4 app. I would like to use Bower to do this instead of using the gem. I have installed it in vendor/assets/components. I have also included the line below in config/application.rb.
config.assets.paths << Rails.root.join('vendor', 'assets', 'components')
I have searched extensively but cannot seem to figure out what exactly I should include in app/assets/javascripts/application.js to get Rails to recognize this. The directory typeahead.js is installed properly in vendor/assets/components. I have tried
//= require typeahead.js
as well a few other options but it's not working. I'm a relative newbie to Rails and am still trying to figure out how some of the things work under the hood. Any help is appreciated.
I would recommend that instead you use Rails Assets. Getting started is as simple as adding
source 'https://rails-assets.org' do
gem 'rails-assets-bootstrap' #example
gem 'rails-assets-angular' #example
gem 'rails-assets-leaflet' #example
end
to your Gemfile, replacing the examples with bower assets you need. Simply prefix a given Bower asset with "rails-assets-", and you're good to go.
Be sure to check out their website if you need any help getting started!
Your bower.json file should specify a "typeahead" dependency like this:
"vendor": {
"name": "bower-rails generated vendor assets",
"dependencies": {
"typeahead": "git#github.com:twitter/typeahead.js.git"
}
}
Run rake bower:install, it should place the library in vendor/assets/bower_components
Then in your application.js
//= require typeahead
I did not use typeahead.js after I posted my question, but need to use it now and got it to work. First of all I didn't need to add anything to config/application.rb since the vendor/assets directory is already included in the asset pipeline.
Bower installed the directory typeahead.js in the vendor/assets/components directory as I put
{
"directory": "vendor/assets/components"
}
in my .bowerrc file.
And in my app/assets/javascripts/application.js I include the full path to the typeahead.jquery.min.js file:
//= require typeahead.js/dist/typeahead.jquery.min.js
Everything works fine now.

Javascript files are not included from manifest

I am using Ruby On Rails and have ActiveAdmin on top of that on a windows machine. I donĀ“t think ActiveAdmin has anything to do with it, but I am just saying.
The Javascript file that is included via HTML is /assets/active_admin.js and the manifest file itself looks like this:
//= require active_admin/base
//= require just_datetime_picker/nested_form_workaround
When I run my Rails application and open http://localhost:3000/assets/active_admin.js, the log tells me, that active_admin.js was compiled, but the file still looks just like the manifest just with an added semicolon at the end:
//= require active_admin/base
//= require just_datetime_picker/nested_form_workaround
;
Why is no Javascript code included?
When I try to run the precompile pipeline manually via bundle exec rake assets:precompile RAILS_ENV=development, it actually creates all the CSS files, but the JS files still look like the one described above.
Since I am operating on a windows machine, I do not have the gem 'therubyracer' installed, but NodeJS. Also the bundle install works fine and I also do not get any errors.
Are you using Ruby v2.0? Try downgrading to 1.9.
Symptom: The JavaScript manifest file does not compile, no errors are shown; however, a mysterious semi-colon ; appears at the end of the file after "preprocessing" the assets.
Solution: Downgrade to Ruby v1.9.3!
See two other posts on the same topic.
My Rails Javascript manifest file neither compiles nor include any required files
Javascript manifest file "application.js" is not processed
What a sorry mess it is to be programming in 2013!

Rails Asset Pipeline: What happens when we do require in application.js

I am trying to contribute to the opensource project jquery-datatables-rails. But it puzzles me as how it works. I have basic understanding of how to create gem. It seems like the project just simplifies the path references but nothing more. The four steps listed to install are
Add to the assets group in your Gemfile:
gem 'jquery-datatables-rails'
Install the gem:
bundle install
Add the JavaScript to application.js:
//= require dataTables/jquery.dataTables
Add the stylesheets to application.css:
*= require dataTables/jquery.dataTables
But I don't understand how Rails knows where to find dataTables folder and why we use jquery.dataTables to reference the file/class. The code is easy to understand, but the file organization confuses me so much. Is there any writing on topics related to my confusion?
Thanks.
Update:
Just found this great documentation on Rails Engine: http://edgeguides.rubyonrails.org/engines.html
By default Rails asset pipeline searches for the files in app/assets/, lib/assets, and vendor/assets of the Rails application. But this gem is a Rails engine, and it adds path of its own assets to default assets paths. It has its own vendor/assets.

Resources