Touch records without running callbacks
ActiveRecord comes with a method touch
which sets the updated_at
timestamp to the current time. Unfortunately it also runs callbacks (and hence validations) on the receiving record, so it is unsuitable if you call it very often.
Use the attached initializer to get a touch_gently
method which updates updated_at
, but does not run callbacks:
User.find(5).touch_gently
The initializer will also give you a method touch_all_gently
whi...
Generate Puppet or Chef recipes from a Ubuntu system state
blueprint is DevStructure’s workhorse tool that looks inside popular package managers, finds changes you made to configuration files, and archives software you built from source to generate Puppet, Chef, or shell code. Everything blueprint sees is stored in Git to be diffed and pushed. It runs on Ubuntu Linux 10.04 and newer.
Global variables in Ruby
List of global variables available to a Ruby script.
Upgrading Cucumber and Capybara to the latest versions available for Rails 2
Specify these gem versions in your Gemfile:
gem 'cucumber', '~> 1.3.0'
gem 'cucumber-rails', '= 0.3.2' # max version for Rails 2
gem 'capybara', '< 2' # capybara 2+ requires Rails 3
gem 'mime-types', '< 2' # dependeny of capybara
gem 'nokogiri', '< 1.6' # dependency of capybara
gem 'rubyzip', '< 1' # dependency of selenium-webdriver, rubyzip 1+ requires Ruby 1.9
gem 'cucumber_factory'
gem 'database_cleaner', '< 1'
gem 'cucumber_spinner', '~> 0.2.5'
gem 'launchy', '~> 2.1.2'
With these versions set, `...
Hide Thunderbird's buttons in message headers area
I don't like those buttons inside the header area of a message that Thunderbird 3 put there. Though the CompactHeader addon is often claimed to be a solution it does way too much for me -- I like the headers the way they are, just not the buttons.
This is a bit of a hack but works very well for me (keeps the date and "other actions" menu on the right, see the screenshot below):
- Open up your profile directory (
~/.thunderbird/<profile_name>/
) - Create a
chrome
directory inside it. - Put the attached
userChrome.css
file into the ...
Onload callback for dynamically loaded images
Sometimes you need to dynamically load an image and do something as soon as its loaded (when for example its size is already available).
With jQuery, this seems to work across browsers:
$('<img>')
.attr('src', '')
.load(function() {
alert('fully loaded!');
})
.attr('src', '/the/real/image/url');
Skip Selenium scenarios in a Cucumber run
Cucumber scenarios that are tagged with @javascript
so they run with Selenium are very slow. You might not want to run them every time.
In order to skip all Selenium scenarios, call Cucumber like this:
cucumber --tags ~@javascript
Note that you should still run all the scenarios, including the Selenium ones, before you push a change.
Include existing PDF files into new TeX document
If you plan to insert an existing PDF into a new LaTeX document that you will compile to PDF, you can use \includegraphics
for this. Although, be prepared to get tons of errors complaining about overfill hboxes and the like.
You can use this package that takes care:
\usepackage[final]{pdfpages}
In order to insert a PDF, use:
\includepdf{$filename}
See the external link for more options.
Prohibit Git from merging .po-files
Merging .po-files with Git is painful.
There have been attempts of making Git more clever when trying to merge .po-files. I believe however that this is not enough as it can still produce invalid .pos (usually due to double definitions), which can seriously confuse gettext afterwards.
Lacking any more secure solution, I think you should avoid editing po files in different branches at the same time. In order to not accidentally produce invalid merges I additionally ...
Customize your Bash prompt
The shell variable PS1
holds your bash prompt. You might want to change it to serve your needs best. Here is how to:
General
- non-printing escape sequences in your prompt have to be inclosed in
\[\e[
and\]
so your shell can correctly count its prompt's length - we recommend to highlight your prompt on production machines
- you can also [show different root prompts for each user](https://makandracards.com/makandra/9569-get-the-username-w...
state_machine 0.10.0 was released
Now allows to list transition paths from and to arbitrary states.
Apache: SSL with Virtual Hosts Using SNI
With SNI, you can have many virtual hosts with HTTPS sharing the same IP address and port, and each one can have its own unique certificate. SNI is supported by all major browsers except Internet Explorer on XP, Safari on XP, wget, Android and Java.
Cheats to optimize your web font rendering
Type looking a little flabby? Overweight? Want to give it a kick in the pants? Take a look at some of these tricks to really give your web type a workout.
Sanitize user-generated filenames and only send files inside a given directory
If in your application your users pass along params that result in filenames, like invoices/generated?number=123
. This could be your (very careless) controller method:
def generated
send_file File.join(Rails.root, 'shared', 'invoices', params[:number])
end
This allows your users not only to access those files but also any files your application can read, like this:
invoices/generated?number=../../../../../etc/passwd
# => send_file '/etc/passwd'
You do not want this. In most cases you should prefer a show
met...
CSS3 Pie: Element not properly redrawn
Pie sometimes does not properly redraw elements upon changes. This often happens when the change comes from somewhere further up the DOM.
Consider something like:
<ul>
<li class="active"><div class="content">Active element</div></li>
<li class="inactive"><div class="content">Inactive element</div></li>
</ul>
with CSS
li .content {
-webkit-box-shadow: #666 0px 2px 3px;
-moz-box-shadow: #666 0px 2px 3px;
box-shadow: #666 0px 2px 3px;
behavior: url(/PIE.htc);
back...
JavaScript Garden
JavaScript Garden is a growing collection of documentation about the most quirky parts of the JavaScript programming language. It gives advice to avoid common mistakes, subtle bugs, as well as performance issues and bad practices that non-expert JavaScript programmers may encounter on their endeavours into the depths of the language.
JavaScript Garden does not aim to teach you JavaScript. Former knowledge of the language is strongly recommended in order to understand the topics covered in this guide
RSpec matcher to check if an ActiveRecord exists in the database
The attached RSpec matcher exist_in_database
checks if a given record still exists in the database and has not been destroyed:
describe Ticket do
describe '.purge_expired' do
fresh_ticket = Ticket.create(:expiry => Date.tomorrow)
expired_ticket = Ticket.create(:expiry => Date.yesterday)
Ticket.purge_expired
fresh_ticket.should exist_in_database
expired_ticket.should_not exist_in_database
end
end
Note that there is also [ActiveRecord::Base#destroyed?
](http://apidock.com/rails/ActiveRecord/Base/destroyed...
Setup (and secure) an SSH server on Ubuntu
Install OpenSSH Server:
sudo apt-get install openssh-server
To check if the server is running you should get no error when you restart it:
sudo /etc/init.d/ssh restart
Now your ssh server is ready to use.
To add additional security edit your sshd_config (gksudo gedit /etc/ssh/sshd_config
):
# Deny root login:
PermitRootLogin no
# To whitelist users:
AllowUsers USERNAME1 USERNAME2
# To disable interactive authentication (without SSH key)
PasswordAuthentication no
Don't forget to restart after editin...
Common VIM commands
An overview of common vim commands, including:
- windows
- buffers
- undo/redo
- navigation
- bookmarks
- selection/whitespace
- clipboard shortcuts
- search/replace
- programming
- external filters
Also see this German command list.
Useful
- toggle syntax highlighting:
:syntax on|off
Mocks and stubs in Test::Unit when you are used to RSpec
We are maintaining some vintage projects with tests written in Test::Unit instead of RSpec. Mocks and stubs are not features of Test::Unit, but you can use the Mocha gem to add those facilities.
The following is a quick crash course to using mocks and stubs in Mocha, written for RSpec users:
|---------------------------------------------------------|
| RSpec | Mocha |
|---------------------------------------------------------|
| obj = double()
| obj = mock()
|
| obj.stub(:method => 'value')
| `obj.stubs...
Find records with a Range condition
You can find ActiveRecord models by using a Range
as its conditions:
User.scoped(:conditions => { :id => 3..5 })
This will generate the following query:
SELECT * FROM `users` WHERE (`users`.`id` BETWEEN 3 AND 5)
This also means that all your scopes that take an array of allowed values and use condition hashes, automagically work for Ranges, too.
Directly search makandra notes from the Firefox address bar
The speed searching for makandra notes in Firefox can be improved by following these steps:
- Download the Firefox-Add-on "Add to Search Bar" and install it
- Go to the makandra notes search box and press the right mouse button to open the context menu
- Click "Add to Search Bar ..." and give it a name like "makandra notes" and a keyword like "n" or "notes"
Now you can type notes my search keywords
into the firefox address b...
Alternative to url_for's deprecated :overwrite_params option
If you have the following deprecation warning after upgrading to rails >= 2.3.10
DEPRECATION WARNING: The :overwrite_params option is deprecated. Specify all the necessary parameters instead.
that is for example caused by
url_for( :overwrite_params => { :order => 'name', :dir => 'asc' } )
you can fix this by using params.merge {:my_param_to_overwrite => 'foo' }
.
To fix the example above the code could look like:
url_for( params.merge { :order => 'name...
Output the descriptions of RSpec examples while they are running
In order to
- track down warnings and to see failing specs immediately
- or to get an overview of the core functionalities,
you can use RSpec's "nested" format. It looks like this:
Tool
validations
should require model to be set
should require place_id to be set
#identifier
should include the model and tag if the tool has a tag
should return the model if the tool has no tag
.search
should find tools by model and maker
should find tools by serial number
Call RSpec like...