Skip to content
Advertisement

Rails gem with javascript dependency

I’m writing a rails gem which requires angular, so I added it to my gem’s dependencies:

s.add_dependency "angularjs-rails", "~> 1.6.2"

Now, how should I include angular ? I tried to add it directly in my gem’s main js script but sprockets fails with this kind of error:

couldn't find file 'angular' with type 'application/javascript'
Checked in these paths:
  ...
  /Users/mac/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/grid_builder-rails-be353dff709f/app/assets/javascripts
  /Users/mac/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/grid_builder-rails-be353dff709f/app/assets/stylesheets
  ...

When I try to include angular in an application that uses my gem, before including my gem’s script, the error is quite the same…

So should I just specify that my gem requires angularjs-rails to be added in the app Gemfile, or is there a better way to do this ?

Advertisement

Answer

As Richard Peck proposed, I required angularjs-rails in my gem’s module file, and everything went fine:

require 'angularjs-rails'

module GridBuilder
  module Rails
    require 'grid_builder/rails/engine'
    require 'grid_builder/rails/version'
    require 'grid_builder/rails/helper'
  end
end

And in my gem’s js script:

//= require angular

Thank you 🙂

Advertisement