Let's say you need to revert a migration that happened a while back. You'd create a new migration that removes what was added back then in the up
path, while its down
path restores the old functionality.
While you could just copy&paste the down
and up
parts of it to the inverse part of the new migration, you may not want to do that. Especially when the up/down paths already contained some logic (that executed update
statements on the created column, for example), copying does not feel right.
Someone already added the logic how to...
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. Compare also the Gatekeeping: Guide for gatekeeper card.
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.
If your project manager wants to do gatekeeping o...
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.
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.
If you're responsible for gatekeeping in a projects, this is what to do:
First, read the [Gatekeeping for developers](https://makandracards.com/makandra/6579-gatekeeping-guide-for-develope...
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...
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')
...
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...
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...
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 ...
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...
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...
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:
HTTP_REFERER
coming from the same application, set a flash, and redirect backHTTP_REFERER
or one coming from an external source, set a flash...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...
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/
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:
^
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.
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.
In order to ignore changed files to being listed...
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...
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...
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.
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...
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:
...
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).
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:
0001-make-st...
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
Given /^my IP address is "(.*?)"$/ do |ip|
ActionDispatch::Request.any_instance.stub(:remote_ip).and_return(ip)
end
Rails 2
-----...
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...
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...
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}...