jQuery: Work with text nodes and comment nodes
Nearly all jQuery traversal functions ignore elements that are not HTML tags.
To work with other type of nodes (like text, comment or CDATA sections) you need to:
- Retrieve child nodes
contents()(which behaves likechildren()except that it returns all types of child nodes) - Filter manually using either plain Javascript or jQuery's
filter()method
Example
Let's write a function that takes a jQuery element and returns an array of all child nodes that are text nodes:
function selectTextNodes($container) {
retu...
Vastly increase the usability of Thunderbird's search
These two addons will change your life:
- Search as list
-
This will always open search results in the list views instead of the barely usabely faceted search view.
- Sort search results by date not relevance
-
Does what it says.

MySql lost connection trouble
Directly from the MySql docs:
There are three likely causes for this error message.
Usually it indicates network connectivity trouble and you should check the condition of your network if this error occurs frequently. If the error message includes during query, this is probably the case you are experiencing.
Sometimes the during query form happens when millions of rows are being sent as part of one or more queries. If you know that this is happening, you should try increasing net_read_timeout from its default of 30 seconds to 60 s...
Cucumber: Skipping steps in a scenario outline, based on the current example
In Cucumber, scenario outlines help avoiding tests that are basically the same, except for a few variables (such as different inputs). So far, nothing new.
The problem
Now what if your test should (or should not) do something, like filling in a field only for some tests?
Scenario Outline: ...
When I open the form
And I fill in "Name" with "<name>" # <= we want to do this only occasionally
Then everybody should be happy
Examples:
| name |
| Alice |
| Bob |
You could o...
Fixing Ruby debugger: *** Unknown command: "something". Try "help".
So you have placed a breakpoint somewhere and now want to dig around, but not even inspecting variables is working:
(rdb:3) @order_item
*** Unknown command: "@order_item". Try "help".
The reason is, you must tell the debugger to evaluate your expression. One workaround is to call irb to open an irb session at your breakpoint. Resume by sending Ctrl+D twice or by returning to the outer irb with "exit" and then continuing with "c".
However, the native debugger command for your issue is eval (or its shorter alias `e...
6 front-end techniques for Rails developers. Part I: From big ball of mud to separated concerns
Amazing guide how to divide a ball of Javascript spaghetti distinct separate layers (model, view, controller, backend adapter).
It does not use a Javascript framework.
RSpec 3 no longer chooses a spec's type based on its directory
While RSpec 1 and 2 decided that specs inside spec/model are model specs, and those inside spec/features are feature specs (and so on), RSpec 3 will no longer do that by default.
This will result in errors such as missing routing helpers, etc.
There are 2 ways to fix this:
-
Explicitly set the type on each spec. For example:
describe '...', type: 'feature' do # ... end -
Add this to your
spec_helper.rb(inside theRSpec.configureblock) to restore the old behavior:...
Howto respond html or json in the same controller action with Rails 2
Code snippet tested with Rails 2.3
def index
# ...
if request.xhr?
html = render_to_string(:partial => "list", :layout => false)
respond_to do |format|
format.html { render :text => html }
format.json { render :json => {:html => html, ... } }
end
end
end
Note: Perhaps you ran into ActionView::MissingTemplate error and this card might help. If you call render_to_string within the format.json block, Rails will only look for an index.json template, but not for an `index.erb...
terminator keyboard shortcuts
When connecting to multiple (i.e. > 4) servers to dive into logfiles or do security updates, terminator is what you want.
There are several keyboard shortcuts available:
- Ctrl-Shift-E: Split the view vertically.
- Ctrl-Shift-O: Split the view horizontally.
- Ctrl-Shift-P: Focus be active on the previous view.
- Ctrl-Shift-N: Focus be active on the next view.
- Ctrl-Shift-W: Close the view where the focus is on.
- Ctrl-Shift-Q: Exit terminator.
- Ctrl-Shift-X: Enlarge active window...
SudoSlider: a jQuery slider
SudoSlider is a simple yet powerful content slider that makes no (or very few) assumptions about your markup and is very customizable.
You can basically embed any HTML into the slides, so you can mix images, videos, texts, and other stuff.
Check out the demos.
Please note:
- There is a ton to configure. Check the demos and read the docs.
- It does not bring styles for prev/next links etc, so you need to style controls yourself (which I consider to b...
Working around OpenSSL::SSL::SSLErrors
If your requests blow up in Ruby or CURL, the server you're connecting to might only support requests with older SSL/TLS versions.
You might get an error like: OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=unknown state
SSL Server Test
This SSL Server Test can help finding out which SSL/TLS versions the server can handle.
Ruby
In Ruby, you can teach Net::HTTP to use a specific SSL/TLS version.
uri = URI.parse(url)
ssl_options = {
use_ssl: true,
ssl_version...
Cucumber step to manually trigger javascript events in Selenium scenarios (using jQuery)
When you cannot make Selenium trigger events you rely on (e.g. a "change" event when filling in a form field), trigger it yourself using this step:
When /^I manually trigger a (".*?") event on (".*?")$/ do |event, selector|
page.execute_script("jQuery(#{selector}).trigger(#{event})")
end
Note that this only triggers events that were registered through jQuery. Events registered through CSS or the native Javascript registry will not trigger.
mattheworiordan/capybara-screenshot
Using this gem, whenever a Capybara test in Cucumber, Rspec or Minitest fails, the HTML for the failed page and a screenshot (when using capybara-webkit, Selenium or poltergeist) is saved into $APPLICATION_ROOT/tmp/capybara.
Link via Binärgewitter Podcast (German).
Atomic Grouping in regular expressions
A little-known feature of modern Regexp engines that help when optimizing a pattern that will be matched against long strings:
An atomic group is a group that, when the regex engine exits from it, automatically throws away all backtracking positions remembered by any tokens inside the group.
A saner alternative to SimpleForm's :grouped_select input type
SimpleForm is a great approach to simplifying your forms, and it comes with lots of well-defined input types. However, the :grouped_select type seems to be overly complicated for most use cases.
Example
Consider this example, from the documentation:
form.input :country_id, collection: @continents,
as: :grouped_select, group_method: :countries
While that looks easy enough at a first glance, look closer. The example passes @continents for a country_id.\
SimpleForm actua...
Lightweight PDF viewer: MuPDF
MuPDF is a PDF reader that renders very quickly, yet still correctly.
It supports PDF 1.7 and all the fancy shenanigans that evince (Ubuntu's default PDF reader) fails to render properly.
On Ubuntu, MuPDF is available in the Universe sources. Simply install via APT:
sudo apt-get install mupdf
Interaction primarily happens via keyboard, but there is basic mouse support.\
See the manpage for more details on navigating.
One downside: There is no printing support, so if...
When using "render :text", set a content type
When your Rails controller action responds with only a simple text, render text: 'Hello' may not be what you want. You should not even use it on Rails 4.1+ any more.
By default, a "text" response from a Rails controller will still be a sent as text/html:
render text: 'Hello'
response.body # => "Hello"
response.content_type # => "text/html"
While this may not be too relevant for a Browser client, the response's content type is simply wrong if you want to send a plain-text response, and can cause trouble. \
For example, con...
Chrome 34+, Firefox 38+, IE11+ ignore autocomplete=off
Since version 34, Chromium/Chrome ignores the autocomplete="off" attribute on forms or input fields. Recent versions of other browser do the same, although implementation details vary.
This is especially problematic for admin areas because Chrome might automatically fill in a password on a "add new user" forms.
Chrome developers say this is by design as they believe it encourages users to store more complex passwords.
Recommended fix for Chrome and F...
About "unexpected '#' after 'DESCENDANT_SELECTOR' (Nokogiri::CSS::SyntaxError)"
The error unexpected 'x' after 'DESCENDANT_SELECTOR' (Nokogiri::CSS::SyntaxError) (where x may be basically any character) occurs when the Nokogiri parser receives an invalid selector like .field_with_errors # or td <strong>.
In Cucumber, the culprit will be an invalid step definition that builds an invalid selector:
# inside some step definition:
field = find_field(label)
page.send(expectation, have_css(".field_with_errors ##{field[:id]}"))
The above raises the mentioned error if field[:id] is nil, i.e. the foun...
Making media queries work in IE8 and below
When using @media CSS queries, Internet Explorer 8 and below will fail to respect them.
Though there are several options (like mediatizr and css3-mediaqueries), Respond.js was the only one that worked for me.
If you do not want to pollute your application's default JS file with Respond.js, simply:
-
Create an extra JS file (like
media_queries_polyfill.js) that loads Respond.js://= require respond-1.4.2 -
Make sure it's added to
config.assets.precompile -
Embed that JS fi...
Retrieving the class an ActiveRecord scope is based on
Edge Rider gives your relations a method #origin_class that returns the class the relation is based on.
This is useful e.g. to perform unscoped record look-up.
Post.recent.origin_class
# => Post
Note that #origin_class it roughly equivalent to the blockless form of #unscoped from Rails 3.2+, but it works consistently across all Rails versions. #unscoped does not exist for Rails 2 and is broken in Rails 3.0.
Disabling Spring when debugging
Spring is a Rails application preloader. When debugging e.g. the rails gem, you'll be wondering why your raise, puts or debugger debugging statements have no effect. That's because Spring preloads and caches your application once and all consecutive calls to it will not see any changes in your debugged gem.
Howto
Disable spring with export DISABLE_SPRING=1 in your terminal. That will keep Spring at bay in that terminal session.
In Ruby, [you can only write environment variables that subproc...
Offtopic: Floppy-disc OS
MenuetOS is an Operating System in development for the PC written entirely in 32/64 bit assembly language. Menuet64 is released under License and Menuet32 under GPL. Menuet supports 32/64 bit x86 assembly programming for smaller, faster and less resource hungry applications.
- Fits on a single floppy, boots also from CD and USB drives
- Responsive GUI with resolutions up to 1920x1080, 16 million colours
- Free-form, transparent and skinnable application windows, drag'n drop
- SMP multiprocessor support with currently up to 8 cpus
- IDE: E...
How to create Rails Generators (Rails 3 and above)
General
- Programatically invoke Rails generators
-
Require the generator, instantiate it and invoke it (because generators are
Thor::Groups, you need to invoke them withinvoke_all). Example:require 'generators/wheelie/haml/haml_generator' Generators::HamlGenerator.new('argument').invoke_allOther ways: Rails invokes its generators with
Rails::Generators.invoke ARGV.shift, ARGV. From inside a Rails generator, you may call the [inherited Thor methodinvoke(args=[], options={}, config={})](https://github...