Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
as all we know bower has make all the things more easier to manage frontend files such as j-query,bootstrap and many others without downloading from the sources manualy. so,how about gems for rails? ruby on rails can require frontend and manage files like bower does.
I've just start to try using bower and newbie in front end development, I have a little bit knowledge about back end development especialy in ROR. I want to know how's bower works and what the advantage of it if we collaborate with ROR, we are currently developing web using ROR. we are usually install bootstrap and many others front end frameworks using gems, but we want to move and start using bower.
where is the different about bower and gems for rails? should I use bower to manage frontend files on rails development?
Well, the question is quite opinion-based, so I'll write my own as well. There are two main differences here. Firstly, bower as you laready mentioned is to handle frontend assets only - in that case anything installed with bower will not give any helper methods within your ruby code.
Example: font-awesome can be installed both ways, but gem will give you fa_icon view helper, which you won't have when you install it with bower.
On the other hand, not all assets are available in form of the gem and hence bower is giving you greater choise of stuff to install.
Personally, I prefer to use bower, unless I know there are some extreme advantages in using gem.
EDIT:
Apparently there is another alternative, which is a combination of gems and bower assets - rails-assets.org. It is a gem source wrapping bower components in a gem. So if you need to install angular, you can (apart from installing angular-rails gem) add this url as a source to your Gemfile and then install rails-resource-angular.
Since you already know what Bower is and how Rails works, I'm assuming you're asking what are the pros/cons of using one or the other for your front-end dependencies.
We can take something like Bootstrap as an example. The disadvantage to using a Rails gem, such as twitter-bootstrap-rails is that everytime the underlying asset (Bootstrap) is updated or changed, you must wait for the gem's authors to update the gem accordingly. If they are slow or the gem's development/maintenance is abandoned, then now you're stuck. The pro is that the authors have specifically adapted the asset to work with Rails and its implementation is usually well-documented and simple. The bootstrap gem for rails makes it pretty easy, you basically just delete sprockets directives and use SASS's import feature.
Bower, on the other hand, is not generally made with Rails in mind, and integrating whatever package my be a bit more involved. For example, if you use bower to get BS, just adding a SASS import (assuming you are grabbing the SASS version of BS) is not going to work because the SASS load path is not going to be set to look in your bower_components/bootstrap folder by default.
I think that if you want to incorporate bower in your Rails workflow, you really ought to take a look at the rails-assets gem. It makes the incorporation of bower much simpler. I would also take a look at this episode from GoRails about using rails-assets. The author, Chris Oliver, seems to have the opinion that this is the way to go.
Bower lets you upgrade/update web asset files like Bootstrap, jQuery etc. Bower is a program, while Gem is packaged code written for you that you can use in your Rails application.
If you want to compare Bower, you can try to compare it with Bundle. And the difference is that while Bower manages the version for web assets, Bundle is used to install, and update gems. For bundle to work successfully, you need to list the gems you want to install in your Gemfile.
For more information on Bower, you can read this article.
Related
In Ruby on Rails, which is better for CSS and Javascript dependencies?
To install them as Gem or as Bower dependency? Such as jQuery and or Bootstrap?
It is better to install CSS frameworks like Bootstrap as a Gem. Here is my favorite Bootstrap installation guide: https://launchschool.com/blog/integrating-rails-and-bootstrap-part-1
jQuery is already included in the default Rails setup.
This is very much a personal preference. There are schools of thought that go both ways.
I haven't looked in a long time, but oftentimes the gems are usually behind the current release of front-end libraries.
UI portability is greater if you keep the frontend/backend logic completely separate. If you wanted to switch to another backend tomorrow, how long would it take?
Using gems is faster, and you have a single tool (bundler) to handle the depedencies.
I have avoided providing my personal preference in favor of a neutral post.
AS my opinion ....
rather than gem/bower, you should download files and integrate to project
like for bootstrap download bootstrap files
To include Angular in rails, I can think of three basic methods:
Use gem 'angularjs-rails' (which no one seems to do).
Download Angular, add it to /vendor and manually include it.
Use Bower to require it and then manually include.
I suppose the advantage of Bower is just the dependency model but apart from that, why would I pick any one of these over any of the other options?
A good approach is to use Rails Assets.
From their website:
Rails Assets is the frictionless proxy between Bundler and Bower.
It automatically converts the packaged components into gems that are
easily droppable into your asset pipeline and stay up to date.
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.
I don't know if this is a ridiculous question however I am creating a website with RefineryCMS, Ruby, and Rails. I chose to speed up some things by using Twitter's Bootstrap via the less-rails-bootstrap gem. I do however need to customize some colors and such though I can't seem to locate any of the less files. I looked in the logs and see references to things such as 'twitter/bootstrap.css' though I'm not able to locate this or where the less files live.
Where are these things? Else, how to do you override the defaults?
When you complete with gem install and bundle install command , you should :
rails g bootstrap:install
This will insert some files in your app/assets dir , including bootstrap_and_overrides.css.less in app/assets/stylesheets.
You can learn more in this Railscast.
UPDATE: (Jan 25 2014): Valuable information form the comment of #Niels Abildgaard:
According to GitHub repository of less-rails-bootstrap gem, Rails generator command shoud be :
rails generate less_rails_bootstrap:custom_bootstrap
I have features I would like to be portable between my own Rails applications.
I wonder if I should create a gem or a plugin for each feature I want to be portable (sharable).
They are just for Rails (for now) because they include css, html, js and image files.
But I have been wondering, the things provided with plugins could be provided with gems too but not the opposite? So maybe it's better to learn how to create gems, because then you I don't have to learn how to create both gems and plugins? And gems seem to be more popular these days.
But then, from what I can understand one gem is shared between all rails app in the OS. So that means I can not customize it for each Rails app right? In that case, maybe creating a plugin is better cause it should be allowed to customize (editing css, js etc) each feature and to have it stored inside the Rails app itself and not in the OS level.
Some advices would be appreciated!
UPDATE:
So gem works great with css, html, js and image files? In a plugin I think you can have a MVC, your own models, views and controllers. Quoted from Rails guides "Storing models, views, controllers, helpers and even other plugins in your plugins". Is this possible too in a gem? Eg. I want to add a extension that gives me a nice Shopping cart (with own migrations, mvc, asset files) that will be hooked into the current Rails app. Is this possible as gem or only as plugin?
You're right that gems offer a little more than plugins. Versioning and dependencies on other gems being the main ones for me.
One gem needn't be shared across everything using ruby. You can install multiple versions of a single gem and specify in your environment.rb that a gem requires a specific version. E.g.
config.gem 'my-gem', :version => '1.2.3'
Also you can freeze gems into your rails application so that you know you are working with a specific version.
You might want to look at the jeweler gem which makes creating your own gems easier.
UPDATE
To include CSS, javascript etc I think you'll need to make an Rails engine which can then be bundled as a plugin or a gem. I've not done this but there's some coverage here and here.
The push with Rails 3 seems to be towards gems and away from plugins as a lot of support has been added to make gems work as well or better than plugins ever did. A well constructed gem is a great thing to have and share between different applications, and also reduces the amount of testing you will have to do since you can test the gem thoroughly before integration.
For extensions to Rails that use CSS, HTML and other assets, it might be that you need to build an engine to bundle this all up and allow it to fit neatly into an application.
As of Rails 4, plugins will no longer be supported.
Gems are the way forward.