Automatically strip all string fields of an ActiveRecord
If your application has forms to edit string fields, you probably want to strip the entered values (remove whitespace from beginning and end). The reason is that your users will copy + paste values from unholy places (websites, Microsoft Office) and end up having trailing whitespace in most of their records.
Because browsers ignore whitespace, no one will usually notice this until you get the weirdest bug reports (e.g. two seemingly equal records are not, or multiple records for "unique" values).
Use the attached trait in your model to hav...
Creating a Grid for Product View for a eCommerce Site
Randy Hoyt is presenting a way to display something (e.g. products) in a grid. It's responsive, too!
Get rid of WARNING: Nokogiri was built against LibXML version 2.7.7, but has dynamically loaded 2.7.8
If you get this warning on your local machine one of these steps might help:
Rebuilt the gem with the newer library
gem install --no-rdoc --no-ri nokogiri -- --with-xml2-include=/opt/local/include/libxml2 --with-xml2-lib=/opt/local/lib
If you still get the error, try to uninstall all nokogiri versions with
gem uninstall nokogiri
and install nokogiri again.
Fixing the issue on servers
However, on our servers this probably will not work. On the server, gems are stored in the ../shared/bundle/ruby/:version/gems
dire...
Git basics: checkout vs. reset
Today I got a better understanding of how git works, in particular what git checkout
and git reset
do.
Git basics
- A commit holds a certain state of a directory and a pointer to its antecedent commit.
- A commit is identified by a so-called ref looking something like
7153617ff70e716e229a823cdd205ebb13fa314d
. - HEAD is a pointer that is always pointing at the commit you are currently working on. Usually, it is pointing to a branch which is pointing to that commit.
- Branches are nothing but pointers to commits. Y...
`simple_format` does not escape HTML tags
simple_format
ignores Rails' XSS protection. Even when called with an unsafe string, HTML characters will not be escaped or stripped!
Instead simple_format
calls sanitize
on each of the generated paragraphs.
ActionView::Base.sanitized_allowed_tags
# => #<Set: {"small", "dfn", "sup", "sub", "pre", "blockquote", "ins", "ul", "var", "samp", "del", "h6", "h5", "h4", "h3", "h2", "h1", "span", "br", "hr", "em", "address", "img", "kbd", "tt", "a", "acrony...
Create autocompletion dropdown for Cucumber paths in Textmate
Ever wanted autocompletion for paths from paths.rb
in Cucumber? This card lets you write your steps like this:
When I go to path *press tab now* # path is replaced with a list of all known Cucumber paths
This is how you do it
(key shortcuts apply for TextMate2)
-
Open the bundle editor (ctrl + alt + + B)
-
Create a new Item ( + N), select "Command"
-
Paste this:
^
#!/usr/bin/env ruby -wKU
require File.join(ENV['TM_SUPPORT_PATH'], 'lib', 'ui.rb')cucumber_paths = File.join ENV['TM_PROJECT_DIRECTORY'], 'features'...
Highlight current navigation item with Staticmatic
StaticMatic is a nice tool to build simple static websites.
In case you want to have some nifty styles on the navigation item that is currently active, you can use this:
=link 'Aktuelles', :class => (current_page =~ /aktuelles/) ? 'current' : 'default'
Keep in mind that current_page
gives you the full relative path of your page. raise
current_path in case you're not sure.
I know there is an navigation helper out there. I did not use it and also did not want to migrate.
How to make your application assets cachable in Rails
Note: Modern Rails has two build pipelines, the asset pipeline (or "Sprockets") and Webpacker. The principles below apply for both, but the examples shown are for Sprockets.
Every page in your application uses many assets, such as images, javascripts and stylesheets. Without your intervention, the browser will request these assets again and again on every request. There is no magic in Rails that gives you automatic caching for assets. In fact, if you haven't been paying attention to this, your application is probabl...
Test meta-refresh redirects with Cucumber
The step definition below allows you to write:
Then I should see an HTML redirect to "http://www.makandracards.com" in the page head
Capybara
Then /^I should see an HTML redirect to "([^\"]*)" in the page head$/ do |redirect_url|
page.should have_xpath("//meta[@http-equiv=\"refresh\" and contains(@content, \"#{redirect_url}\")]")
end
To find meta
tags with Capybara, you can also use page.find('meta', visible: false)
.
"rake gettext:findpo" running forever
Under certain circumstances gettext_i18n_rails
will hit a near-infinite loop. This occured in Rails 2.3.5 with Haml 3.0.18 and fast_gettext 0.5.10.
gettext_i18n_rails
's Haml-Parser compiles the Haml code and delegates the parsing to ruby_parser
. Unfortunately, ruby_parser
appears to be confused when a string contains both escaped chars (that is, any unicode characters as ndash, umlauts etc.) and #{}
blocks, which makes it extremely slow.
The easiest "solution" we came up with was to replace all occurrences of UTF-8 chars wi...
defunkt/jquery-pjax
pjax loads HTML from your server into the current page without a full reload. It's ajax with real permalinks, page titles, and a working back button that fully degrades.
pjax enhances the browsing experience - nothing more.
Fun with Ruby: Returning in blocks "overwrites" outside return values
In a nutshell: return
statements inside blocks cause a method's return value to change. This is by design (and probably not even new to you, see below) -- but can be a problem, for example for the capture
method of Rails.
Consider these methods:
def stuff
puts 'yielding...'
yield
puts 'yielded.'
true
end
We can call our stuff
method with a block to yield. It works like t...
How to test print stylesheets with Cucumber and Capybara
A print stylesheet is easy to create. Choose a font suited for paper, hide some elements, done. Unfortunately print stylesheets often break as the application is developed further, because they are quickly forgotten and nobody bothers to check if their change breaks the print stylesheet.
This card describes how to write a simple Cucumber feature that tests some aspects of a print stylesheets. This way, the requirement of having a print stylesheet is manifested in your tests and cannot be inadvertedly removed from the code. Note that you can...
Reset mysql root password
This article describes how to reset MySQL's or MariaDB's root password on your workstation. It's meant for local development purposes only, don't do this in production. This article will also help you if you have a fairly recent MariaDB version that uses authentication based on linux users instead of passwords for the root user and you prefer using a password for root.
Solution
Step 1 is getting a root mysql shell that allows us to change user credentials. We need to stop the mysql
daemon first and re-start it without authorization c...
Flat Icons & Icon Fonts | CSS-Tricks
Nice list of icon sets that come in the form of fonts.
I recommend Font Awesome.
Impressumspflicht bei Facebook & Twitter: Schnelle Info und Abhilfe
Jedes nicht rein private Profil auf Social Media Portalen muss ein Impressum haben, das heißt insbesondere Unternehmen und Freiberufler (§ 5 Abs.1 TMG). Wer das Impressum dort nicht unter einem eigene Button mit der Aufschrift “Impressum” oder “Kontakt” unterbringt, bietet eine große Angriffsfläche für Abmahner (Quelle: http://rechtsanwalt-schwenke.de/impressumspflicht-bei-google-plus-und-twitter/).
Hier also in kurzen Schritten, wie man ein Impressum erstellt und wie man es bei Facebook und Twitter einbindet:
#1. Impressum erstellen
Ic...
RubyMine: Accessing views and partials from controllers
You can quickly access views that belong to a controller by using the tiny "page with arrow" icon in the gutter:
Access a method's view file
Click the icon next to the method definition in the controller.
If a view file does not yet exist, RubyMine will prompt you for its filename.
All views & partials associated to a controller
For a list of all views and partials that belong to the current ...
Firebug tip: Log DOM Events
This Firebug feature is called simply Log Events and allows developers to log DOM events into the Console panel.
All you need to do is right click on an element in the HTML panel, pick Log Events from the context menu and switch to the Console panel to see the logs in action.
Test your CSS rendering output with GreenOnion
No one wants to cry over regression issues in views; does testing HTML and CSS have to be such a back and forth between designers and devs? Why is it that the rest of the stack can have TDD and BDD but not the presentation layer? Well, GreenOnion is here to help you get the same results on testing front-end styling that you've enjoyed in your unit and integration tests up to now.
GreenOnion records 'skins', which are snapshots of the current state of a view (or any page that a browser can navigate to). The first time that it is run on a view...
Twitter Bootstrap: Base CSS
Explanation of the "bootstrap 2" base CSS.
Contains information about:
- Typography
- Code
- Tables
- Forms
- Buttons
- Icons
Gem development: When your specs don't see dependencies from your Gemfile
When you develop a gem and you have a Gemfile
in your project directory, you might be surprised that your gem dependencies aren't already required in your specs. Here is some info that should help you out:
- Bundler actually doesn't automatically require anything. You need to call
Bundler.require(:default, :your_custom_group1, ...)
for that. The reason why you never had to write this line is that Rails does this for you when it boots the environment. - That also means that if you have an embedded Rails app in your
spec
folder (like [h...
Consul 0.4.0 released
Consul 0.4.0 comes with some new features.
Dependencies
- Consul no longer requires
assignable_values
, it's optional for when you want to use theauthorize_values_for
macro. - Consul no longer uses
ActiveSupport::Memoizable
because that's deprecated in newer Railses. Consul now uses Memoizer for this.
Temporarily change the current power
When you set Power.current
to a power in an RS...
PhoneGap Build
Write your app using HTML, CSS or JavaScript, upload it to the PhoneGap Build service and get back app-store ready apps for Apple iOS, Google Android, Windows Phone 7, Palm, Symbian, BlackBerry and more.
By compiling in the cloud with PhoneGap Build, you get all the benefits of cross-platform development but can still build apps just the way you like.
Capistrano: How to find out which version of your application is currently live
When deploying, Capistrano puts a REVISION
file into your application's release directory. It contains the hash of the commit which was deployed.
If you want to know the currently deployed release, simply SSH to a server and view that file.
$ cat /var/www/my-project/current/REVISION
cf8734ece3938fc67262ad5e0d4336f820689307
Capistrano task
When your application is deployed to multiple servers, you probably want to see a result for all of them.
Here is a Capistrano task that checks all servers with the :app
role.