Running Rails 2 apps with modern MariaDB SQL server
You might have some trouble running a Rails LTS 2 app with MySQL 5.7.
If you don't want to hack Mysql 5.6 into your modern Ubuntu or use the MySQL sandbox, you might want to try MariaDB 10.x.
MariaDB 10.x should work with both old and new Rails applications.
[Switch to MariaDB](https://makandracards.com/makandra/468343-how-...
Store MySQL passwords for development
On your local system that only hosts non-critical development data and only you have access to, you can store MySQL's root password in your home directory so you can use mysql commands without prompt for passwords, i.e. when doing batch processing.
Of course, this has security implications. The password must be stored in plain text, so this method is out of the question if there's any confidential data in your databases. Assuming diligent screen-locking, an encrypted hard disk and correct permissions set on your credential file, it shoul...
Making httpclient use the operating system's SSL cert store
The httpclient gem comes with a custom SSL cert store.
While an customizable, application-level cert store is great when you need to deal with broken or self-signed certificates, you usually want to use the cert store from the underlying Linux. The Linux cert store is updated periodically while httpclient's cert store goes out of date and will eventually not be able to verify certs.
To use the cert store from the underlying operating system:
client = HTTPClient.new
client.ssl_config.cert_store...
Upgrading a Rails app to Cucumber 3
Upgrade gems
You need to update a lof gems. Make sure you don't have any version constraints in your Gemfile or your bundle update won't do anything!
Upgrade cucumber_priority:
bundle update cucumber_priority
Upgrade spreewald:
bundle update spreewald
Upgrade cucumber_factory:
bundle update cucumber_factory
Upgrade parallel_tests:
bundle update parallel_tests
Even on the latest version, parallel_tests will print some deprecation warnings due to using an older formatter A...
Delegating an instance method to a class method in Ruby
With ActiveSupport you can say:
class Robot
def self.likes_humans?
'Nope.'
end
delegate :likes_humans?, to: :class
end
Robot.likes_humans?
# => 'Nope.'
Robot.new.likes_humans?
# => 'Nope.'
Legacy docs for Font Awesome 4.7
See the attached link for a list of icons in Font Awesome 4.
Icon names work differently in Font Awesome 5, where a name is only valid in combination with a name of the icon set.
VNC browser disappears while typing
We often use the Then console step from spreewald in combination with geordi vnc from geordi to debug tests within a real browser. Sometimes when you type in the browser it suddenly disappears. You will only see a grey screen then.
This will always happen if you press the d key. Press the d key again and the browser will appear again.
Firefox: Focus-sensitive Selenium tests do not work reliably in parallel test execution
This is a problem when using Selenium with Firefox. We recommend using ChromeDriver for your Selenium tests.
Firefox will not trigger focus/blur events when its window is not focused. While this makes sense in standard usage, it breaks in parallel test execution.
Please do not rely on focus events in your tests. The linked card has an example of how to build passing tests that deal with focus/blur events.
Nested Spreewald patiently blocks are now patient
In Spreewald 1.10.4+, nested patiently blocks are now patient.
Here is an example:
patiently do
outer_code
patiently do
inner_code
end
end
On spreewald 1.11.2+ the inner block will wait for the full configured wait time (by default 5 seconds). The outer patiently block would now be out of time, but it will always be retried at least a second time. This behavior allows with_scope to be patient, and it must be patient, as explained below.
In versions 1.10.4 - 1.11.1, inner blocks would keep giving the ou...
PostgreSQL: Upgrading your user to a superuser
Your default postgres user is named like your linux user. That default user has limited access privileges, which can cause issues such as:
- DatabaseCleaner needs to disable foreign key constraints before it can wipe the database.
- Importing a remote dump with geordi
- Asking Postgres to show the storage path of a database
Doing these things without a superuser will show a Postgres error or (in Ruby) raise PG::InsufficientPrivilege.
To do so, the application's PostgreSQL user must be a superuser. ...
How to: Fix incorrect MySQL client library version
Bundler::GemRequireError: There was an error while trying to load the gem 'mysql2'.
Gem Load Error is: Incorrect MySQL client library version! This gem was compiled for 5.5.46 but the client library is 5.6.30.
Same as in Fix "libmysqlclient.so.20: cannot open shared object file: No such file or directory":
gem pristine mysql2
gem pristine re-installs a gem (without re-downloading), re-compiling all native extensions in the process.
How to: Restart vnc server for geordi
Trying to open a vnc window with geordi geordi vnc ended up with this error:
> VNC viewer could not be opened:
vncviewer: ConnectToTcpAddr: connect: Connection refused
Check if vncserver 17 (default geordi session) is running:
ps -aux|grep vnc
If you see Xvnc4 :17 then the server is already running. Kill this server if it is already running.
vncserver -kill :17
Als...
Mysql::Error: BLOB/TEXT column can't have a default value
mysql> SELECT @@global.version;
+------------------+
| @@global.version |
+------------------+
| 5.6.30 |
+------------------+
1 row in set (0,00 sec)
MySQL 5.6 Reference Manual says "BLOB and TEXT columns cannot have DEFAULT values".
If you want to run migrations in development here are two variants which might help. If you are not sure about the side effects (e.g. your application is broken when it doesn't set additional default values on application side, too...
How to: Solve gem loaded specs mutex
Use bundler > 1.15 to fix Gem::LOADED_SPECS_MUTEX (NameError).
Given the following project:
ruby -v
ruby 1.8.7
bundler -v
Bundler version 1.13.7
gem -v
1.8.30
rails -v
Rails 3.2.22.1
Running specs or features resulted in:
uninitialized constant Gem::LOADED_SPECS_MUTEX (NameError)
The previous settings described in Maximum version of Rubygems and Bundler for Ruby 1.8.7 and Rails 2.3 (even the rails version was rails 3.2 and not 2.3) seems not to work here, so I used (also described in the ca...
List of :status symbols for rendering in Rails
When your Rails controller calls render, you can pass a :status option for the HTTP status code:
render 'results', status: 400
All important status codes also have a symbol alias, which makes your code easier to read:
render 'results', status: :bad_request
Attached is a list of available symbol values for :status.
How to stub class constants in RSpec
Hint: There's another card with this helper for Cucumber features.
Sometimes you feel like you need to stub some CONSTANT you have defined in an other class. Since actually constants are called constants because they're constant, there's no way to easily stub a constant.
Here are three solutions for you.
Easiest solution
Rethink! Do you really need CONSTANT = %w[foo bar] to be constant? In many cases, setting it as a...
How to control Chromedriver using curl
Here is how to use Chromedriver without libraries like selenium-webdriver. This can be useful for debugging.
The following example visits a web page and reads the a headline's text contents.
-
Create a session. You will get a JSON response containing lots of information about your Chrome session, including a
sessionId. Use that to send any future commands to your chromedriver session.$ curl -XPOST http://localhost:9515/session -d '{"desiredCapabilities":{"browserName":"chrome"}}' {"sessionId":"your-session-id-here","sta...
Ruby: Removing leading whitespace from HEREDOCs
If you're on Ruby 2.3+ there's a <<~ operator to automatically unindent HEREDOCs:
str = <<~MESSAGE
Hello Universe!
This is me.
Bye!
MESSAGE
If you have an older Ruby, you can use the String#strip_heredoc method from ActiveSupport. See Summarizing heredoc in ruby and rails for an example.
Technically...
Chrome: Making high-resolution website screenshots without add-ons
If you want to make a screenshot of a website that works well in print or on a high-DPI screen (like Apple Retina displays), here is how you can capture a high-resolution screenshot.
You can do this without an addon:
- Open the website
- If you have multiple monitoros:
- Resize the Chrome window so it covers multiple monitors (in Linux you can hold ALT and resize by dragging with the right mouse button)
- Zoom into the page using
CTRL +andCTRL -so it covers most of the window area. Leave a little padding on the left and right so...
RSpec expects pending tests to fail
When flagging a spec that will be implemented later as pending, include a failing spec body or RSpec 3 will consider the pending test as failing.
The reasoning is: If the spec is flagged as pending but passes, it should not be pending. So these will fail:
it 'does something' do
pending
end
it 'does something else' do
pending
expect(1).to eq(1)
end
The first case may be unexpected, if you just wanted to write down that something should eventually happen that will be implemented later.
Instead, ...
Heads up! Years are always floats in Rails < 4
Watch out when saying something like 1.year in Rails. The result is not a Fixnum and can cause unexpected errors when the receiving end expects a Fixnum.
While anything from seconds to months are Fixnums, a year is a Float in Rails -- when called on a Fixnum itself:
>> 10.seconds.class
=> Fixnum
>> 2.minutes.class
=> Fixnum
>> 24.hours.class
=> Fixnum
>> 28.days.class
=> Fixnum
>> 9.months.class
=> Fixnum
>> 1.year.class
=> Float # Boom.
While they are [technically correct](http:...
Centering with CSS vertically and horizontally (all options overview)
There are a million ways to center <div>s or text in CSS, horizontally or vertically. All the ways are unsatisfying in their own unique way, or have restrictions regarding browser support, element sizes, etc.
Here are two great resources for finding the best way of aligning something given your use case and browser support requirements:
- How to center in CSS
-
A long form that lets you define your use case and browser support requirements, then shows you the preferred way of aligning.
[Centering CSS: A co...
Think twice before using application names in your variables and methods
I recently had fun with replacing a custom video plattform ("mycustomtv") in one of our applications. I learned a lot about naming stuff.
youtube_mycustomtv_chooser is a bad name for a directive.
Why?
- if you add, for example, other video systems like vimeo, the name of the method either lies or will get longer.
- if you replace one system, you will have to change the method and all of its occurrences (and you're very lucky if it is only one poorly named thing in your application)
What would be a better solution?
Abstract fro...
Katapult 0.4.0 released
Features
- Generating a project README
- Finally: Support for modelling associations between models in your application model! Example:
# lib/katapult/application_model.rb
model 'Cart' do |cart|
cart.belongs_to 'User'
end
crud 'User' do |user|
user.attr :fullname
end