Multiline comments in indented Sass syntax

Write a // and indent every subsequent line by two spaces.

This is great for documenting BEM blocks!

//
  An action button
  ================
  
  Basic usage
  -----------
  
      <a href="/path" class="action">New booking</a>
      <button class="action">Save</a>
      <input type="submit" class="action">Save</a>
  
  Colors
  -------
  
      <a href="/path" class="action is-red">Primary</a>
      <a href="/path" class="action is-grey">Secondary</a>
  
  Small inline buttons
  --------------------
  
      <p>
        Recor...

FactoryBot: How to get the class from a factory name

When you want to look up a class for a given factory, do it like this:

>> FactoryBot.factories.find('admin').build_class
=> User

In older versions, you could do:

>> FactoryBot.factory_by_name('admin').build_class
=> User

Speed up file downloads with Rails, Apache and X-Sendfile

When you use the send_file method to send a local file to the browser, you can save resources on the application server by setting the :x_sendfile option to true. This option is activated by default for Rails 3, so you need to understand this.

What this option does is not to send any data at all, but rather set the local file path as a new response header:

X-Sendfile: /opt/www/awesome-project/shared/downloads/image.png

When the response comes back from Rails to...

Ruby: Enumerable#partition

If you want to sort values from an enumerable into two arrays based on whether they match a certain criteria or not, Enumerable#partition can come in handy.

# Enumerable#partition returns two arrays, 
# the first containing the elements of enum 
# for which the block evaluates to true, 
# the second containing the rest.

(1..6).partition { |v| n.even? }  #=> [[2, 4, 6], [1, 3, 5]]

Works well with destructuring assignment, too.

even, odd = (1..6).partition { |n| n.even? ...

SQL: Find out number of rows of all tables within a MySQL database

Here you are:

SELECT table_name, table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'your_database' order by table_rows;

Sharing cookies across subdomains with Rails 3

To achieve this goal you have to setup the session store like the following example:

  MyApp::Application.config.session_store(
    :cookie_store,
    {
      :key => '_myapp_session',
      :domain => :all, # :all defaults to da tld length of 1, '.web' has length of 1
      :tld_length => 2 # Top Level Domain (tld) length -> '*.myapp.web' has a length of 2
    }
  )

The invconvenient side effect for local development

… or: Why do I get "Can't verify CSRF token authenticity" even if csrf token is present?

As `:domain => :all...

Remove URLs from the Google index

Obviously, you only can do this for your own sites.
You need to authenticate a domain you want to remove content from via Webmaster Tools.

To remove a URL:

  1. Open Webmaster Tools
  2. Select the respective site from the list of domains under your control
  3. Choose "Google Index" from the menu left
  4. Click "Remove URL"

Carrierwave: Efficiently converting images

When uploading images, adding more than one process to a version can cause MiniMagick to run multiple commands. In order to have all processing done in one mogrify system call, you'll need to define only one process that combines all options you'd like to pass in.

An auto-mapper for BEM classes in Cucumber selectors

When you are using the #selector_for helper in Cucumber steps, as e.g. Spreewald does, the following snippet will save you typing. It recognizes a prose BEM-style selector and maps it to the corresponding BEM class.

For a variation on this idea, see An auto-mapper for ARIA labels and BEM classes in Cucumber selectors.

Examples

"the main menu" -> '.main-menu'
"the item box's header" -> '.item-box--header'

Here are some examples of steps (using Spreewald, too):

T...

Ruby: Counting occurrences of an item in an array / enumerable

Enumerable#count can do three things.

  • With no argument provided, it returns the number of items.
  • With an argument, it returns the number of items matching the given value.
  • With a block, it counts the number of elements yielding a truthy value.
ary = [1, 2, 4, 2]
ary.count #=> 4
ary.count(2) #=> 2
ary.count { |x| x % 2 == 0 } #=> 3

Ruby: Flatten arrays by only one level

Array#flatten by default flattens an array recursively. To only flatten the array for e.g. one level, it takes an optional argument.

# Flattens the array recursively
>> [1, [2, [3]]].flatten
=> [1, 2, 3]

# Flattens the array the given number of times
>> [1, [2, [3]]].flatten(1)
=> [1, 2, [3]]

Fix error UDPSocket.open: wrong number of arguments (0 for 1)

I got the following error after updating the selenium-webdriver gem:

wrong number of arguments (0 for 1) (ArgumentError)
/home/pointoo-dev/.rvm/gems/ruby-1.8.7-p374/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/platform.rb:183:in `open'
/home/pointoo-dev/.rvm/gems/ruby-1.8.7-p374/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/platform.rb:183:in `ip'
/home/pointoo-dev/.rvm/gems/ruby-1.8.7-p374/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/common/platform.rb:196:in `interfaces'

It was caused...