Capistrano 2: How to deploy a single server
When you have a multi-server setup, you'll be adding a new server from time to time. Before doing a full deploy, you might want to test that server in an isolated deploy. There is a single way to do this: the HOSTFILTER
env variable.
Commenting out "server" lines in the Capistrano deploy config will raise a Capistrano::NoMatchingServersError
with <task> is only run for servers matching {:roles=> <role>}, but no servers matched
. Instead, specify the server-under-test like this:
HOSTFILTER=separate-sidekiq.makandra.de cap productio...
Ubuntu: keyring password won't change with user password
Ubuntu might create several keyrings for you. Note that keyring passwords are initially set to the user password but do not change with it. If you can't seem to find the correct password for a keyring, try entering the inital password.
If you still can't find it, you might have to delete that keyring:
- open "Passwords and Keys" (seahorse application)
- right-click on the keyring you want to change
- press
Delete
Testing webpages globally (as in "around the globe")
These tools help you in checking websites globally:
- DNS Checker
-
This tool allows for global DNS propagation checking.
- GeoScreenshot
-
This tool takes screenshots of a given URL from various locations across the world.
How to define height of a div as percentage of its variable width
This is useful if, for example, you want to use a background-image that has to scale with the width and the div should only have the height of the picture.
html:
<div class="outer">
<div class="inner">
</div>
</div>
css:
.outer {
width: 100%;
background-image: image-url('background.png');
background-size: cover;
}
.inner {
padding-top: 60%;
}
How does it work?
There are several CSS attributes that can handle values as percentage. But they use different other attributes as "reference value...
Webmock's hash_including doesn't parse query values to string
Webmocks hash_including
is similar to RSpec::Mocks::ArgumentMatchers#hash_including
. Be aware that hash_including
(webmock v3.0.1) doesn't parse integer values to String.
Without hash including you would say:
uri = URI('http://example.com/?foo=1&bar=2')
stub_request(:get, 'example.com').with(query: {foo: 1, bar: 2})
Net::HTTP.get(uri) # ===> Success
If you only want to check if foo
is present you can use hash_including
:
uri = URI('http://example.com/?foo=1&bar=2')
stub_request(:get, 'example.com').with(query: hash_i...
Listing all gems on a private gem server
You can use gem list
to list all gems available from a remote gem server:
gem list -r --clear-sources -s 'https://user:password@gemserver.tld/'
This is useful to debug cases where Bundler complains of a gem existing in more than one gem source.
Standard Gems
Ruby's standard library is in the process of being gemified. It will soon - Ruby 2.5 - consist of RubyGems, which can be updated independently from Ruby.
This might mean smoother Ruby upgrades in the future. If breaking API changes happen in standard gems, we can update these before upgrading Ruby.
HTTP/2 push is tougher than I thought - JakeArchibald.com
TLDR: Browser implementations of HTTP/2 push are horrible. You might end up with worse performance than without pushing. However, the article includes a great explanation of how HTTP/2 push are supposed to integrate with browser APIs.
RSpec: Stubbing a method that takes a block
If you stub
a method or set expectations with should_receive
these stubbed methods may also yield blocks. This is handy if the returning object is receiving a block call.
Consider this, where you cannot say and_return []
because of the block:
def crawl_messages
Message.find_in_batches do |messages|
messages.each(&:crawl)
end
end
It works similar to and_return
-- just use and_yield
:
describe '#crawl_messages' do
it 'should proc...
Middleman: Use pretty URLs without doubling requests
By default Middleman generates files with a .html
extension. Because of this all your URLs end in /foo.html
instead of /foo
, which looks a bit old school.
To get prettier URLs, Middleman lets you activate :directory_indexes
in config.rb
. This makes a directory for each of your pages and puts a single file index.html
into it, e.g. /foo/index.html
. This lets you access pages with http://domain/foo
.
Don't double your requests!
Unfortunately you are now forcing every br...
Middleman configuration for Rails Developers
Middleman is a static page generator that brings many of the goodies that Rails developers are used to.
Out of the box, Middleman brings Haml, Sass, helpers etc. However, it can be configured to do even better. This card is a list of improvement hints for a Rails developer.
Gemfile
Remove tzinfo-data
and wdm
unless you're on Windows. Add these gems:
gem 'middleman-livereload'
gem 'middleman-sprockets' # Asset pipeline!
gem 'bootstrap-sass' # If you want to use Bootstrap
gem 'byebug'
gem 'capistrano'
gem 'capistrano-mid...
Capistrano 3: How to deploy when a firewall blocks your git repo
Sometimes, through some firewall or proxy misconfiguration, you might have to deploy to a server that cannot access the git repository.
Solution 1: HTTP Proxy (this is the preferred fix)
SSH can be tunneled over an HTTP Proxy. For example, when the repo is on github
, use this:
-
Install
socat
-
Add a
~/.ssh/config
on the target server(s) with permission 0600 and this content:Host github.com ssh.github.com User git Hostname ssh.github.com Port 443 ProxyCommand socat - PROXY:<your proxyhost>:%h:%p,...
Quickly printing data in columns on your Ruby console
Dump this method into your Ruby console to quickly print data in columns. This is helpful for e.g. comparing attributes of a set of Rails records.
def tp(objects, *method_names)
terminal_width = `tput cols`.to_i
cols = objects.count + 1 # Label column
col_width = (terminal_width / cols) - 1 # Column spacing
Array(method_names).map do |method_name|
cells = objects.map{ |o| o.send(method_name).inspect }
cells.unshift(method_name)
puts cells.map{ |cell| cell.to_s.ljust(col_width) }.join ' '
end
nil
end
Usag...
Git: undo delete
Assuming you're wanting to undo the effects of git rm
or rm
followed by git add -A
or something similar:
This restores the file status in the index:
git reset -- <file>
then check out a copy from the index
git checkout -- <file>
To undo git add
, the first line above suffices, assuming you haven't committed yet.
Note:
Make sure to use double dashes --
to tell git to checkout a file instead of a branch. This only is relevant for [files having the same name as a branch](https://git-scm.com/docs/git-ch...
Howto: Require a gem that is not in Gemfile
In case you want to require a gem, that is not in the Gemfile of you bundle and therefore not in your loadpath, you need to add the path manually and require the gem afterwards.
Expected error
Requiring a gem, that is not in the Gemfile
or .gemspec
, will cause an LoadError
exception:
require 'example_gem' => LoadError: cannot load such file -- example_gem
Adding a gem to the loadpath temporary
- You need to install the
gem
gem install 'example_gem'
- Then you need to require the path where the gem was install...
Classic CSS problems that are easy with flexbox
Solved with flexbox is a collection of css problems which were hard or impossible to solve without flexbox:
- Better, Simpler Grid Systems
- Holy Grail Layout
- Input Add-ons
- Media Object
- Sticky Footer
- Vertical Centering
Rubymine provides a visual merge conflict resolution tool
RubyMine provides a visual tool for resolving merge conflicts locally.
Follow
Git > Resolve Conflicts
in the context menu to open RubyMine's merge conflict tool.
- Left pane: local copy (read-only)
- Right pane: checked in version from repository (read-only)
- Central pane: base revision from which both conflicting versions are derived
You can also use a similar pane view to compare to files.
Mark two files and press Ctrl + D
to compare.
How to use your iPhone's internet connection on your Ubuntu machine via USB
Luckily, this is simple. Just install three packages:
sudo apt install ipheth-utils libimobiledevice-dev libimobiledevice-utils
Then turn on the "Personal Hotspot" in iPhone settings, connect it to your Ubuntu machine via USB and you should be up and running.
Solved: Element inside overflow:scroll container cannot be scrolled on iOS when dragged by a contained iframe
Imagine the following HTML structure, where the scrolling container has overflow-y: scroll
:
+--scrolling container+-+
| |
| +-child element+----+ |
| | ++iframe++ | |
| | | | | |
| | | | | |
+-----------------------+
| | | | <-- actually cut off by
| +--------+ | <-- scrolling container
+-------------------+
The issue: On iOS, the child element cannot be scrolled when grabbing the iframe (i.e. putting your finger somewhere on the iframe).
...
Rendering 404s for missing images via Rails routes
When you load a dump for development, records may reference images that are not available on your machine.
Requests to those images may end up on your application, e.g. if a catch-all route is defined that leads to a controller doing some heavy lifting. On pages with lots of missing images, this slows down development response times.
You can fix that by defining a Rails route like this:
if Rails.env.development?
scope format: true, constraints: { format: /jpg|png|gif/ } do
get '/*anything', to: proc { [404, {}, ['']] }
...
How to disable Chrome's save password bubble for Selenium tests
When filling out forms in Selenium tests, Chrome shows the (usual) bubble, asking to store those credentials.
While the bubble does not interfere with tests, it is annoying when debugging tests. Here are two ways to disable it:
Option 1: prefs
You can set profile preferences to disable the password manager like so:
prefs = {
'credentials_enable_service' => false,
'profile.password_manager_enabled' => false
}
Capybara::Selenium::Driver.new(app, browser: :chrome, prefs: prefs)
Sadly, there are no command line s...
image-to-DataURI converter: Duri.me
Small web application where you can upload an image (PNG, JPEG, GIF) and generate a base64-encoded version of it.
You can copy the result as
- HTML
<img>
tag with data URI, - CSS rule with
background-image
and data URI, - plain Base64-encoded data URI string.
How to make http/https requests yourself with nc/telnet/openssl
Sometimes you want/have to send specific http(s) requests. You can do that easy with curl
or just write the request yourself.
make a http request with nc
nc example.com 80
GET / HTTP/1.1
Host: example.com
# press enter
make a http request with telnet
telnet example.com 80
GET / HTTP/1.1
Host: example.com
# press enter
make https request with openssl
openssl s_client -connect example.com:443
GET / HTTP/1.1
Host: example.com
# press enter
You can specify more headers if you want:
nc example.c...
Sending errors to sentry from development
For the initial setup or changes in the sentry reporting it might be useful to enabled reporting of sentry in development. Don't commit these changes and prefer to report to the staging environment. As other developers might be confused of these errors try to given them a proper message and delete them afterwards.
- Add
config.raven_dsn = 'your-dns'
inconfig/environments/development.rb
. - Add development to existing environments in the
Raven.configure
block:config.environments = ['development', 'staging', 'production']
. - ...