randym/axlsx · GitHub
Axlsx is an incredible gem to generate "Office Open XML" spreadsheet files (XLSX). Does not break on large spreadsheets and supports a ton of features like graphs.
API looks mature and existing code is easy to migrate when coming from the spreadsheet gem.
The documentation of some methods is a bit out of date, but you'll find your way around the gem's code.
No support for reading files, however. :( If you want to open XLSX spreadsheets (for example to confirm your output in tests), you can use [roo](h...
RSpec: Defining helper methods for an example group
You can define methods in any example group using Ruby's def keyword or define_method method:
describe "example" do
def sum(a, b)
a + b
end
it "has access to methods defined in its group" do
expect(sum(3, 4)).to be(7)
end
end
The helper method is also available to groups nested within that group. The helper method is not available to parent or sibling groups.
Global helpers
To define helpers for all specs (or all specs of a type), [define it in a module](https://rspec.info/features/3-12/rspec-core/help...
Understanding database cleaning strategies in tests
TLDR: In tests you need to clean out the database before each example. Use :transaction where possible. Use :deletion for Selenium features or when you have a lot of MyISAM tables.
Understanding database cleaning
You want to clean out your test database after each test, so the next test can start from a blank database. To do so you have three options:
- Wrap each test in a transaction which is rolled back when you're done (through
DatabaseCleaner.strategy = :transactionor `config.use_transactional_fi...
Git: In an (interactive) rebase, find out which commit you are currently working on (until version < 1.7.9.5)
When you are using git rebase and are currently editing a commit (due to a conflict, for example), you may want to know the current commit. [1]\
Luckily, there is lots of useful stuff in the .git directory.
Commit hash that you are currently applying
cat .git/rebase-merge/stopped-sha
Useful if you want to inspect the original commit for its changes, for example like:
git show `cat .git/rebase-merge/stopped-sha`
Current commit's message
cat .git/rebase-merge/message
In case you forgot what the changes are suppo...
jquery-timing - a jQuery plugin you should know about
jquery-timing is a very useful jquery plugin that helps to remove lots of nested anonymous functions. It's API provides you some methods that help you to write readable and understandable method chains. See yourself:
Example
// before
$('.some').show().children().doThat();
window.setTimeout(function(){
$('some').children().doSomething().hide(function() {
window.setTimeout(function() {
otherStuffToDo();
}, 1000);
});
}, 500);
// after
$('.some').s...
How to provoke Selenium focus issues in parallel test processes
As attachments to this card you will find a Cucumber feature and supplementing step definition that you can use to provoke Selenium focus issues that only occur when two focus-sensitive Selenium scenarios run at the same time (probably with parallel_tests). This can help you to detect and fix flickering integration tests.
The attached feature works by going to your root_path and focusing a random form element every 5...
Analyse TCP/UDP traffic with netcat
Sometimes you want to see what data you get through a TCP or UDP connection.
For example, you want to know how a HTTP Request look like.
It's very easy with netcat.
Example to listen on port 80 and the output gets to stdout.
sudo nc -kl 80
It's also possible write it into a file:
sudo nc -kl 80 > output.txt
If you use Ports higher than 1000 you don't need to be root (sudo).
Custom bash autocompletion
The bash offers control over the behavior of autocompletion.
The most primitive example is this (just run it in your bash; if you want it available everywhere, put the complete ... line into your .bashrc):
> complete -W "list of all words for an automatic completion" command_to_be_completed
> command_to_be_completed a<TAB>
all an automatic
With complete you define how the specified command shall be completed. For basic needs, -W (as in "word list") should be enough, but you may also specify a function, a glob patte...
How to test if an element has scrollbars with JavaScript (Cucumber step inside)
The basic idea is pretty simple: an element's height is accessible via the offsetHeight property, its drawn height via scrollHeight -- if they are not the same, the browser shows scrollbars.
var hasScrollbars = element.scrollHeight != element.offsetHeight;
So, in order to say something like...
Then the element "#dialog_content" should not have scrollbars
... you can use this step (only for Selenium scenarios):
Then /^the element "([^\"]+)" should( not)? have scrollbars$/ do |selector, no_scrollbars|
scroll_heig...
How to find out the currently focused DOM element with JavaScript
This works in all relevant browsers:
document.activeElement
You can use this in your Selenium steps, for example, to assert that a form field is or is not focused.
Performance analysis of MySQL's FULLTEXT indexes and LIKE queries for full text search
When searching for text in a MySQL table, you have two choices:
- The LIKE operator
- FULLTEXT indexes (which currently only work on MyISAM tables, but will one day work on InnoDB tables. The workaround right now is to extract your search text to a separate MyISAM table, so your main table can remain InnoDB.)
I always wondered how those two methods would scale as the number of records incr...
Custom error pages in Rails
Static error pages
To add a few basic styles to the default error pages in Rails, just edit the default templates in public, e.g. public/404.html.
A limitation to these default templates is that they're just static files. You cannot use Haml, Rails helpers or your application layout here. If you need Rails to render your error pages, you need the approach below.
Dynamic error pages
- Register your own app as the applicatio...
Cronjobs: "Craken" is dead, long live "Whenever"
Our old solution for cronjobs, the "craken" plugin, is no longer maintained and does not work on Rails 3.2+.
We will instead use the whenever gem.
"Whenever" works just like "craken", by putting your rake tasks into the server's cron table. Everything seems to work just like we need it.
Installation for new projects
-
Add "whenever" to your
Gemfile:group :deploy do gem 'whenever', require: false end -
Add it to your
config/deploy.rb:
...
CSS that lets your alarm bells ring
Harry Roberts, a youngster from the UK, wrote a comprehensive article telling you how to smell CSS rot early.
Examples:
- Undoing styles
- Magic numbers
- Qualified selectors
- Dangerous selectors
- Reactive !important
… and more.
How to change the hostname in Cucumber features
Capybara uses www.example.com as the default hostname when making requests.
If your application does something specific on certain hostnames and you want to test this in a feature, you need to tell Capybara to assume a different host.
Given /^our host is "([^\"]+)"$/ do |host|
page.config.stub app_host: "http://#{host}"
# In older Capybaras (< 2.15) you needed to do this instead:
Capybara.stub app_host: "http://#{host}"
end
You can now say:
When I go to the start page
Then I should not see "Home ...
SearchableTrait is now a gem: Dusen
For two years we've been using SearchableTrait which gives models the ability to process Googlesque queries like this:
Contact.search('a mix of words "and phrases" and qualified:fields')
This trait used to be a huge blob of code without tests and documentation, so I made a gem out of it. Check out https://github.com/makandra/dusen for code, tests, and a huge README.
You should use the Dusen gem and delete SearchableTrait in all future projects.
Note that the syntax to define query proc...
How to solve Selenium focus issues
Selenium cannot reliably control a browser when its window is not in focus, or when you accidentally interact with the browser frame. This will result in flickering tests, which are "randomly" red and green. In fact, this behavior is not random at all and completely depends on whether or not the browser window had focus at the time.
This card will give you a better understanding of Selenium focus issues, and what you can do to get your test suite stable again.
Preventing accidental interaction with the Selenium window
--------------------...
How to fix: Unable to read files from a VirtualBox shared host folder
When you want to copy/move from a shared folder (on Windows guests, for example) and it fails with absurd error messages (lots of text or an error about the target being read-only), you are probably running on Guest Additions that no longer match your VirtualBox version.
Fix: install the latest VirtualBox Guest Additions in your guest machine.
Note that you need to make sure you are using the correct ISO.
How to fix: VirtualBox Guest Additions setup is out of date
When you are trying to install/update VirtualBox Guest Additions on your guest machine but the setup is for a different/older version (for example 4.0.4 tools for a 4.1.x VirtualBox installation), try this:
sudo apt-get install virtualbox-guest-additions-iso
For some reason, this is no dependency to VirtualBox itself -- and if you have an old ISO lying around, VirtualBox will just pick that one, regardless of its version.
Oh joy.
Zeus promises to make rails development faster
I am talking about development speed. When your application starts growing and you start adding gems, it starts to take really long to start up, be it the server, console or just running a single spec.
Zeus is smart, you don’t have to put it in your Gemfile or run it with Bundler, all you need to do is create a JSON config file via
zeus initand then start the serverzeus start.
After that, you’re ready to go, all you need to do is prefix every command with zeus. That means
rails serverbecomeszeus server, `rails console...
A jQuery plugin for producing bar charts from tables.
As the title says: this jQuery plugin creates bar charts from HTML tables. It comes in some different flavors.
Check the examples page: http://alphagov.github.com/magna-charta/.
Capybara 2.0 has been released
The gem author Jonas Nicklas highlights in a Google Groups post that the release
- is not backwards compatible to 1.x versions of Capybara
- does not support Ruby 1.8.x anymore
- removes confusion with Rails' built in integration tests (you put capybara rspec integration tests into the
spec/feature/...folder) and the:typemetadata has been changed from:requestto:feature - throws exceptions when trying to interact with an element whose identifier is...
How to set the default monospace font on Xfce (Xubuntu)
While you can set your own font in your terminal or other tools, it will not change the default "Monospace" font that some applications use.
To change that, edit ~/.fonts.conf and add settings for the "monospace" family. Here is how it looks on my machine now:
<fontconfig>
<match target="pattern">
<test qual="any" name="family">
<string>monospace</string>
</test>
<edit name="family" mode="assign">
<string>DejaVu Sans Mono</string>
</edit>
</match>
</fontconfig>
...
Rails: When to use :inverse_of in has_many, has_one or belongs_to associations
When you have two models in a has_many, has_one or belongs_to association, the :inverse_of option in Rails tells ActiveRecord that they're two sides of the same association.
Example with a has_many / belongs_to association:
class Forum < ActiveRecord::Base
has_many :posts, inverse_of: :forum
end
class Post < ActiveRecord::Base
belongs_to :forum, inverse_of: :posts
end
Knowing the other side of the same association Rails can optimize object loading so forum and forum.posts[0].forum will reference the same o...