Bundler loaded gem order - ruby-on-rails

I'm working on a small gem that will extend the paperclips has_attached_file method. I need this gem to be loaded after paperclip.
Is there a way to know the order in which the gems are loaded? And is there a way to modify that order?
Thanks a lot,
NHI

In your gemspec require the other gem then it should load before yours.
See the docs for more information.

i guess this might help give it a try.
gem 'New-Gem','some-version', :require => 'paperclip'

I had to add a Railtie to make this work. Paperclip was loading before my gem but wasn't include until much later...

Related

Why do I need to explicitly include a gem when it's already a dependency of another gem I'm using?

I have created a gem/Rails engine (let's call it my_rails_gem) that depends on another gem; specifically, the composite_primary_keys gem (some of the models need it). So in the .gemspec file, I have
Gem::Specification.new do |s|
#...
s.add_dependency 'composite_primary_keys'
The problem is that when I include the my_rails_gem in another Rails project's Gemfile and try to use the models, I get errors about composite_primary_keys' functionality. I must also explicitly add gem 'composite_primary_keys' to the app's Gemfile for it to function correctly.
Why is this? Isn't this the whole point of Bundle and gem dependencies? I want to take the burden off the developer using my_rails_gem to remember to have to include composite_primary_keys, but this is specifically preventing that. Am I doing something wrong, or are my expectations/understanding wrong?
I believe the reason that the dependent gem is not automatically required is so that the user of your gem (you) have the option of requiring the dependent gem. There are reasons for that, but it's a bit of a longer conversation.
If you want the dependent gem to be required automatically when you include your gem, then in your my_rails_gem.rb file (in your lib directory) you can do:
require 'composite_primary_keys'
I believe that ought to do the trick for you. At least, that's how I do it.
Also, if the dependent gem has stylesheet and javascript assets that you want to include (I suspect composite_primary_keys does not), you'll need to add the appropriate directives to your my_rails_gem.js and my_rails_gem.sass (or whatever templating engine you use) files.

require 'tire' gem not being mentioned in a model class but usage of it still works in rails app

Im reviewing code of a rails application I inherited and I'm no expert in rails. In the gemfile for the application there is: gem 'tire'. Great, but I expect to use require 'tire' in models or controllers to be able to use the libraries this gem provides.
According to the documentation of a particular gem it says it needs to be included as a mixin in a model class.
include Tire::Model::Search
include Tire::Model::Callbacks
Or the documentation mentions to use require 'tire' to the model class where you plan to use the tire gem.
But in the project I don't see any of these require/includes in the models. The models are just using the api without requiring/including like so:
ret = Tire.index "icrd" do
import [icrdObj]
refresh
end
How does this work? Where should I look in the rails project to get an understanding of why require or include is not needed? Is there some kind of autoloading going on?
Bundler has a lot of nice methods. Bundler.setup ensures you can require gems easily in your app.
Bundler.require(:default, :production) for example (it accepts group names) requires all gems in the gemfile automatically by performing require 'tire', require 'rails' and such, for you.
You can skip auto-require for some gems by adding require: false near your gem row in gemfile, like
gem 'rails', require: false
Check Bundler.require documentation (which is placed under groups)
When a gem is included in a Gemfile, the associated require .. statement is automatically called.
See http://www.justinweiss.com/blog/2014/10/13/how-does-rails-handle-gems/ for an explanation of how rails handles gems.
From that blogpost:
So, Bundler will look in your Gemfile for gems belonging to each of
those groups, and call require on each of the gems it finds. If you
have a gem nokogiri, it’ll call require "nokogiri" for you. And that’s
why your gems usually just work in Rails, without any extra code on
your part.

Bootstrap gem in Rails

I'm going to create a new Rails project, and as I know I should use gem 'bootstrap-sass', it's not a problem for me. But I have one question - why I should use gem instead of just adding some files (CSS and JS) directly to index.html? Can you explain me? Thanks in advance!
Use gem or source files don't make any big difference.
But, when you use gem it's easier to update... you need just update the gem and it's done! And when you use the source files, you must download the new version and replace the old files. I think it's the big deal when using gem: easer to update.

How to use ruby libraries in rails?

I just installed a ruby gem
gem install anemone
But I cannot hit off to use it directly in my rails application, this is the first time I'm using an external ruby library so it will be very helpful if you can give me an insight into it rather than just a solution
Cheers
In your Gemfile add:
gem 'anemone'
This way Rails will load the library when it starts, and you can then use it. You don't even have to use the require keyword.
You must put used libraries into Gemfile. Everything about gemfiles is available on Bundler page.
There's an official Rails guide about this topic.
You also need to use 'require' and the name of the library on top of the file where you want to use the library. For example if you are using nokogiri you would need to add the line require 'nokogiri'

Creating my own PaperClip GEM? for Heroku?

based on the needs of my app, I need to make a modification to paperclip, it's only one line but it makes all the difference for my apps needs.
In my Rails 3 GEM FILE I have the following:
gem 'paperclip', '2.3.5'
With github, how does one go about making there own paperclip GEM? So I can use it on Heroku?
Something like
gem 'paperclip', '2.3.5' -> my version?
Would love to hear how this is done. How this is maintained as paperclip is updated. and how I use this locally and on Heroku.
Thanks
Here's the github page on 'Forking'. http://help.github.com/forking/
You must fork the repo on github. Then, instead of passing a version in the Gemfile, pass the following:
gem 'paperclip', :git=>'git://github.com/username/repo.git'
Why are you creating a gem for this? You can monkey patch the gem. This will allow you to use the standard version. What is the change you need? What is the link to your gem, may be there is an easier solution.

Resources