RSpec & Devise: How to sign in users in request specs

You know that Devise offers RSpec test helpers for controller specs. However, in request specs, they will not work.

Here is a solution for request specs, adapted from the Devise wiki. We will simply use Warden's test helpers -- you probably already load them for your Cucumber tests.

First, we define sign_in and sign_out methods. These will behave just like ...

You can now override all Spreewald steps with more specific versions

You can now define this step without Cucumber raising Cucumber::Ambiguous:

Then /^I should see "whatever I want"$/ do
  ...
end

This is available in Spreewald 1.5.0+.

Override Cucumber steps without an ambiguity error

Cucumber raises a Cucumber::Ambiguous if more than one step definitions match a step.

Our new cucumber_priority gem provides a way to mark step definitions as overridable, meaning that they can always be overshadowed by a more specific version without raising an error.

This gem is currently used by spreewald and cucumber_factory.

Marking step definiti...

Spreewald: Click on an element with a CSS selector

Spreewald 1.4.0 comes with this step:

When I click on the element ".sidebar"

We recommend to define a selector_for method in features/support/selectors.rb so you can refer to the selector in plain English:

When I click on the element for the sidebar

Defining and calling lambdas or procs (Ruby)

Ruby has the class Proc which encapsulates a "block of code". There are 2 "flavors" of Procs:

  • Those with "block semantics", called blocks or confusingly sometimes also procs
  • Those with "method semantics", called lambdas

lambdas

They behave like Ruby method definitions:

  • They are strict about their arguments.
  • return means "exit the lambda"

How to define a lambda

  1. With the lambda keyword

    test = lambda do |arg|
      puts arg
    end
    
  2. With the lambda literal -> (since Ruby 1.9.1)
    ...

Lazy-loading images

Note

This card does not reflect the current state of lazy loading technologies. The native lazy attribute could be used, which is supported by all major browsers since 2022.

Since images are magnitudes larger in file size than text (HTML, CSS, Javascript) is, loading the images of a large web page takes a significant amount of the total load time. When your internet connection is good, this is usually not an issue. However, users with limited bandwidth (i.e. on mobile) need to mine their data budget...

PostgreSQL: Expanded display and other command line features

One useful postgres command I stumbled upon recently was \x. It gives you an expanded display which allows you to actually read the results of your select * from queries. The link below describes a few more useful techniques and commands.

About Ruby's conversion method pairs

Ruby has a set of methods to convert an object to another representation. Most of them come in explicit and implicit flavor.

explicit implicit
to_a to_ary
to_h to_hash
to_s to_str
to_i to_int

There may be even more.

Don't name your methods like the implicit version (most prominently to_hash) but the like the explicit one.

Explicit conversion

Explicit conversion happens when requesting it, e.g. with the splat opera...

A case for Redactor

Redactor is yet another WYSIWYG editor. It definitely has its weak points, but I want to point out that it has clear strengths, too.

