Disable css in test environment for Rspec - capybara-webkit

I'm using rails 4 and rspec with capybara for testing.
I want to disable css in my test environment. How can I do that? Can anyone help me.

The simplest way - skip CSS files loading in your /app/views/layouts/application.html.erb for test env.:
<% unless Rails.env.test? %>
<%= stylesheet_link_tag "application", :media => "all" %>
<% end %>

Capybara allows you to blacklist specific URLs from being loaded. You can utilize this feature to block the loading of your CSS files. For example if your CSS is loaded from /assets/application.css:
RSpec.configure do |config|
config.before(:each, js: true) do
page.driver.browser.url_blacklist = [
"http://127.0.0.1:#{page.server.port}/assets/application.css"
]
end
end
You can confirm what assets are getting loaded by turning on Capybara's debugging. To enable debugging simply set the driver to :webkit_debug instead of :webkit
Capybara.javascript_driver = :webkit_debug

Related

EJS giving error "Error: property value expected" when used inside a style attribute

I am trying to set a background color on my div based on a a color send down by my server. I can't solve this with classes because the color value could be any legal color value. However, VSCode is giving me an error when I type the following. Is EJS not allowed to use with css related stuff?
<div style="background-color: <%= display_color %>;"></div> // Error: property value expected
This is an ESLint error. ESLint is a linting tool. It can help you find and fix (mostly/usually) stylistic problems with your code. It's highly configurable, and can also be used to auto-fix some problems for you.
As you already found, you can disable the lint warning for a single site/line like so:
<div <% /* eslint-disable css-propertyvalueexpected */ %> style='background-color: <%= display_color %>;'></div>
If you want to disable the warning once and for all, create an ESLint configuration file and do it there.
If you haven't installed the ESLint NPM package to use it that way, you're probably getting this because of one of your VS Code extensions.

How can I use coffeescript within a underscore.js template

I am using underscore.js templates, and have certain if conditions embedded within template. I would like to use coffeescript within the template.
<% if (app.user.get("id") != -1 or app.user.get("product")?.name != "Foo") {%>
do-stuff
<% } %>
Above won't work, I have to use javascript instead of coffee.
Is there a way to get this done, other than using any further third party libraries like haml-coffee?
Nope if you're working in that environment it'll have to be native js. You could however, do some of the work in a coffee script file that returns an html template instead.

Devise error messages missing after custom redirect

I have a custom redirects in place for my devise sign up forms, I have two forms, one for an individual and another for a company. I have added this in the create action of the registrations controller:
if resource.company_form
redirect_to new_user_registration_path(company: true)
else
redirect_to new_user_registration_path
end
In doing this though I have lost all the devise error messages, as in the don't display any validation errors, so I need to send the error messages along with the redirect, don't I? However, I am not sure how.
So far I have tried printing the error messages to the console:
ap(resource.errors.full_messages)
[
[0] "Email can't be blank",
[1] "Password can't be blank",
[2] "Company name can't be blank"
]
Whereas this:
ap(resource.errors)
#messages={:email=>["can't be blank"], :password=>["can't be blank"], :company_name=>["can't be blank"]}
How would I get the error messages to be displayed above the form again?
The magic of devise error messages is made with respond_with method.
Hence you can change the redirect_to for a respond_with block
respond_with(resource) do |format|
if resource.company_form
format.html { render 'new', locals: { is_organisation: true } }
else
format.html { render 'new' }
end
end
and in your view
<% params[:organisation] ||= is_organisation -%>
Not sure if this will help, but you can customize this even further with your messages. First things first it's a good idea to actually ensure this works first. Then customize it further.
Add your toastr gem to your gemfile.
gem 'toastr-rails', '~> 1.0'
In your application.js you will need to add //= require toastr
In your stylesheet.scss you will need to import toastr #import "toastr";
Then run bundle in your terminal
In your views/devise/registrations/ folder and views/devise/password/ folder, the pages inside are your devise views that show the error messages. There you will find the error messages by devise. <%= devise_error_messages! %>
So what you wanna do, is customize these messages.
Now go to views/shared and create a new file and name it _devisemes.html.erb
<% unless resource.errors.empty? %>
<script type="text/javascript">
<% resource.errors.full_messages.each do |value| %>
toastr.error('<%= value %>')
<% end %>
</script>
<% end %>
After you have saved this file. Just go to the following files and find
<%= devise_error_messages! %>
Replace it with <%= render 'shared/devisemes' %> in the following files below:
views/devise/registrations/edit.html.erb
views/devise/registrations/new.html.erb
views/devise/password/new.html.erb
views/devise/password/edit.html.erb
Now logout and go to create an account and without an email, or password and test it. You will notice all the error messages from devise showing up with toastr.

TinyMCE and ActiveAdmin for Rails

I am reacquainting myself with Rails and I am really liking Active Admin. I would like to get tinyMCE working with it for use in text areas. However, any instructions I find are incomplete. For some reason, I think that I am missing something really simple here.
So, for example, I have tinymce-rails installed (3.4.9) and followed the instructions (https://github.com/spohlenz/tinymce-rails). However, here's where I think I failed: actually initiating tinyMCE. According to the doc, I have two options:
use the <%= tinymce %> helper or...
initialize it like the following tinyMCE.init({
mode: 'textareas',
theme: 'advanced'
});
I tried adding the latter to my active_admin.js file to no avail.
If someone could guide me on this, I would be most appreciative.
I got it working doing the following things (outside of the install described at the repo)
In admin/my_class.rb:
ActiveAdmin.register MyClass do
form do |f|
f.inputs do
f.input :body, :input_html => { :class => "tinymce" }
end
end
end
In initializers/active_admin.rb:
...
config.register_javascript 'tinymce.js'
This was what actually got the tinymce.js script to show up in the head of the admin layout.
In javascripts/active_admin.js:
//= require active_admin/base
//= require tinymce
$(document).ready(function() {
tinyMCE.init({
mode: 'textareas',
theme: 'advanced'
});
});
After doing those things, that body input (text area) had a fully functioning editor on it.
Do your textarea inputs have a 'class' attribute or something that tinyMCE can hook into? Does it work from the javascript console (firefox/chrome)? Have you checked the presence of tinymce.js in the head(source) of your page.
I got it working with a form partial, and I had to give a class to the input element so tinymce could hook to it.
<%= f.input :literature_nld, :input_html => { :class => 'tinymce', :size => "80x4" } %>
Good luck

How can I associate ".js.php" to JavaScript Syntax in the NetBeans 6.5 IDE?

When I generate CSS or JavaScript files using PHP I like to use .js.php or .css.php file extensions. so that I know what's going on.
Is there a way of associating these "compound" file extensions to their respective languages?
I don't use NetBeans or PHP, but the following trick helped me in a similar setting:
<?php if(0) { ?><script><?php } ?>
# code goes here
<?php if(0) { ?></script><?php } ?>
Simply surround the js code with <script> or <style> tags that don't get rendered. No need to configure any special associations, assuming the editor is smart enough about HTML.
What I usually do is condense such "compound" extensions into one, following the tradition of condensing .tar.gz into .tgz.