Saurabh Sikchi
Saurabh Sikchi

Reputation: 21

Rails - How do I check if js file exists before requiring it in application.js?

I forked the plutus gem (https://github.com/mbulat/plutus) and it has a requirement of jquery.ui.datepicker. rails-jquery-ui gem 5.0 changed folder name to jquery-ui (previous jquery.ui). Hence, this returns a Sprockets::FileNotFound for my app which depends on rails-jquery-ui 5.0.

How do I conditionally include jquery.ui.datepicker or jquery-ui/datepicker in plutus application.js so that the gem is usable by people who may have any gem version.

Is my approach correct or is there a better way to do this?

Upvotes: 1

Views: 126

Answers (1)

Dharam Gollapudi
Dharam Gollapudi

Reputation: 6438

You could rename the application.js to application.js.erb and include any ruby conditional in there.

Once you rename it to application.js.erb you could write something like the following:

<% if Jquery::Ui::Rails::VERSION >= '5.0' %>
  //= require jquery-ui/datepicker
<% else %>
  //= require jquery.ui.datepicker
<% end %>

I have not checked the actual file names or folder structure. Above code is just a pseudo code, adapt it to your need/requirement.

Upvotes: 0

Related Questions