Change file permissions with Ruby
You can say
File.chmod(0604, '/path/to/file') # rw----r--
The leading zero is meaningful.
Delete a Clearance session after some time of inactivity
This note describes how to kick a user out of a Rails application after she hasn't requested an action for a while. Note that this is different from deleting sessions some time after the last login, which is the default.
Also note that this is probably a bad idea. Most sites keep sessions alive forever because having to sign in again and again is quite inconvenient for users and makes your conversion rates go down the toilet. [The Clearance default is to keep sessions around for one year](https://makandracards.com/makandra/701-when-sessio...
Rails 3: Sending tempfiles for download
When you create a temporary file (e.g. to store a generated Excel sheet) and try to send it to the browser from a controller, it won't work by default. Take this controller action:
class FoosController < ApplicationController
def download
file = Tempfile.new('foo')
file.puts 'foo'
file.close
send_file file.path
end
end
Accessing this controller action will usually raise a 404 not found in the browser and the Apache log will say:
The given path was above the root path: xsendfile: ...
How to type accented characters on keyboard layouts without dead keys
Ubuntu comes with keyboard layouts like "Germany Eliminate Dead Keys", which are practical for programming.
If you need to type accented characters with such a layout, make sure to configure a Compose key. You can then look up which compose combo will produce the character you need.
E.g. you can type "á" by pressing Compose, ´, a
.
Rails 3: Make "link_to :remote => true" replace HTML elements with jQuery
In Rails 2, you could use link_to_remote ... :update => 'id'
to automatically replace the content of $('#id')
.
To do the same in Rails 3, include usual rails-ujs JavaScript, and put this into your application.js
:
$(function() {
$('[data-remote][data-replace]')
.data('type', 'html')
.live('ajax:success', function(event, data) {
var $this = $(this);
$($this.data('replace')).html(data);
$this.trigger('ajax:replaced');...
Fix for: Your Firefox profile cannot be loaded. It may be missing or inaccessible.
If you use Selenium and Launchy to open web pages, you might run into an error saying "Your Firefox profile cannot be loaded. It may be missing or inaccessible.".
This happens because Launchy tries to use your Firefox running the web driver to open the page.
For Launchy < 0.4.x, as a workaround you can set an environment variable
export LAUNCHY_BROWSER=`which google-chrome`
For newer Launchys you also need to set:
export BROWSER=`which google-chrome`
If you want to do this in a ruby script, you can say
chrome_path ...
RSpec 2.6 supports "any_instance" now
This finally works:
User.any_instance.should_receive(...)
as does
User.any_instance.stub(...)
Note: You won't have RSpec 2.6 if you're still working on Rails 2.
Failtale
Free Hoptoad/Airbrake alternative which can capture exceptions from any platform. It comes with a Rails notifier and a RESTful API to write your own notifiers for Javascript, etc.
Should we some day wish to do away with the current way we deal with exceptions, this might be a good place to start.
Hex color codes for the CGA palette
Below you can find the hex color codes for the Color Graphics Adapter palette which used to be popular in the 1980s.
$cga_black: #000000
$cga_white: #ffffff
$cga_light_gray: #aaaaaa
$cga_dark_gray: #555555
$cga_yellow: #ffff55
$cga_brown: #aa5500
$cga_light_red: #ff5555
$cga_dark_red: #aa0000
$cga_light_green: #55ff55
$cga_dark_green: #00aa00
$cga_light_cyan: #55ffff
$cga_dark_cyan: #00aaaa
$cga_light_blue: #5555ff
$cga_dark_blue: ...
Virtual attributes for date fields
Note that this card is very old. You might want to use ActiveType for your auto-coerced virtual attributes instead.
We sometimes give our models virtual attributes for values that don't need to be stored permanently.
When such a virtual attribute should contain Date
values you might get unexpected behavior with forms, because every param is a string and you don't get the magic type casting that ...
How to remap your caps lock to a second Escape key
When have you last used your caps lock key? On purpose? Right...
In Gnome
Make it an additional ESC key by going to System -> Preference -> Keyboard -> Layouts -> Options -> CapsLock key behavior and select "Make CapsLock an additional ESC".
On recent Ubuntus this option was removed so you have to resort to using dconf.
dconf write /org/gnome/desktop/input-sources/xkb-options "['caps:escape']"
Or, open Tweak advanced GNOME 3 settings
and ...
url_for with added params
You cannot say this because url_for
only takes one parameter:
url_for(@deal, :tab => 'general') # won't work
Just use polymorphic_url
instead:
polymorphic_url(@deal, :tab => 'general')
Rails 3 issue: update_all ignores conditions, when :orders and :limit options are supplied
Leads to awesomeness and unicorns when used in production.
How to use different encodings for text in HTTP headers
In order to use different encodings than ASCII for HTTP headers use the following syntax:
Header-Key: Header-Value; Parameter-Name*=utf-8''parameter_value_in_utf8_and_encoded_chars
Concrete example how to use an utf8 encoded filename for file downloads (with fallback):
Content-Disposition: attachment; filename="aepfel"; filename*=utf-8''%c3%a4pfel
Shell script to generate a Git commit with Pivotal Tracker story ID and title
We usually generate our commit messages from Pivotal Tracker IDs and titles, like
[#15775609] Index view for conflicts
The geordi command commit
automates this. (See: Pretty Commit messages via geordi).
Just run geordi commit
and it will connect to PT and let you select from a list of all started and finishes stories. Then it runs git commit
with the generated message (i.e. all staged changes will be commited).
When running for the first time, Geordi will request your PT...
Fix "private method `select' called for Capybara::Node::Element
API breakage ahoy. You need to either upgrade your Capybara or downgrade your selenium-webdriver
gem.
Alternatively, this could solve your trouble.
Edit your previous Skype messages
Just in case you did not know it: If you sent a Skype message containing a typo, you don't need to send a second one to correct yourself.
Instead, simply press the up key (↑
). The message area will become yellow and contain your previously sent message. Make any changes and submit again.\
You can also right-click any other message (of the current session) to edit it.
Linux: Create file of a given size
Sometimes you need a file of some size (possibly for testing purposes). On Linux, you can use dd
to create one.
Let's say you want a 23 MB file called test.file
. You would then run this:
dd if=/dev/zero of=test.file bs=1048576 count=23
The block size (bs
) is set to 1 MB (1024^2 bytes) here, writing 23 such chunks makes the file 23 MB big.\
Adjust to your needs.
This linux command might also come in handy in a Ruby program. It could be used like:
mb = 23
mb_string, _error_str, _status = Open3.capture3('dd if=/dev/zero...
Dotfiles: Keep your linux configuration on github
Running the Awesome window manager within Gnome
Note: Consider using MATE instead of Gnome 3 on newer system
Awesome is a very good tiling window manager that provides neat features like automatic layouting of windows, good multi-display support with per display workspaces and more. Unfortunately, it is only a window manager, and lacks a lot of Gnome's conveniences like the network manager, application menus, automatic updates etc.
Fortunately, Gnome allows you to selectively replace only the window manager, so you get all of Gnome'...
Hoptoad is now Airbrake
We are changing our name from Hoptoad to Airbrake. You see, some folks much larger than us reached out and claimed trademark over all things related to frogs and toads and little animals of that ilk. After speaking to our lawyers we reluctanctly decided it’s best to change the name.
How to enlarge a VirtualBox VDI disk file
VirtualBox does not offer anything for this task -- you need to do it yourself. It's not that hard:
Get more disk space
- Add an extra virtual hard disk to the machine with the disk size you want to achieve.
- Get a Linux live CD (like the Ubuntu live image) that offers
fdisk
,dd
andgParted
. - Boot the guest from the CD, open a terminal (on the guest, not the host!) and become root:
sudo su
-
fdisk -l
to see the disk information. \
There should be one drive with some partitions and one without any....
Show the status of a running dd copy
When you do a bitwise copy using the dd
tool you will not see any output until it completes or an error occurs.
However, you can send a command signal to the process to have it show its progress so far.
From another terminal, simply call (be root or use sudo
):
pkill -USR1 dd
This makes dd
write something like this into the terminal it is running in:
388+0 records in
387+0 records out
396288000 bytes (396 MB) copied, 24.9862s, 15.9 MB/s
Disable Thumbs.db on Windows 7
Every time you open a directory containing images, Windows creates those pesky Thumbs.db
files for a minor speed-up and lots of cluttering your zip files etc.
You do want to disable them. Here is how on Windows 7:
- Start → Run (or press Meta+R)
- gpedit.msc
- User Configuration → Administrative Templates → Windows Components → Windows Explorer
- Locate "Turn off the caching of thumbnails in hidden thumbs.db files" (should be one of the top list entries)
- Set it to "Enabled". Yes, seriously -- you enable the disabling of thumbs.d...