Allow setting the #id attribute when creating an ActiveRecord
When creating an ActiveRecord with .new
, .create
or create!
, you cannot set the ID attribute (note: When using Machinist's .make
you can).
This is because even when you are not using attr_protected
or attr_accessible
, some attributes are always protected. These attributes are #id
and #type
.
If you want to allow setting #id
on .new
, .create
or create!
you can include the attached module in order to whitelist #id
on a model of your choice like this:
class MyModel <...
CSS: Matching against attributes and their values (or parts of them)
You probably know that you can use CSS selectors to match against elements and their attributes, such as:
a[title] { /* any <a> that has a "title" */ }
a[data-fancy="true"] { /* any <a> that has their "data-fancy" attribute set to "true" */ }
But there is more: You do not need to match against "full" attribute values but can match against parts of them.
They work in all somewhat modern browsers, and IE9 or later.
Match variants
Exact match (CSS2)
[foo="bar"]
(matches `<div foo="b...
CSS: Set content from other attributes
You can use the content
CSS attribute to set an element's content -- which is especially useful for the :before
and :after
pseudo elements:
a:before {
content: 'Click me: ';
}
The above example would prepend "Click me:" to any link on the page.
Note that you can also refer the contents of other attributes of the element. So, if your links have a helpful title
set, you could do this:
a:before {
content: attr(title) ": ";
}
There also is a jsFiddle for the examp...
Regex: Be careful when trying to match the start and/or end of a text
Ruby has two different ways to match the start and the end of a text:
-
^
(Start of line) and$
(End of line) -
\A
(Start of string) and\z
(End of string)
Most often you want to use \A and \z.
Here is a short example in which we want to validate the content type of a file attachment. Normally we would not expect content_type_1
to be a valid content type with the used regular expression image\/(jpeg|png)
. But as ^
and $
will match lines, it matches both content_type_1
and content_type_2
. Using \A
and \z
will wo...
How to test your website for different versions of Internet Explorer or Edge
Virtualization
Microsoft provides virtual machines for different Internet Explorer versions.
The images are available for various virtualization solutions, including VirtualBox.
BrowserStack
For a monthly fee browserstack.com provides access to various versions of Windows and Internet Explorer through your web browser. It's pretty convenient.
By installing a Chrome addon you can ...
Test that a form field is visible with Cucumber/Capybara
Spreewald now comes with a step that tests if a form field is visible:
Then the "Due date" field should be visible
But the "Author" field should not be visible
The step works by looking up the field for the given label, then checks if that field is hidden via CSS (or Javascript).
It is not currently tested if the label is visible or hidden. For this see: [Check that an element is visible or hidden via CSS with Cucumber/Capybara](https://makandracards.com/makandra/1049-check-that-an-elem...
Consul: Querying a power that might be nil
Consul 0.6.1+ gives your Power
class a number of static methods that behave neutrally in case Power.current
is nil
. This allows you to create authorization-aware models that still work when there is no user at the end of a web browser, e.g. on the console, during tests or during batch processes.
You will often want to access Power.current
from another model, to e.g. iterate through the list of accessible users:
class UserReport
def data
Power.current.users.c...
Memcache: Your cache node may degenerate over time, check your settings
We recently had a problem on a Memcache cluster, where one of the servers showed a significantly worse cache hit rate and a lot more evictions.
It turned out that the only reason was that the server was running for a few months longer than the others. Some investigation showed this to be a known problem with Memcache: Once your cache gets full, it might be "hardwired" for your specific usage patterns. If those change (and you for example start to store larger values), memory is no longer allocated optimally, in extreme cases Memcache might ...
How to update a single gem conservatively
The problem
Calling bundle update GEMNAME
will update a lot more gems than you think. E.g. when you do this:
bundle update cucumber-rails
... you might think this will only update cucumber-rails
. But it actually updates cucumber-rails and all of its dependencies. This will explode in your face when one of these dependencies release a new version with breaking API changes. Which is all the time.
In the example above updating cucumber-rails
will give you Capybara 2.0 (because capybara
is a dependency of `cucumber-rail...
Running "bundle update" without arguments might break your application
Calling bundle update
(without arguments) updates all your gems at once. Given that many gems don't care about stable APIs, this might break your application in a million ways.
To stay sane, update your gems using the applicable way below:
Projects in active development
Update the entire bundle regularily (e.g. once a week). This ensures that your libraries are up-to-date while it's easy to spot major version bumps which may break the app.
Projects that have not been updated in a while
- [Update a single gem conservatively](htt...
Error: "sylogd: /var/log/authlog : no such file or directory" but the file exists
If you get a message like this:
Jan 21 13:42:38 foobar syslogd: /var/log/authlog : no such file or directory
But you are sure the file exists and it have the correct permissions:
# example for Solaris 9
-rw-r--r-- 1 root sys 8,0K 21. Jan 13:51 /var/log/authlog
Then you perhaps have a trailing whitespace after /var/log/authlog
in the /etc/syslog.conf
.
Note: Use TAB to separate log components from log file names in Solaris. Because spaces do not work.
Using Apache Benchmark (ab) on sites with authentication
Apache HTTP server benchmarking tool (ab
) is a nice tool to test performance on sites delivered by HTTP. If the site you're about to test is placed behind a login, follow these steps to successfully use ab
on it.
- Open the site to test in the browser of your choice. Do not login yet.
- Use developer tools to show all cookies used by the site. (Chrome: Ctrl+Shift+i, open the 'Resources' tab and click on the site below 'Cookies' on the left. Firefox: Right-click on the site, open 'We...
CSS Explain - A tool which calculates CSS selector specificity
Example input:
li.active a:link
Example output (specificity):
| 0 | 2 | 2 |
See also: https://www.codecaptain.io/tools/css-specificity-calculator
Compare two jQuery objects for equality
Every time you call $(...)
jQuery will create a new object. Because of this, comparing two jQuery collections with ==
will never return true, even when they are wrapping the same native DOM elements:
$('body') == $('body') // false
In order to test if two jQuery objects refer to the same native DOM elements, use is
:
var $a = $('body');
var $b = $('body');
$a.is($b); // true
Jasmine equality matcher for jQuery
See [here](/makandra/34925-jasmine-testing-complex-types-for-e...
The Shapes of CSS
Examples how to create dozens of shapes using pure CSS and a single HTML element.
Git blame: How to ignore white-space modifications
When doing a git blame
, git will blame the person who added or removed white space in a line (e.g. by indenting), not the person who originally wrote the code.
Say git blame -w
to ignore such white-space changes. You want this. \
Note that you can also use it when diffing: git diff -w
.
Example
Consider this method, created by a user in commit d47bf443
:
def hello
'world'
end
^
$ git blame foo
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 1) def hello
d47bf443 (Arne Hartherz 2012-12-19 14:44:38 +0100 2...
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...
Helper methods - RSpec Core
You can define methods in any example group using Ruby's def
keyword or define_method
method. These helper methods are exposed to examples in the group in which they are defined and groups nested within that group, but not parent or sibling groups.
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 = :transaction
or `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...