rails 4.1 jasmine-jquery not loading - ruby-on-rails

I recently upgraded to Rails 4.1
Im using the gem 'jasmine-rails', which is working fine by itself.
I am also trying to use gem 'jasmine-jquery-rails' to load fixtures, but am unable to load it (jasmine simply doesnt recognise the jasmine-jquery functions). I added "jasmine-jquery.js" to my jasmine yml file.
I also tried adding the file directly into my app/javascripts/assets folder, and changing the jasmine.yml file to
src_dir: "app/assets/javascripts"
src_files:
- "application.{js.coffee,js,coffee}"
- "jasmine-jquery.js"
but it still will not load jasmine-jquery.
Any thoughts?

All you need is the following.
In the jasmine.yml file it suffices to have:
spec_files:
- "**/*[Ss]pec.{js.coffee,js,coffee}"
Then in the spec file you need to include the files needed for testing (those files are in the asset pipeline, so you just need to include it. that's what the jasmine-rails gems are for).
Maybe this example might help:
//= require jquery
//= require jasmine-jquery
describe("Jasmine jQuery Test", function() {
it("works out of the box", function() {
expect($('<input type="checkbox" checked="checked"/>')).toBeChecked();
});
});
Note that you require the files used for testing. In this case jquery and jasmine-jquery.
jquery is required because jasmine-jquery uses its functionality (as mentioned in the docs on GitHub) and jasmine-jquery is required because we use a matcher "toBeChecked()".
Think of it this way, all your Rails asset files are in the asset pipeline, so in your test files you just need to include what you need for testing.
Happy coding

Try adding:
#= require jquery
#= require support/jasmine-jquery-1.7.0
to your spec/javascripts/spec_helper.coffee file.
HTH,
Jarra

#Elyasin gave a working solution except that you have to require jasmine-jquery in each of your js spec files. Add //= require jasmine-jquery to app/assets/javascripts/application.js. Then you don't have to require it ever again anywhere else. It works good for me.

Related

Use grails-asset-pipeline to compile bootstrap 5 from source

I'm trying to figure out how to compile the latest bootstrap (v5.0.2) from source so I can more easily customize the theme using SASS variables for a grail (v5.2.3) app using the grails asset-pipeline.
I've been able to get the .scss to compile and load via the asset pipeline by making these changes to the build.gradle:
buildscript{
dependencies{
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:4.0.0"
classpath 'com.bertramlabs.plugins:sass-dart-asset-pipeline:4.0.0'
}
}
assets {
includes = [ '**/bootstrap.scss' ]
excludes = [ '**/_*.scss' ]
}
I downloaded the bootstrap-5.0.2.zip source distribution, unzip it, copied the
bootstrap-5.0.2/scss/ folder to the 'grails-app/assets/stylesheets/bootstrap-5.0.2-scss' folder inside my app, and update the 'grails-app/assets/stylesheets/application.css' file to include the top level bootstrap.scss file:
*
-*= require bootstrap
+*= require bootstrap-5.0.2-scss/bootstrap
*= require grails
*= require main
When I run my app using ./gradlew bootRun the styling is loading from .css, but some of the javascript features seem to be broken. For example, if I click on a dropdown menu the dropdown does not appear.
I figured this means I also need to install javascript libraries that come with bootstrap 5.0.2. I tried importing bootstrap.bundle.js copying the "bootstrap.bundle.js" file that comes with the bootstrap-5.0.2 distribution, and making this modification to 'grails-app/assets/javascripts/application.css'
//= require jquery-3.5.1.min
//= require popper.min
-//= require bootstrap
+//= require bootstrap.bundle
//= require_self
But the dropdown menus still do not show up.
Am I going about this the right way? Are there other javascript assets I need to use bootstrap v5 in my asset-pipeline?
It turns out I was being silly, it was not a JavaScript issue, it was a breaking change in the bootstrap "API". The drop-down toggle uses an attribute called 'data-toggle' in bootstrap v4, and the corresponding attribute in bootstrap v5 is data-bs-toggle. After I updated the attribute names the drop-down menus worked as expected.

How to make recurring_select gem work with Rails 6 and Webpacker

recurring_select is a gem that makes recurring events easy to select in a popup form. Demo here: http://recurring-select-demo.herokuapp.com/
(gitgub: https://github.com/GetJobber/recurring_select, see also GoRails cast on using it https://gorails.com/episodes/recurring-events)
The problem I've experienced is in including the js.coffee, and js.coffee.erb assets into my app
Following the instructions to require assets doesn't work e.g. in application.js: //= require recurring_select
I had limited success by making a change so that coffeescript files could be included and I copied the file in directly. This makes the popup appear (as seen in the image below), but it is missing but the remaining js.coffee.erb which provides the dialog (red box in image)
Any recommendations and workaround suggestions would be greatly appreciated
My files as per the gem instructions:
Application.js
...
require("trix")
require("#rails/actiontext")
require('./calendar/create_booking.js')
//= require recurring_select
//= require jquery-mobile-rs
...
Application.scss
...
#import 'tippy.js/dist/tippy.css';
#import 'flatpickr/dist/flatpickr.css';
//= require recurring_select
//= require jquery-mobile-rs
...
Extract the relevant assets, per this comment:
https://github.com/GetJobber/recurring_select/issues/125#issuecomment-557345359
Import the files:
https://github.com/GetJobber/recurring_select/issues/125#issuecomment-596663751
You will experience a few broken parts, that are to be decided by yourself what you do with those:
2 missing files (throbber and cancel)
Rails.application.config.action_controller.relative_url_root
Possible solution: Comment out the 2 missing files and remove the <%= Rails.application.config.action_controller.relative_url_root %> completely, and it will work.

How to include js and css files in rails - correct syntax

I'm trying to use Bootstrap Combobox for rails.
The instructions say that:
*"You will need two files included in your HTML in order for this to
work:
js/bootstrap-combobox.js
css/bootstrap-combobox.css*
I realise that they need to go in the application js and application css respectively, but I think that I'm getting the syntax wrong because I'm getting an error.
This is how I'm adding to application.css.scss:
* require css/bootstrap-combobox.css
This is how I'm adding to application.js:
//= require bootstrap-combobox
What error are you getting?
Without knowing the exact error, here's what I suggest:
After you make sure you have a file in your assets/stylesheets named bootstrap-combobox.css, you want your require statement to look like this:
*= require bootstrap-combobox
Your require statement looks fine for the JS assuming you have the file in the assets/javascripts folder.
You can also use //= require_tree . and *= require_tree . to require all files in your javascripts and/or stylesheets folders respectively instead as well.
Check out this section of the Rails asset pipeline for more info.

Ruby on Rails 3.1 and jQuery UI images

I'm using Ruby on Rails (Edge, the development version), and Ruby rvm 1.9.2.
application.js is as follows.
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree
Where is the right place in Ruby on Rails 3.1 to put the jQuery UI theme?
According to Autocomplete fields in Ruby on Rails 3.1 with jQuery UI I should put a jQuery UI theme in vendor/assets/stylesheets folder. That sounds like a smart place to have it, but I don't get it to work :-(.
I managed to get the CSS loaded by putting it in the assets/stylesheets folder, but the images I havn't managed to get loaded.
I could of course be using the old way with just putting the theme in the public/stylesheets/ folder, and using:
<%= stylesheet_link_tag "jquery/ui-lightness/jquery-ui-1.8.11.custom" %>
in application.html.erb, but trying to be a modern man, I would rather use the new way of doing tings :-).
Now that we have Ruby on Rails 3.1.0, this is what worked for me:
app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
This directly includes the jQuery UI provided by the jquery-rails gem. But the gem does not provide the theme files. For these, I added a theme directory under vendor/assets/stylesheets, containing:
the jquery.ui.theme.css file,
the jQuery UI theme's images directory.
Be sure to keep the theme's images directory with the CSS file! Do not put the image files under vendor/assets/images, or they won't be found by jQuery (which search them under /assets/images).
Finally, changed the app/assets/stylesheets/application.css file to:
/*
*= require_tree ../../../vendor/assets/stylesheets
*= require_tree .
*/
Example of a working setup:
$ cat app/assets/javascripts/application.js
//= require jquery
//= require jquery-ui
$ cat app/assets/stylesheets/application.css
/*
*= require vendor
*
*/
$ cat vendor/assets/stylesheets/vendor.css
/*
*= require_tree ./jquery_ui
*
*/
vendor/assets/ $ tree
stylesheets
vendor.css
jquery_ui
     jquery-ui-1.8.13.custom.css
...
images
   jquery_ui
   ui-bg_flat_0_aaaaaa_40x100.png
...
Finally run this command:
vendor/assets/images $ ln -s jquery_ui/ images
Enjoy your jQuery UI
I've fallen down to doing it the old way:
I put the jQuery folder, containing the theme (unchanged with both CSS and images folder) in the assets/stylesheets folder, and putting in: <%= stylesheet_link_tag "jquery/ui-lightness/jquery-ui-1.8.13.custom" %> in app/views/layouts/application.html.erb file. This solution is the one with less hazel when I will update jQuery later.
(Thanks for all suggestions on the solution. It is time to conclude.)
I like to selectively download jQuery UI JavaScript code so that I can easily upgrade to any future versions and have a light-weight jQuery UI (include needed files only, here progressbar.js).
I have the following setup for the "Dot Luv" jQuery UI theme.
Note:
The JavaScript and CSS files are uncompressed and taken from jquery-ui-1.8.16.custom/development-bundle/ui and jquery-ui-1.8.16.custom/development-bundle/themes/dot-luv respectively, and I rely on sprokets to minify and compress them.
The images are from jquery-ui-1.8.16.custom/development-bundle/themes/dot-luv/images.
Directory Structure:
app/assets/javascripts/application.js
//= require jquery
//= require jquery-ui/v1.8.16/Core/jquery.ui.core
//= require jquery-ui/v1.8.16/Core/jquery.ui.widget
//= require jquery-ui/v1.8.16/Widgets/jquery.ui.progressbar
//= require jquery_ujs
app/assets/stylesheets/application.css.scss
*= require_self
*= require jquery-ui/v1.8.16/dot-luv/jquery.ui.all
*= require jquery-ui/v1.8.16/dot-luv/jquery.ui.base
*= require jquery-ui/v1.8.16/dot-luv/jquery.ui.core
*= require jquery-ui/v1.8.16/dot-luv/jquery.ui.progressbar
*= require jquery-ui/v1.8.16/dot-luv/jquery.ui.theme
config/application.rb
config.assets.paths << File.join(Rails.root,'vendor/assets/images/jquery-ui/v1.8.16/dot-luv/')
I know this thread already has a lot of answers but I'm going to throw in what worked best for me.
There is a gem called jquery-ui-themes that includes the default jQuery UI themes already converted to sass using the image-path helper. So you can include the gem and get any of the default themes out of the box just by adding them to your application.css file
If you want to use your own custom theme (as I did) there is a rake task that will automatically convert the CSS file to SCSS and use the image-path helper to find the right path.
With Ruby on Rails 3.1.2 I did the following.
#app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
For the CSS files, I like to do #import instead to have more control over the load order of CSS files. To do this, I have to add the .scss extension to the app/assets/stylesheets/application.css file, and also to all CSS files I want to import, like the jQuery UI CSS file.
#app/assets/stylesheets/application.css.scss
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*/
#import "jquery-ui/ui-lightness/jquery-ui-1.8.16.custom.css.scss";
/* Other css files you want to import */
#import "layout.css.scss";
#import "home.css.scss";
#import "products.css.scss";
....
Then I put everything jQuery UI related in vendor/assets like this:
jQuery UI stylesheet:
vendor/assets/stylesheets/jquery-ui/ui-lightness/jquery-ui-1.8.16.custom.css.scss
jQuery UI images folder:
vendor/assets/images/images
Note that you can create additional folder in the stylesheets path like I did here with "jquery-ui/ui-lightness" path. That way you can keep multiple jQuery themes nicely separated in their own folders.
** Restart your server to load any newly created load paths **
Ryan Bates has some excellent screencasts about the asset pipeline and Sass in Ruby on Rails 3.1, where he shows how to use the #import function in Sass. Watch it here:
#279 Understanding the Asset Pipeline
#268 Sass Basics
Edit: I forgot to mention that this works both locally and on Heroku on the Cedar stack.
There is now a jquery-ui-rails gem (see announcement). It packages the images as assets (and correctly references them from the CSS files) so things Just Work. :-)
So, here's one way to do it that lacks the downsides of some of the others mentioned here -- it doesn't require you to take apart the theme and put parts of it in different places, it doesn't require symbolic links, and it still allows you to compile the theme css into the one main css as part of the asset pipeline. It does not require a monkey patch like Nash Bridges' suggestion.
However, it does require an additional kind of hacky configuration line. (a one-liner though, basically).
Okay, put your theme in vendor/assets/jquery/ui-lightness/, like you wanted to. (will also work in lib/assets or app/assets, same way).
And
/* =require ui-lightness */
in your application.css. So far so good. Now to get the images to show up right, just add this to config/application.rb:
initializer :add_jquery_ui_asset_base, :group => :all, :after => :append_assets_path do
config.assets.paths.unshift Rails.root.join("vendor", "assets", "stylesheets", "jquery-ui", "ui-lightness").to_s
end
For me, it now works in dev, production, and other non-standard asset configs I could think of (like dev with debug=false, which trips up some of the other attempted solutions).
More info at http://bibwild.wordpress.com/2011/12/08/jquery-ui-css-and-images-and-rails-asset-pipeline/
Building on a number of other suggestions here, I found a solution that works in my dev environment and in production on Heroku.
app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require_tree .
app/assets/stylesheets/application.css
/*
*= require_self
*= require vendor
*= require_tree .
*/
vendor/assets/stylesheets/vendor.css
/*
*= require_self
*= require_tree .
*/
I added jquery-ui-1.8.16.custom.css and the associated images folder to vendor/assets/stylesheets (I found that unless the images folder was in the same folder as vendor.css it didn't work).
No other changes were necessary for this to work in the Heroku production environment.
Thanks to #denysonique, #softRli and #Paul Cook for their previous answers which helped me.
To get this to work on both my local dev environment and on Heroku, I did almost the same thing as denysonique suggested, but with a couple of differences at the end:
First, my directory structure looked like this:
vendor/assets/images/
jquery_ui/
images/
ui-bg_flat_0_aaaaaa_40x100.png
...
And second, my symbolic link was:
vendor/assets/images $ ln -s jquery_ui/images images
This is what finally worked for me.
There's a proposed fix in Ruby on Rails that makes precompilation of jQuery UI's images work.
(As of 3.1.0rc6, the asset precompiler uses the regular expression /\w+\.(?!js|css).+/ to find things to compile. This misses all the jQuery UI images because their names include dashes and underscores.)
Combining suggestions here is what got things working for me:
Put the jQuery UI theme CSS folder in vendor/assets/stylesheets.
Put vendor.css in vendor/assets/stylesheets:
*= require_tree ./theme-css-name
In production.rb I added this:
config.assets.paths << File.join(Rails.root,'vendor/assets/stylesheets/theme-css-name
That is what it took to get the images to get precompiled and resolve without editing the jQuery UI theme CSS file or moving the images out of the theme CSS folder.
I think you can put ui styles in app/assets/stylesheets. Do something like this:
# app/stylesheets/application.css.scss
//= require_self
//= require libraries/jquery-ui
//= require_tree .
In 'jquery-ui' stylsheet, something like this:
.class{
background: url(/assets/jquery-ui/ui-icons_222222_256x240.png)
}
What I did to get everything to work properly is as follows.
1.) Added the CSS to the assets/stylesheets folder
2.) Added the images to the assets/images folder
3.) Removed the paths to all the images in the CSS using find "url(images/" and replace with "" leaving just the image file name.
/* Example: */ .ui-icon { background-image: url(images/ui-icons_222222_256x240.png) ; }
/* Becomes: */ .ui-icon { background-image: url(ui-icons_222222_256x240.png) ; }
Bingo! Everything should work correctly.
Using Ruby on Rails 3.1.1, I simply placed the files as follows. No other changes were required.
app/assets/stylesheets/jquery-ui-1.8.16.custom.css
app/assets/images/ui-bg_highlight-soft_75_cccccc_1x100.png
...
What worked for me was instead of having the jQuery theme CSS file in app/assets/stylesheets/ and the images in app/assets/images/. I placed them into app/assets/images/images/, and it worked. It's kind of a hack, but it seems to work at this point with minimal fudging and without modifying the CSS files.
Get the CDN hosted theme from Google:
= stylesheet_link_tag 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/themes/ui-lightness/jquery-ui.css'
For that moment, I found not a perfect but working a solution.
Assuming you have jQuery UI theme in the /vendor/assets/stylesheets/ folder. Then you have to modify application.css:
/* =require ui-lightness */
and create plugin_assets_monkey_patch.rb in the /config/initializers
Dir[File.join(Rails.root, 'vendor/assets/stylesheets/*/')].each do |dir|
AppName::Application.config.assets.paths << dir
index_content = '/*=require_tree .*/'
index = File.join(dir, 'index.css')
unless File.exist?(index)
File.open(index, 'w') { |f| f.puts index_content }
end
end
index.css in every /vendor/assets/stylesheets/ subfolder guarantees that stylesheets like jquery-ui-1.8.11.custom.css will be compiled (if you require that subfolder).
config.assets.paths ensures that folders like /vendor/assets/stylesheets/ui-lightness/images are visible at the application root scope.

