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...
A rough guide how to implement a REST API.
The discussion here includes some interesting points as well:
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.
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.
If you haven't installed Docker yet, use our guide or the [official instructions](https://docs.docker.com/get-started/...
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.
Another fast way to insert many...
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...
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 ...
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>
...
Added another method that seems to work on Ruby 1.8.7 and Ruby 2.6.
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
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 fromjQuery
.
...
The linked article provides a good overview of the various concurrency primitives in Ruby, and what's changing in Ruby 3.
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.
A plain *
or +
is greedy. It will make the longest possible match for the subpattern it repeats. Only when the rest of the expres...
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.
 {
return this.firstName + " " + this.lastName;
},
set fullName(name) {
var parts = name.split(" ");
this.firstName = parts[0];
this.lastName = parts[1];
}
};
```...
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...
bundle outdated [--filter-major|--filter-minor|--filter-patch]
bundle outdated --filter-major
yarn outdated
Uglifier can't minify some ES6 language constructs, even with harmony: true
.
In these cases you can use terser-ruby instead.
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.
The usage is almost self explanatory.
You can install i...