WebMock 1.8.0 does not play nice with Curb < 0.7.16
When updating WebMock, be prepared that your specs may send real requests into the depths of the internet unless you update Curb as well.\
WebMock will not complain about those requests not being stubbed.
One of the commits that made it into 1.8.0 actually breaks Curb versions below 0.7.16 while fixing it for that version (and above, hopefully).\
WebMock's hooks for Curl::Easy
are sti...
Gatekeeping: Guide for developer
If your project manager wants to do gatekeeping on a project, as a developer you need to follow the following guidelines (e.g. by using something like this issue checklist template).
In order to reduce the number of rejects we get from clients, we want to review all code written before it goes to the staging server.
Note
This process is tailored to our specific needs and tools at makandra. While it will certainly not apply to all (especially larger teams), we think it...
Gatekeeping: Guide for gatekeeper
If you're responsible for gatekeeping in a projects, here is a guide, what to do.
In order to reduce the number of rejects we get from clients, we want to review all code written before it goes to the staging server.
Note: This process is tailored to our specific needs and tools at makandra. While it will certainly not apply to all (especially larger teams), we think it is a helpful starting point.
First, read the [Gatekeeping for developers](https://makandracards.com/makandra/6579-gatekeeping-guide-for...
RestClient sends XML Accept header by default
REST Client is a nice, simple HTTP client library for Ruby.
When you do a simple GET request like that:
RestClient.get 'http://example.com/'
it will result in this request beeing sent to www.example.com:
GET / HTTP/1.1
Accept: */*; q=0.5, application/xml
Accept-Encoding: gzip, deflate
Host: www.example.com
The application/xml
accept header might lead to unexpected results on your server. You can force REST Client to ask the server for default text/html
that way:
RestC...
Auto-generate Cucumber navigation paths
Don't you just hate to write Cucumber path helpers to be able to say this?
When I go to the user form for "foo@bar.de" # goes to edit_user_path(User.find_by_anything!('foo@bar.de'))
When I go to the form for the user "foo@bar.de" # goes to edit_user_path(User.find_by_anything!('foo@bar.de'))
When I go to the form for the user above" # goes to edit_user_path(User.last)
When I go to the project page for "World Domination" # goes to project_path(Project.find_by_anything!('World Domination')
...
Use the "retry" keyword to process a piece of Ruby code again.
Imagine you have a piece of code that tries to send a request to a remote server. Now the server is temporarily not available and raises an exception. In order to re-send the request you could use the following snippet:
def remote_request
begin
response = RestClient.get my_request_url
rescue RestClient::ResourceNotFound => error
@retries ||= 0
if @retries < @max_retries
@retries += 1
retry
else
raise error
end
end
response
end
This sni...
Sunspot for Solr fails with '400 Bad Request' in 'adapt_response'
If Sunspot does not work and fails with a backtrace similar to this:
/project/shared/bundle/ruby/1.8/gems/rsolr-1.0.6/lib/rsolr/client.rb:227:in `adapt_response'
/project/shared/bundle/ruby/1.8/gems/rsolr-1.0.6/lib/rsolr/client.rb:164:in `execute'
/project/shared/bundle/ruby/1.8/gems/rsolr-1.0.6/lib/rsolr/client.rb:158:in `send_and_receive'
(eval):2:in `post'
then the schema.xml
that is shipped with Sunspot is not loaded into Solr correctly.
Often the latter can be found in /etc/solr/conf/schema.xml
. So copy Sunspo...
Why your javascripts should be executed after the dom has been loaded
Most of the JavaScript snippets have code that manipulates the DOM. For that reason dom manipulating javascript code should have been executed after the DOM has loaded completely. That means when the browser has finished HTML parsing and built the DOM tree. At that time, you can manipualte the DOM although not all resources (like images) are fully loaded.
The following snippets show how you can do this with plain JavaScript, jquery or prototype ([dom ready ...
Detect mobile or touch devices on both server and client
Although it's tempting flirt with detecting mobile/touch devices with CSS media queries or Javascript feature detection alone, this approach will be painful when heavily customizing a feature beyond just tweaking the looks. Eventually you will want want the same detection logic to be available on both server and client side.
This card shows how to get a Ruby method touch_device?
for your Rails views and a method TouchDevice.isPresent()
for your Javascripts.
Note that we are detecting touch devices by grepping the user agent, and the ke...
Limiting CPU and memory resources of Paperclip convert jobs
If you're using Paperclip to store and convert images attached to your models, processing a lot of images will probably cause headache for your system operation colleagues because CPU and/or memory peaking.
If you're on Unix you can use nice
to tell the Kernel scheduler to prefer other processes that request CPU cycles. Keep in mind that this will not help if you're running into memory or IO trouble because you saved some bucks when you ordered (slow) harddrives.
ImageMagick (the tool which is used by Paperclip to do all that funky ima...
How to deal with MethodNotAllowed errors
One of the most common production errors are ActionController::MethodNotAllowed
errors. They usually happen when someone reloads a form by pressing enter
/return
in the URL field, or by opening JavaScript links incorrectly.
The attached initializer provides a default way to deal with this.
You'll get the following behaviour:
- if the incorrect request has a
HTTP_REFERER
coming from the same application, set a flash, and redirect back - if the incorrect request has no
HTTP_REFERER
or one coming from an external source, set a flash...
Useful script to collect downloads from several sites
For university I have to stay up-to-date with lecture documents. Since my university doesn't offer RSS feeds, I wrote a little script that collects files from web pages.
You want this, if you have several web pages that offer downloads that you don't want to check manually. Just register the URL and a CSS snippet to retrieve the files in the attached script and run it – it will fetch all your files. It will store all files in a single place or sort them into respective directories.
Edit the header of the file (providing your data), save it...
Request a gzipped response from a web server using Wget
To reduce download time, application servers usually serve content using gzip compression, if the browser supports it.
When using a tool like Wget to explicitly download an application's response, the server responds with the uncompressed version:
wget http://example.com/
If you are curious about the compressed file's size, pass the corresponding HTTP header:
wget --header="accept-encoding: gzip" http://example.com/
Caching may break relative paths in your merged stylesheet
If you turn on stylesheet caching, it might happen that stylesheets from different locations with different relative pathes will be put together to one big stylesheet.
This stylesheet then resides in /stylesheets but still holds the old pathes, which aren't valid anymore. This leads to the effect that images are displayed on your local development machine (where caching is turned off automatically) but not on the server.
To fix this, either:
^
- Move all stylesheets to the same folder
- or have one cache per folder
How to ignore new files or changed files in git
How to ignore new files
Globally
Add the path(s) to your file(s) which you would like to ignore to your .gitignore
file (and commit them). These file entries will also apply to others checking out the repo.
Locally
Add the path(s) to your file(s) which you would like to ignore to your .git/info/exclude
file. These file entries will only apply to your local working copy.
How to ignore changed files (temporarily)
In order to ignore changed files to being listed...
Declare different CSS background-images for different locales
If you would like to use language specific layout (e.g. background-images) in your applications stylesheets you can achieve this easily by using the lang
attribute in your views (ERB):
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<%= I18n.locale || 'en' %>" lang="<%= I18n.locale || 'en'%>">
...
</html>
or in HAML:
%html :xmlns => "http://www.w3.org/1999/xhtml", :"xml:lang" => I18n.locale || 'en', :lang => I18n.locale || 'en'
Then, in your stylesheet you can for example declare different background-images fo...
Web font embedding requires new CSS for IE9
If you embedded web fonts in the past years (e.g. by copying CSS from a Font Squirrel @font-face kit), that CSS won't work in Internet Explorer 9.
You can fix it by turning these styles...
@font-face
font-family: 'MyFont'
src: url('myfont.eot')
src: local("☺"), ('myfont.ttf') format("truetype")
font-weight: normal
font-style: normal
... into these:
@font-face
font-family: 'MyFont'
src: url('myfont.eot')
src: local("☺"), url('myfont.e...
How the Clearance gem remembers and clears sessions
Clearance is a gem that provides authentication functionality (e.g. login, logout). This note explains, how the clearance login, logout and (in old Clearances) remember me functionality works.
Login
Clearance defines a database column called "remember_token". When you login in, that token will be saved in a cookie. For that reason you don't have to re-sign-in when you close and open the browser again.
This also means that you can be logged in in more than a single browser. Also see [When ses...
Improve web font rendering in Windows by autohinting fonts
Web fonts are awesome. After being restricted to Arial for two decades there is finally a cross-browser way to embed fonts into web pages.
Unfortunately while web fonts look awesome on Linux and MacOS, they look horrible on Windows, a problem that gets worse with smaller font sizes.
The culprit is something called font hinting:
...
Creating a patch in git and how to apply patches
You can convert git commits into patch files. Those can be used to apply to a different repository [1] or by someone else (e.g. sent when sent to them via e-mail).
Creating a patch in git
- Make your changes and commit them.
- Run
git format-patch COMMIT_REFERENCE
to convert all commits since the referenced commit (not including it) into patch files.
For example, let's say you prepared 2 commits. Run:
git format-patch HEAD~~
This will create 2 files, one for each commit since HEAD~~
, like these:
00...
Stub a request's IP address in a Cucumber scenario
The solution in this card is based on a stack overflow post by Leventix.
If you need to make request come from a fixed IP address for the duration of a Cucumber scenario, the code below lets you write this:
Given my IP address is 188.174.117.205
Rails 3
Given /^my IP address is "(.*?)"$/ do |ip|
ActionDispatch::Request.any_instance.stub(:remote_ip).and_return(ip)
end
Rails 2
-----...
Parse & sort unique hits in logfiles
If you want to know the exact hits on your website (or whatever logfile you want) for a specific date without duplicates, here's how.
"Unique" means you don't want to count hits to an URL originating from the same IP twice.
You can use the attached script to do so:
# ./log_parser.rb 2011-10-04
27 hits on /rss.xml
36 hits on /stylesheets/fonts/slkscr-webfont.woff
37 hits on /stylesheets/fonts/slkscrb-webfont.woff
37 hits on /images/bullet.png
38 hits on /images/download.png
38 hits on /images/play.png
39...
Rails logs are not flushed automatically (in Rake tasks)
The Rails logger will store its content in a buffer and write it into the file system every 1000 lines. This will come back to bite you when using Rails.logger.info
to write log output during Rake tasks or on a production console.
You often won't notice this because for the development
and test
environments auto_flushing
is set to write after each line. On production environments the Rails logger writes only every 1000 lines -- and not upon shell or script ter...
Monitor a Rake task with God
In order to monitor a Rake task using God your Rake file must write a file with its process ID (PID) to a path determined by God. This way God can check whether the Rake process is still alive.
Here is how to do this: In your God config, call the Rake task with an environment variable PIDFILE
. This variable should equal the PID file path desired by God:
God.watch do |w|
w.dir = "#{rails_root}"
w.name = "my_task"
w.interval = 10.seconds
w.pid_file = "#{rails_root}/tmp/pids/#{w.name}...