Function to return the minimum or maximum value per row with MySQL
MySQL's MIN and MAX functions are for aggregations only. This will not work and produce an error:
SELECT id, MIN(birthday, '1978-01-01') FROM users;
In order to compute the minimum or maximum value for the current row, use LEAST and GREATEST instead:
SELECT id, LEAST(birthday, '1978-01-01') FROM users;
Export more than 500 rows in Google Analytics
Currently, only up to 500 rows of Analytics table data can be exported at a time into CSV format. If you need to export larger data sets, like exporting all keywords that sent traffic to your site, you can export multiple times as long as each batch contains at maximum 500 rows.
If you have thousands of rows that require multiple exports, you can use the convenient workaround below to export all your rows in one go.
Dual monitors crashing Linux with NVIDIA drivers
My T410 has a NVIDIA graphics card (NVS 3100M).
My BIOS configuration looks like this:
- NVIDIA Optimus is disabled in
- "Discrete" mode on
- Internal Intel card deactivated
My X crashed when trying to activate both Dell U2410 monitors connected using DVI to the docking station.
The solution was to disable DDC/CI on the monitors: Menu -> Other settings -> DDC/CI disable
Note that you shouldn't disable DDC/CI if you don't have any issues with your displays.
Add a prefix to form field IDs
If you use a form (or form fields) multiple times inside one view, Rails will generate the same id attributes for fields again.
This card presents you with a way to call something like
- form_for @user, :prefix => 'overlay' do |form|
= form.text_field :email
and get this as HTML:
<input name="user[email]" id="overlay_user_email" (...) />
You can also put a :prefix into a field's options. Note how only the id but not the name changes as we would not want to pass an overlay_user[email] param to the controller. Sett...
Exchange messages between Javascript and Flash
Flash movies (.swf files) can talk with Javascript code embedded in the same HTML page. There are two ways to do this:
- The preferred way is to use the ExternalInterface class to call Javascript functions from ActionScript, and to bind ActionScript functions to the Flash movie's DOM element so they can be called from Javascript.
- The deprecated way is to use the global [fscommand](http://help.adobe....
Insert an ndash and other special characters using the Compose key on Linux
Although you can access many symbols using the AltGr key you may be missing some, like the en-dash (–) or em-dash (—). You can use a compose key for them instead.
First, make sure you have a compose key configured.
Configuring a compose key
I suggest using the "Menu" key which is located between the right Meta and Ctrl key.
Ubuntu / MATE
Control Center → Keyboard → Layout → Options → Position of Compos...
Configuring User Agents with Capybara + Selenium Webdriver
A while ago we were working on an application that had an entire version specially created for mobiles, such as the iPhone. This specific application was entirely tested with Capybara, Steak and Selenium Webdriver. Although the test suite wasn’t the fastest one in the world, the web application was very well tested, and to guarantee that we would also be testing the mobile version, we would have to simulate an iPhone user agent accessing the application.
But wait, you might be thinking that we are not able to change browser headers while ...
Paperclip: Image resize options
Paperclip uses the imagemagick resize options like 100x50> , 100x50<, 100x50# etc to resize images.
See the link what options are available.
- ('!') Ignore Aspect Ratio
- ('>') Only Shrink Larger
- ('<') Only Enlarge Smaller
- ('^') Fill Given Area
- ('%') Percentage Resize
- ('@') Pixel Area Limit
- ('#') Crop thumbnail centrally and ensure the requested dimensions, not documented at imagemagick doc
Escaping of quotation marks in Cucumber steps and step definitions
Issue
When you have a Cucumber step like
Then I should see "Did you see those \"quotation marks\" over there?"
you'll run into trouble. Cucumber won't take your escaped quotation marks.
Workarounds
One workaround is to write the step as regex (since there is a step taking a regex):
Then I should see /Did you see those "quotation marks" over there\?/
Keep in mind it’s a regex – escape regex characters like '?'.
When you have a Cucumber step like
Then I should fill in "query" with "\"some phrase\""
The fo...
sudo command for Windows
There is no good equivalent for the Unix sudo command in Windows. Below are a few workarounds that are all unsatisfactory to some degree.
Run the command prompt as Administrator
- Right-click on the Command Prompt icon
- Select Run as Administrator
- Do what you need to do
- Quickly close the terminal
Sudo for Windows
If you are using an English Windows, you can use Sudo for Windows. Unfor...
Ubuntu: Open a new terminal window with a keyboard shortcut
To open a terminal quickly navigate to System → Preferences → Keyboard Shortcuts. There, click the "Run a terminal" row (It should be in the "Desktop" section) and press the keyboard shortcut you like.
If you cannot find such an entry you may need to add it yourself -- the command for Ubuntu's default terminal is gnome-terminal.
Be aware that many programs use shortcuts of their own and you would not want to clash with those (like Ctrl+T). Something like Meta+T is something usually not ta...
Maximum representable value for a Ruby Time object
On 32bit systems, the maximum representable Time is 2038-01-19 03:14:07 in UTC or 2038-01-19 04:14:07 in CET. If you try to instantiate a Time with any later value, Ruby will raise an ArgumentError.
If you need to represent later time values, use the DateTime class. This is also what Rails does when it loads a record from the database that has a DATETIME value which Time cannot represent. Note that there are some [subtle differences](http://stackoverflow.com/quest...
How to change MySQL's data directory
-
Have a backup.
-
Stop MySQL:
sudo service mysql stop -
Move (or copy) your
mysqldirectory. If you want/mnt/mysqlto be the new directory, do it like this:sudo mv /var/lib/mysql /mnt/ -
Open your MySQL configuration (
sudo vim /etc/mysql/my.cnf) and change thedatadirvalue to your new path (e.g./mnt/mysql) -
Modify your AppArmor configuration:
sudo vim /etc/apparmor.d/usr.sbin.mysqldChange/copy the lines granting access to
/var/lib/mysqlto your new path. Otherwise MySQL will not ...
HTML5 and Web Video: Questions for the Industry from the Community
What are Google’s plans for turning WebM into a genuinely open standard, one that is based on consensus like the rest of W3C’s HTML5 effort? Would Google fully support such an effort? Even the WebM project’s domain is controlled by Google. Google chose to release WebM under the Creative Commons license which would theoretically allow a standards body to use the specification as a basis for a truly open standard. Would Google agree to adopt the specification and changes that would emerge from an open process in a timely and robust manner? Wha...
Encrypt files using OpenSSL
Using OpenSSL it's very easy to seriously encrypt files.
Use the script below. Input / Output are self explanatory. Put a long passphrase into PASSWORD_FILENAME. It is the key to decrypt to file again. Paste this into a console and copy it to wherever you need it.
touch /tmp/openssl_encryption_password && chmod 0700 /tmp/openssl_encryption_password && apg -n1 -m 60 > /tmp/openssl_encryption_password
Remember to at least delete the input file afterwards. Better use [shred to wipe files on Linux](https://makandracards.com/makandra/1013-s...
Securely remove files on Linux
When you delete a file with rm it's still possible to recover the file or parts of it. Use shred to overwrite the content of a file and delete it afterwards.
shred -u $file
Be aware that this is not sufficient for flash memory, like SSDs or USB pen drives. The write will possibly target another memory area than where the file was located previously. Prefer disk encryption whenever possible.
Erasing directories
To erase complete directory structures, using shred is cumbersome. Use wipe instead:
wipe -...
Upgrade from Rails 2.3.10 to Rails 2.3.11
See our new comprehensive guide for upgrading every Rails 2 version ever.
Dump your database with dumple
This tool is used on our application servers (and called when deploying) but it also works locally.
Just call dumple development from your project directory to dump your database.
This script is part of our geordi gem on github.
Validate an XML document against an XSD schema with Ruby and Nokogiri
The code below shows a method #validate which uses Nokogiri to validate an XML document against an XSD schema. It returns an array of Nokogiri::XML::SyntaxError objects.
require 'rubygems'
gem 'nokogiri'
require 'nokogiri'
def validate(document_path, schema_path, root_element)
schema = Nokogiri::XML::Schema(File.read(schema_path))
document = Nokogiri::XML(File.read(document_path))
schema.validate(document.xpath("//#{root_element}").to_s)
end
v...
Continously run command under bash
Sometimes you want to run a command forever, e.g. to compile a haml to html file on the console. Use this:
$ while(true) do haml index.haml index.html; sleep 1.5; done
Cucumber fails without giving error message
To improve your chances to get proper error messages, you can try
^
- to start cucumber with the
-boption, so it shows a full backtrace - to start cucumber with the
-f prettyoption to get a more verbose formatter - to start cucumber with the
-doption to show debug information
Linux: rename or change extension of multiple files
When you need to bulk rename files you can not call "mv *.foo *.bar" to change the extension of all .foo files to bar (because bash resolves wildcards and replaces them with the list of matched files).
This works on linuxes who use the Perl version of the rename command (like Ubuntu):
rename 's/\.foo$/\.bar/' *
You can also use this to rename other parts of the file, e.g. from flag_en.png, flag_de.png etc. to just en.png or de.png:
rename 's/^flag_//' *
Note that we used $ and ^ to explicitly look at the ...
Deploy and migrate with a single Capistrano command
Note that this sounds good but is not good at all when hooking tasks on cap deploy (see this article). Make sure to hook your calls properly when using this.
To deploy an application and run all pending migrations before restarting it, you can use the following standard Capistrano task:
cap deploy:migrations
Little is known what happens when the deployment goes through, but a migration or a rake task aft...
Show the description of a Capistrano task
In order to bring up a textual description of a Capistrano task you can say
cap -e taskname
... where taskname is the name of the task you're not sure about.