Pro

  • Simple and beautiful interface.
  • Outstandingly organized source code. Have never seen a JS library that was this structured.
  • Clear, comprehensive and searchable API documentation. Filled with code examples.
  • Easily customizable: specify toolbar buttons, pass various callbacks, etc.
  • Features a collection of great [plugins](ht...

Heads up: Ruby implicitly converts a hash to keyword arguments

When a method has keyword arguments, Ruby offers implicit conversion of a Hash argument into keyword arguments. This conversion is performed by calling to_hash on the last argument to that method, before assigning optional arguments. If to_hash returns an instance of Hash, the hash is taken as keyword arguments to that method.

Iss...

Recommended Git workflow for feature branches

This is a guide on how to effectively use Git when working on a feature branch. It is designed to get out of your way as much as possible while you work, and ensure you end up with clean commits in the end.

We assume you are the only person working on this branch. We also assume the branch has never been "partially" merged into master.

You want to start a feature branch

git checkout master
git checkout -b my-feature-branch
git push -u origin my-feature-branch

You've added code that works ind...

pgAdmin has a "graphical EXPLAIN" feature

When working with PostgreSQL, you can use pgAdmin as a GUI.
While you can do most things just like on an SQL console, you can use it to display EXPLAIN results in a more human-readable way.


(image from the Postgres manual)

  1. Open up pgAdmin, connect to your server
  2. Pick a database from the left pane
  3. Click the "SQL" icon in the toolbar, or press Ctrl+E to open the query tool.
  4. Paste any queries that you'd like to explain.
  5. Go to "Query" → "Explain analyze", or ...

Rarely say yes to feature requests

A fantastic guide for a dilemma facing any web-based product.

Here’s a simple set of Yes/No questions that you can quickly answer before you add another item to your product roadmap.

Saying yes to a feature request – whether it’s a to an existing customer, a product enquiry, a teammate, or a manager – is immediately rewarding. It’s an unspoken transaction where you barter long term product focus in exchange for short term satisfaction. Buying short term joy for the cost of long term pain is the human condition.

  1. Does it fit your ...

Improving browser rendering performance

As the web is being used for more and more tasks, expectations rise. Not only should web pages offer rich interaction, they must be responsive in both size and interaction.

This imposes a paradoxon that needs to be solved by building performing applications. It's not enough any more to have your web site do crazy stuff, it is also required to do it crazy fast. This card is intended to give you an introduction to this emerging aspect of web development.

Read this introductory [performance study on Pinterest](http://www.smashingmagazine.com/...

httpclient: A Ruby HTTP client for serious business

While debugging an intricate issue with failed HTTP requests I have come to appreciate the more advanced features of the httpclient Rubygem.

The gem is much more than a lightweight wrapper around Ruby's net/http. In particular:

  • A single HTTPClient instance can re-use persistent connections across threads in a thread-safe way.
  • Has a custom and configurable SSL certificate store (which you probably want to disable by default...

Project management best practices: User stories & Issues

We organize our daily work with issues in our Linear workspace.

Issue format

A good issue needs to be precise. It should be very clear what is part of an issue, and what is not. If there are different expectations between the person who writes and who implements an issue, there will be rejects.

To this end, we use a consistent format for issues that looks like this:

Issue: Autocomplete

As a journalist, I want to have an autocomplete in the search bar, to have a more efficient way to find articles.

Acceptance criteri...

Installing Node.js / npm under Ubuntu with nvm (with yarn)

I recommend install Node.js using nvm. This way you can have multiple Node versions in your ~/.nvm. You also won't need to install global packages with sudo anymore.

Node via nvm will automatically bring npm. yarn will automatically be available if corepack is enabled for node.

Installing nvm

DigitalOcean has a HOWTO for installing nvm on Ubuntu (16.04, [18.04](https://www.digitalocean.com/community/tutorials/how-to-...

natritmeyer/site_prism

SitePrism gives you a simple, clean and semantic DSL for describing your site using the Page Object Model pattern, for use with Capybara in automated acceptance testing.

The Page Object Model is a test automation pattern that aims to create an abstraction of your site's user interface that can be used in tests. The most common way to do this is to model each page as a class, and to then use instances of those classes in your tests.

If a class represents a page then each element of the page is represented by a method that, when cal...

An auto-mapper for ARIA labels and BEM classes in Cucumber selectors

Spreewald comes with a selector_for helper that matches an English term like the user's profile into a CSS selector. This is useful for steps that refer to a particular section of the page, like the following:

Then I should see "Bruce" within the user's profile
                                 ^^^^^^^^^^^^^^^^^^

If you're too lazy to manually translate English to a CSS selector by adding a line to features/env/selectors.rb, we already have an [auto-mapper to translate English into ...

Using tig

tig is a command line explorer for Git that is just awesome. Install via apt-get or brew.

Handy commands

  • t ("tree"): Directory-structure based access. You'll see the current directory annotated with the latest change date and its author. Navigate with arrow keys or vim.
  • b ("blame"): Opens the file under the cursor and annotates each line with change date and author.
  • d ("diff"): Like ENTER on a commit, but arrow keys will scroll the diff!
  • /: Search current view (e.g. commit list, diff). Jump to next hit with n....

Getting Sidekiq error "delay is defined by Active Record"

Reason: You very likely have a model that has a delay attribute.

You can configure Sidekiq to remove its delay method by adding this to your Sidekiq initializer:

Sidekiq.remove_delay!

If you need to keep Sidekiqs delay features, add Sidekiq.hook_rails! before the option above. The sidekiq methods will be prefixed with sidekiq_ then.

Bootswatch: Paper

Free Bootstrap theme resembling Material Design.

Bootswatch offers Sass and Less files, so the theme can easily be integrated into your usual Rails application.

Implements only Bootstrap features which means that some Material stuff is missing, but also that you can easily use or replace the theme.
Does not come with extra JavaScript; some effects like button click ripples are implemented via CSS.

Also check out their other themes which can be used in a similar fashion.

List of Helpful RubyMine Shortcuts

Navigation

CTRL + SHIFT + ALT + N

Search for any symbol in your application, like CSS classes, Ruby classes, methods, helpers etc.

CTRL + SHIFT + N

Search for filename in your application (also dependencies)

CTRL + E

Open a list of recently opened files

ALT + POS1

Open a the navigation bar as a context menu. Allows you to quickly navigate between files.

CTRL + G

Go to line

Actions

CTRL + SHIFT + A

:...

Spreewald 1.2.11 fixes frequent "no alert open" errors on Chrome

When running capybara with Chrome you might start seeing frequent "no alert open" errors when trying to interact with browser dialogs. This seems to be triggered by a recent Chrome update.

Apparently these dialogs no longer appear instantly. Spreewald 1.2.11 fixes the "When I confirm the browser dialog" and similar steps by waiting for the dialog to appear.