Search_spinner Cross_grey Magnifier
help

Search syntax

  • Search for repeating cards with repeats:yes
  • Search for unfinished cards with unfinished:yes
  • Search for cards in a certain topic with topic:"name"
  • Search for phrases with "a phrase"
  • Exclude a word from search: -word
  • Exclude a phrase from search: -"a phrase"
  • Exclude a topic from search: -topic:"name"

Delay your Jasmine tests until the document is ready

To delay your entire Jasmine test suite until the DOM is ready, add the following:

beforeAll(function(done) {
  $(done);
});
External content

Official Color Codes for the World's Biggest Brands

brandcolors.net provides you with the colors of the world's biggest brands, easily searchable.

Repeats

Obtain row locks on ActiveRecord objects

When requests arrive at the application servers simultaneously, weird things can happen. Sometimes, this can also happen if a user double-clicks on a button, for example.

This often leads to problems, as two object instances are modified in parallel maybe by different code and one of the requests writes the results to the database.

In case you want to make sure that only one of the requests "wins", i.e. one of the requests is fully executed and completed while the other one at least has to wait for the first request to be completed, you ha…

How to fix: "rake db:rollback" does not work

When you run rake db:rollback and nothing happens, you are probably missing the latest migration file (or have not migrated yet).

$ rake db:rollback
$ 

If that happens to you, check your migration status.

$ rake db:migrate:status
   up     20160503143434  Create users
   up     20160506134137  Create pages
   up     20160517112656  Migrate pages to page versions
   up     20160518112023  ********** NO FILE **********

When you tell Rails to roll back, it tries to roll back the latest change that was mi…

Prevent RubyMine from reformatting pasted code

When you paste copied code with CTRL+V, RubyMine will change the indentation of the pasted code. You can prevent this by pasting with CTRL+ALT+Shift+V instead ("Paste Simple").

To change this behavior entirely, you can open your settings and navigate to Editor / General / Smart Keys. Here you can select one of three options for Reformat on paste:

  • None
  • Indent Each Line (default)
  • Indent Block
  • Reformat Block

You might want to try "Indent Block".

Repeats

MySQL: CONCAT with NULL fields

In MySQL,

CONCAT('foo', 'bar', NULL) = NULL

the NULL always wins.

If you would rather treat NULL as an empty string, use CONCAT_WS (concatenation with separator) instead:

CONCAT_WS('', 'foo', 'bar', NULL) = 'foobar'
Auto-destruct in 56 days

Updated: RSpec: Expecting non-primitive objects as method invocation arguments

Restructured the card and added a lot of new options, including a custom object_having matcher that tests both argument class and method return values:

Test that the argument is an instance of a certain class

expect(object).to receive(:foo).with(instance_of(Person))

Test that the argument responds to certain methods

expect(object).to receive(:foo).with(duck_type(:first_name, :last_name))

Test that the argumen…

Repeats

Don't build randomness into your factories

Tests are about 100% control over UI interaction and your test scenario. Randomness makes writing tests hard. You will also push tests that are green for you today, but red for a colleague tomorrow.

That said, please don't do something like this:

Factory(:document) do |document|
  document.category { ['foo', 'bar', 'baz'].sample }
end

Instead do this:

Factory(:document) do |document|
  document.category 'foo'
end

I even recommend to not use libraries like Faker. Faker makes awesome samp…

How to: Client-side language detection

When you have a localized website, you may want to redirect users to their preferred language when they visit the root path.
Here is how to do it without a server-side component (like a Rails application).

  • Use JavaScript's navigator.language (real browsers and IE11+) and navigator.userLanguage (old IEs).
  • Use a <meta> refresh as fallback
  • Provide buttons for paranoid users that disabled JavaScript and meta refreshs.

JavaScript

The following JavaScript will try to auto-detect a user's preferred language.

It understands string…

Auto-destruct in 51 days

Reminder: There's a script to migrate from Modularity 1 to 2

If you have been using Modularity 1 with the old does syntax, the Modularity 2 gem provides a script to migrate your Ruby project automatically

  1. Make sure your project has tests and you have a backup of your files (or pushed your commits to Git)

  2. Install Modularity 2+

  3. From your project directory, do this:
     find . -name "*.rb" | migrate-modularity1-to-modularity2
    
  4. The script will rename your files and change your code. It will also syntax-check your files after conversion
    (since the script is not perfect).

  5. Ch…
Repeats

How to explain SQL statements via ActiveRecord

ActiveRecord offers an explain method similar to using EXPLAIN SQL statements on the database.

However, this approach will explain all queries for the given scope which may include joins or includes.

Output will resemble your database's EXPLAIN style. For example, it looks like this on MySQL:

User.where(id: 1).includes(:articles).explain

```
EXPLAIN for: SELECT users.* FROM users  WHERE users.id = 1
+—-+————-+——-+——-+—————+
| id | select_type | table | type  | possible_keys |
+—-+—–…

External contentRepeats

Rails 3+: strip_heredoc (String) - APIdock

Note: If you're on Ruby 2.3+ there's a <<~ operator to automatically unindent HEREDOCs.


Copied from the documentation:


strip_heredoc() public

Strips indentation in heredocs.

For example in

if options[:usage]
  puts <<-USAGE.strip_heredoc
    This command does such and such.

    Supported options are:
      -h         This message
      ...
  USAGE
end

the user…

External content

jQuery Core 3.0 Upgrade Guide | jQuery

Since jQuery 3 saw it's first release candidate today, the links has a list of (breaking) changes.

Repeats

Tailor Rails error messages for models and attributes

Rails has generic error messages you can define in your locale .yml files. You may override those application-wide error messages using model or attribute scope like this:

en:
  activerecord:
    errors:
      messages:
        invalid: is invalid # used for any invalid attribute in the application
      models:
        car:
          invalid: does not work # used for invalid car attributes
          attributes:
            driver:
              invalid: not allowed to drive # used if the car's ...
External content

thoughtbot/fake_stripe: A Stripe fake so that you can avoid hitting Stripe servers in tests.

fake_stripe spins up a local server that acts like Stripe’s and also serves a fake version of Stripe.js, Stripe’s JavaScript library that allows you to collect your customers’ payment information without ever having it touch your servers. It spins up when you run your feature specs, so that you can test your purchase flow without hitting Stripe’s servers or making any external HTTP requests.

We've also had tests actually hitting the testing sandbox of Stripe, which worked OK most of the time (can be flakey).

postgres_ext: additional Rails bindings for PostgreSQL

Adds missing native PostgreSQL data types to ActiveRecord and convenient querying extensions for ActiveRecord and Arel for Rails 4.x

Common table expressions

  • Relation#with
  • Model.from_cte

Arrays

Face.where.contains tags: %w[happy smiling] # Matching faces have both 'happy' and 'smiling' tags
Face.where.overlap tags: %w[happy smiling] # Matching faces have at least one of these tags
Face.where.any tags: 'happy' # Matching faces include the 'happy' tag
Face.where.all tags: 'dunno' # Not documented, try for yourself

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|
  Capybara.stub app_host: "http://#{host}"
end

You can now say:

When I go to the start page
Then I should not see "Home of Foo"

When our host is "foo.example.com"
  And I go to the start page
Then I should see "Home of Foo"

The ho…

FreeBSD pkg can't find any packages

If you're trying to searching or installing packages via pkg the your repository data might be broken. If pkg update shows that your repositories are up to date try:

pkg update -f

Afterwards you should be able to search and install packages again.

3086 cards