3864 cards
View

How to fix: WrongScopeError when using rspec_rails with Rails 6.1

tl;dr: Upgrade the gem to at least 4.0.1

When you use rspec_rails in a version < 4 with Rails 6.1 you may encounter an error like this:

Failure/Error:
  raise WrongScopeError,
    "`#{name}` is not available from within an example (e.g. an " \
    "`it` block) or from constructs that run in the scope of an " \
    "example (e.g. `before`, `let`, etc). It is only available " \
    "on an example group (e.g. a `describe` or `context` block)."
    `name` is not available from within an example (e.g. an `it` block) or from constructs that...
Linked content

Best practices for REST API design

A rough guide how to implement a REST API.


The discussion here includes some interesting points as well:

  • Timestamps: ISO8601 format ("2021-02-22T20:34:53.686Z")
  • Google API guideline: https://google.aip.dev/
  • Numbers: String vs. Number

    The JSON number type is not a double. It's just a number of arbitrary size and precision in integer/decimal/E format that can be parsed as whatever the parser finds fitting.

  • Pagination: Limit + Offset vs. Object ID / Pointer vs. System-Version...

Running old ImageMagick versions in a Docker container

If your project depends on an old version of ImageMagick that you can no longer install in your system, you can choose the run an old ImageMagick in a Docker container.

Dockerized ImageMagick commands will only work with absolute path arguments. You need to boot a corresponding docker container once before using it.

Setting up Docker

If you haven't installed Docker yet, use our guide or the [official instructions](https://docs.docker.com/get-started/...

Repeats

ActiveRecord: Creating many records works faster in a transaction

When you need to insert many records into the same table, performance may become an issue.

What you can do to save time is to open a transaction and save multiple records within that transaction:

transaction do
  500.times { Model.create! }
end

Although you will still trigger 500 INSERT statements, they will complete considerably faster.

When I tried it out with a simple model and 500 iterations, the loop completed in 1.5 seconds vs. 6 seconds without a transaction.

Alternative

Another fast way to insert many...

Repeats

Using ActiveRecord with threads might use more database connections than you think

Database connections are not thread-safe. That's why ActiveRecord uses a separate database connection for each thread.


For instance, the following code requires three database connections:

3.times do
  Thread.new do
    User.first # first database access makes a new connection
  end
end

Unfortunately ActiveRecord never returns these connections after the thread finishes, unless you tell it to. Since the number of connections is limited, you will eventually run out of connections. In that case, the next thread t...

Repeats

Customizing 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 ...
Repeats

Parallelize Development Using Git Worktrees

You can use git worktree to manage multiple working trees attached to the same repository. But why should I use git worktree?

You can use more than one working tree to ...

... run tests while working on another branch
... compare multiple versions
... work on a different branch without disturbing your current branch

Creating a new working tree is as simple as creating a new branch. You only need to execute git worktree add <path> <branch>. When you are done, you can remove the working tree with git worktree remove <Worktree>...

Linked contentAuto-destruct in 48 days

Updated: Inspecting a live Ruby process

Added another method that seems to work on Ruby 1.8.7 and Ruby 2.6.

Linked contentAuto-destruct in 41 days

Updated: Running Capybara tests in headless Chrome

Note: In case you are using geordi, it makes sense to disable its VNC wrapper around integration tests. Therefore create a file named .geordi.yml in the project root with the following content:

use_vnc: false
Linked contentAuto-destruct in 38 days

Updated: Some tips for upgrading Bootstrap from 3 to 4

Restructured the whole card and add some more suggestions for external libraries:

Here are some libraries that I had come across:

Select2

  • you have to use at least version 4 to work with Bootstrap 4

Suggestion: Upgrade to a newer version before doing the Bootstrap upgrade and test if everything still works as expected. Here is a migration guide for Select2
Maybe you can find an alternative to Select2 if you want to go away from jQuery.
...

Linked contentRepeats

What is a reduction and why Fibers are the answer for Ruby concurrency | julik live

The linked article provides a good overview of the various concurrency primitives in Ruby, and what's changing in Ruby 3.

Repeats

Regular Expressions: Quantifier modes

When you repeat a subpattern with a *, + or {...} operator, you may choose between greedy, lazy and possessive modes.

Switching modes may affect the result and performance of your regular expressions. In the worst case, an ill-suited mode may make your regular expression so slow that it can DoS your application.

Greedy quantifiers (default)

A plain * or + is greedy. It will make the longest possible match for the subpattern it repeats. Only when the rest of the expres...

Linked contentDeprecated

Make your Rails console (and irb) output better readable

Pour color on your Rails console with awesome_print. Turn confusing long strings into formatted output. Have objects and classes laid out clearly whenever you need it.

Put gem 'awesome_print', :group => :development into your Gemfile. Now on the Rails console you have the command ap that will give you a colored, formatted output of whatever you pass it. See the example output of the User class below.

For customization visit the repository on Github.

![awesome_print.png](https://makan...

Repeats

Getter and setter functions for JavaScript properties

JavaScript objects can have getter and setter functions that are called when a property is read from or written to.

For example, if you'd like an object that has a virtual person.fullName attribute that dynamically composes person.firstName and person.lastName:

var person = {

  firstName: 'Guybrush',

  lastName: 'Threepwood',

  get fullName() {
    return this.firstName + " " + this.lastName;
  },
  
  set fullName(name) {
    var parts = name.split(" ");
    this.firstName = parts[0];
    this.lastName = parts[1];
  }

};
```...
Repeats

Raising JavaScript errors in Cucumber scenarios and how to silence them for particular scenarios

Raising JavaScript errors in Cucumber

With selenium JavaScript errors do not cause any failure in your cucumber scenarios. To archive a failure we can use the the Ruby bindings to Selenium Webdriver expose the Webdriver logging API introduced in version 2.38 .

The following step can be used:

Then /^there should be no JavaScript errors$/ do
  if alert_present?
    # Chrome 54 and/or Chromedriver 2.24 introduced a breaking change on how
    # accessing browser logs work.
    #
    # Apparently, while an alert/confirm is open, Chrome...
Repeats

How to list updateable dependencies with Bundler and Yarn

Bundler

bundle outdated [--filter-major|--filter-minor|--filter-patch]

Example output for bundle outdated --filter-major

Image

Yarn

yarn outdated

Example output

Image

Linked contentAuto-destruct in 31 days

Updated: Using ES6 without a transpiler

Uglifier can't minify some ES6 language constructs, even with harmony: true.

In these cases you can use terser-ruby instead.

Repeats

Insomnia helps you querying your API

Insomnia is a GUI tool to help you communicating with an API. There are also other tools to do this, e.g. Postman or or the command line tool cURL.

While it is quite similar to Postman, I found the UI to be less cluttered and therefore easier to use.

Image

The usage is almost self explanatory.

You can install i...

This website uses short-lived cookies to improve usability.
Accept or learn more