How do I add jquery-ui to a Ruby on Rails 3.1 app?

I have added //=require jquery-ui to my application.js file and that seems to give me the javascript but I can't seem to get the stylesheets to be included. If I understand it right they should go in vendor/stylesheets but how do I get them to be included?
Perhaps it's easier to use the jquery-ui-rails gem (see announcement). It packages everything up so things "just work".
You can use google CDN, to add the css theme inside the head section of your app. Simply add this application.html.haml under the %head section(or the same thing translated to erb).
The css theme
%link{:href => "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/themes/ui-lightness/jquery-ui.css", :rel => "stylesheet", :type => "text/css"}
If you want the jquery-ui minified.
%script{:src => "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/jquery-ui.min.js"}
if you include 'jquery-rails' in Gemfile jquery-ui will be included requiring this in application.js:
//= require jquery-ui.min
if you run in Console: Rails.application.config.assets.paths you will get all paths Rails will look into for assets. In my case for instance:
- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/app/assets/images
- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/app/assets/javascripts
- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/app/assets/stylesheets
- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/vendor/assets/javascripts
- /Users/aldo/Satio/Desarrollo/rails/subaquaclub/vendor/assets/stylesheets
- /Users/aldo/.rvm/gems/ruby-1.9.2-p290#subaquaclub31/gems/jquery-rails-1.0.13/vendor/assets/javascripts
See the last line ? if you check in there you will find jquery-ui so there you go.
quote from the jquery-rails manual:
In order to use the themed parts of jQuery UI, you will also need to supply your own theme CSS. See jqueryui.com for more information.
So, you have to include or style them yourself! Just include the theme's .css in the header of your page. In Rails 3 you'd put the css in public/stylesheets, don't know for Rails 3.1.
jquery-rails no longer has jquery-ui as part of its assets. You should use gem 'jquery-ui-rails' for that.
Furthermore, to find out where an asset is coming form in rails you can do the following:
paths = Rails.application.config.assets.paths
for path in paths do
puts "Found in: #{path}" if Dir.glob("#{path}/*").grep(/jquery-ui/).present?
end
This should make it easy to find the asset.
Just remember //= require jquery.ui.all needs to be after //= require_tree .
Wasted a lot of my time because of this as most of Jquery UI functionalities won't work.
denysonique has the answer in this question as pointed out by epochwolf in the comments.
Rails 3.1 and jquery-ui assets
If you have jquery-rails on Gemfile, just do:
//= require jquery-ui
or
//= require jquery-ui.min
both will work fine. If not, try to update jquery-rails gem
Don't forget the css files, here I followed some articles on that other answer: Ruby on Rails 3.1 and jQuery UI images and got it working by this way:
*= require jquery-ui/jquery-ui.css
this is the path for files:
vendor/assets/stylesheets/jquery-ui/jquery-ui.css
vendor/assets/images/jquery-ui/images/ (your theme images)

Resources