How to make a single check box (or image, etc) align vertically
Consider this HTML:
<div style="line-height: 42px">
<input type="checkbox" />
</div>
Even though the surrounding container defines a line-height
, which vertically centers its inline elements, the check box will be top aligned if it is the only element inside the container.
It will be aligned correctly if the HTML looks like this:
<div style="line-height: 42px">
<input type="checkbox" /> foo
</div>
So the ac...
Ruby GetText will eval scripts containing ActiveRecord classes
When the Ruby parser module of Ruby-GetText comes across a file in one of its search directories (e.g. lib/scripts/
) and finds out that you are defining ActiveRecord classes inside it, it evaluates the whole file. Here is how to avoid that.
What's happening?
Let's say you have the following script which is only run once, manually, via script/runner
:
# lib/scripts/doomsday.rb
class User < ActiveRecord::Base; end
User.destroy_all
In that case we ...
List sizes of MySQL databases
Do you wonder which databases are actually taking up how much space but only have one huge ibdata1
in your /var/lib/mysql
and the directories inside your mysql data directory don't represent the actual database sizes? This is for you!
Run from a mysql root console:
SELECT table_schema AS "Database name", SUM(data_length + index_length) / 1024 / 1024 AS "Size (MB)" FROM information_schema.TABLES GROUP BY table_schema;
This will get you a result like the following:
+----------------------+--------------+
| Database name ...
Grep the number of occurences in a file, counting multiple hits per line
Grep prints one line per match. To return the number if matches, use the -c
switch:
grep -c "something" filename
However, if a word appears more than once in a line, it is only counted once.
To count every match, you can use sed
to force line breaks on multiple matches:
sed 's/something/something\n/g' filename | grep -c "something"
Solarized color scheme for Rubymine
If you want to use the (badly implemented!) solarized color scheme in your Rubymine IDE:
-
Clone the IntelliJ IDEA port:
git clone https://github.com/jkaving/intellij-colors-solarized.git
-
Import the settings in Rubymine by chosing the above directory
File -> Import Settings
-
Change the color scheme in the settings (Ctrl-Alt-S) in
Editor -> Colors & Fonts
Note that selecting a color scheme will also reset your previous editor font choice. You might want to change the font to [Envy Code R](http://damieng.com/blog/2008/05/2...
Fix: FreeBSD is not able to connect to the network
This article from the FreeBSD Handbook suggests that editing /etc/rc.conf
enables DHCP. Unfortunately, some times this seems not sufficient.
Try a reboot, or start the DHCP client manually with the command
dhclient <network adapter>
You can find a list of network adapters by running ifconfig
. Examples are em0
or lo0
.
How to change the font in Sublime Text 2
Open up your "Base File.sublime-settings
" (Preferences Menu → File Settings – User) in Sublime Text 2. Add entries for font_face
, and optionally font_size
, so it looks similar to this:
{
"font_face": "Envy Code R",
"font_size": 11.5
}
Cool: when you save the configuration file, changes will be applied instantly.
Solve issues with Samsung TV as computer screen
We have a big flat screen TV (Samsung LE46c650l1kxxu) in our conference room. Configuring it properly, we were encountering some issues.
This is a list of issues, providing a solution to each.
No sound with DVI/HDMI and separate audio
There are some inputs on the side of the TV. These will not work for split video and audio.
On the back are more inputs you might not have seen. Choose HDMI/DVI as video in and Audio in for HDMI/DVI as audio in. Do not use RCA audio input but the [...
Create RAID on Amazon EC2 EBS volumes
If you use Amazon AWS cloud services you definitively want to utilize software raid for IO intensive stuff such as database data directories and the like.
Here's how you create it:
- Create some EBS volumes within management console or
sudo mdadm --create -f /dev/md0 --chunk=256 --level 10 --raid-devices 4 /dev/sdf /dev/sdg /dev/sdh /dev/sdi
- Create a fs on
/dev/md0
using mkfs*-tools - As soon as this is running, you should do that:
sudo mdadm --detail --scan >> /etc/mdadm/mdadm.conf
Make sure the last line in `/etc/mdadm/md...
Solarized
Solarized is a sixteen color palette (eight monotones, eight accent colors) designed for use with terminal and gui applications. It has several unique properties. I designed this colorscheme with both precise CIELAB lightness relationships and a refined set of hues based on fixed color wheel relationships. It has been tested extensively in real world use on color calibrated displays (as well as uncalibrated/intentionally miscalibrated displays) and in a variety of lighting conditions.
Marry Date::Performance and Spreadsheet gems
Date::Performance is a gem that replaces various method in Ruby's Date
class with fast C implementations. Unfortunately it doesn't fully implement an internal method (Date.ajd_to_jd
) which makes your code blow up when you use it together with the Spreadsheet gem.
A solution is to restore the Ruby implementation of this particular method. To do this, copy the attached file to lib/fix_date_performance.rb
to config/initializers
.
"The certificate is not trusted because no issuer chain was provided." Error with SSL + Nginx
If you use Nginx with SSL and get an intermediate certificate with your certificate you have to do this:
cat /path/to/intermediate/certificate >> /path/to/certificate
Capistrano: Delete old releases automatically
Whenever you deploy using Capistrano, a new folder is created within the releases directory on the remote server containing the application code.
By default Capistrano 3 keeps the last 5 releases in case you need to rollback to an older release. You can overwrite this setting by using
set :keep_releases, 3
Resque + God + Capistrano
Attached is a working config to deploy an application with Capistrano that needs to monitor Resque workers with God.
God will run as the deployment user, no need to register it as a system service.
Put this into your config/deploy.rb
:
namespace :god do
def god_is_running
!capture("#{god_command} status >/dev/null 2>/dev/null || echo 'not running'").start_with?('not running')
end
def god_command
...
How to: Store multiple Vim commands in macros and recall them
Vim allows recording a batch of commands as a macro. This is handy if you need to do the same things over and over.
Here is how:
- Press
q
to enter macro mode. - Press a letter (not a number!) key to assign a slot to your macro.
- You are now recording. Do whatever you want with usual commands.
- Once you are done, press
q
again to stop recording. - You can now run your recorded macro by pressing
@
and its assigned letter key.
Cheats:
- If you want to run a macro repeatedly, type a number before pressing the
@
key. Example: ...
Chosen - makes select boxes better
Chosen is a JavaScript plugin that makes long, unwieldy select boxes much more user-friendly. It is currently available in both jQuery and Prototype flavors.
Thunderbird: Confirm encryption absence/presence before sending an e-mail
To avoid sending e-mails containing sensitive data unencrypted I strongly suggest you enable a confirmation dialog.
Enigmail can show if the e-mail being sent will be encrypted or not -- and if, which keys will be used. This applies to both clicking the "Send" button or pressing Ctrl+Enter. Thus, you may disable the confirmation for the keyboard shortcut as you get a message box anyways which will avoid sending e-mails by accident.
Instructions
Head over to the Enigmail preferences from the "OpenPGP" menu and tick the corresponding che...
Always store your Paperclip attachments in a separate folder per environment
tl;dr: Always have your attachment path start with :rails_root/storage/#{Rails.env}#{ENV['RAILS_TEST_NUMBER']}/
.
The directory where you save your Paperclip attachments should not look like this:
storage/photos/1/...
storage/photos/2/...
storage/photos/3/...
storage/attachments/1/...
storage/attachments/2/...
The problem with this is that multiple environments (at least development
and test
) will share the same directory structure. This will cause you pain eventually. Files will get overwritten and...
Nginx Error "413 Request Entity Too Large"
If you get the error "413 Request Entity Too Large" from Nginx client_max_body_size
is too low (default is client_max_body_size=1m
).
This can happen for example during file upload.
Check whether a Paperclip attachment exists
Don't simply test for the presence of the magic Paperclip attribute, it will return a paperclip Attachment
object and thus always be true:
- if user.photo.present? # always true
= image_tag(user.photo.url)
Use #exists?
instead:
- if user.photo.exists?
= image_tag(user.photo.url)
Insert multiple blank rows into an OpenOffice.org/LibreOffice Calc spreadsheet
Select as many rows as you'd like to insert by dragging over the row numbers on the left. Then right-click on any selected row number and select "Insert Rows". Calc will now insert multiple blank rows.
The inserted rows will copy the style from the row above the selection.
This is horrible.
How to diff two strings in Ruby
When you need to use diff
in either some Ruby code or your Rails app, use the differ gem.
puts Differ.diff "foo", "boo"
# => {"boo" >> "foo"}
Usage
There are several variants available, all using the base method diff(to, from, separator = "\n")
.
You have diff_by_line
, diff_by_word
, diff_by_char
and may of course use your own separator:
puts Differ.diff 'Hauptsatz, und mein Nebensatz.', 'Hauptsatz, und dein Nebensatz.', ','
# => Hauptsatz,{" und dein Nebensatz." >> " un...
Responding to the OPTIONS HTTP method request in Rails: Getting around the Same Origin Policy
Code example for implementing Cross-Origin Resource Sharing (CORS) in Rails.
Cross-Origin Resource Sharing - Wikipedia
Cross-Origin Resource Sharing (CORS) is a browser technology specification, which defines ways for a web service to provide interfaces for sandboxed scripts coming from a different domain under same origin policy. CORS is a modern alternative to the JSONP pattern. While JSONP supports only the GET request method, CORS also supports other types of HTTP requests. Using CORS enables a web programmer to use regular XMLHttpRequest which supports better error handling than JSONP. On the other hand, JSONP works on legacy browsers that do not have C...