Researching a new form of HTTP caching optimization - Phusion BlogPhusion Blog
Interesting approach to caching responses directly in the HTTP server, based on the value of an individual cookie.
No word yet how to force cache-invalidation.
mailru/FileAPI
A set of javascript tools for working with files.
It offers different kinds of things:
- A cross-browser JS API to work with
Fileobjects. - Image helper methods, like rotating images, or generating thumbnails in the browser (because you don't want your browser to scale tons of 20MB-JPEGs just for an upload preview)
- Webcam access
- Uploads
When HTML5 support is unavailable, it uses Flash polyfills.
Check out the documentation and demos at their GitHub page.
To install via bower, simply add the `...
Fixing the warning Time#succ is obsolete; use time + 1
Chances are you're seeing the warning repeated a lot of times, maybe thousands of times. Here's how to reproduce the issue:
Example 1
# bad code
(Time.current .. Time.current + 1.hour).include?(Time.current)
# Use Range#cover? instead of Range#include? since the former does no typecasting into integers.
(Time.current .. Time.current + 1.hour).cover?(Time.current)
Example 2
# bad code
Post.where(:created_at => min_date.beginning_of_day .. max_date.end_of_day)
# Use 'BETWEEN x AND y'
Post.where(['posts.created_at BETWEEN...
Angular: Keeping attributes with invalid values in an ngModel
The Angular 1.2 way:
# By default, angular returns undefined for invalid attributes which removes
# the value from the form field's ngModel (which means it's not sent to the
# server, and old values would not be overwritten).
#
# This directive makes sure that form fields with an invalid value return an
# empty string instead of undefined.
for elementType in ['input', 'textarea', 'select']
@app.directive elementType, ->
priority: 1
restrict: 'E'
require: '?ngModel'
link: (scope, element, attributes, ngModelControlle...
Installing Adobe Reader on Ubuntu
Adobe no longer supports their PDF reader on Linux and the official page does not offer it for download. \
However, some files may not display properly on Ubuntu's default PDF reader Evince or PDF forms may not work as expected. Hence, you may still need to use it.
Here is how to install the Adobe Reader (acroread) if you need to:
-
Download the
.debarchive from the Adobe servers (yes, it's still there):cd /tmp && wget http://ardownload.adobe.com/pub/adobe/reader/unix/9.x/9.5.5/enu/AdbeRdr9.5.5-1_i386linux_enu.deb -
I...
"controller as" syntax for AngularJS 1.2+
In the Controller:
// Instead of doing this:
app.controller('TodoCtrl', function ($scope) {
$scope.input = 'ex. buy milk';
});
// Do this:
app.controller('TodoCtrl', function () {
this.input = 'ex. buy milk';
});
In the HTML:
<!-- Instead of this: -->
<div ng-controller="TodoCtrl">
<input type="text" ng-model="input" />
</div>
<!-- Do this: -->
<div ng-controller="TodoCtrl as todo">
<input type="text" ng-model="todo.input" />
</div>
josephschmitt/Clamp.js
Clamps (ie. cuts off) an HTML element's content by adding ellipsis to it if the content inside is too long.
While you can only truncate single lines with CSS by using text-overflow, this small JavaScript library also allows truncating text after multiple lines.
Please note:
- For Internet Explorer, the element you clamp on needs to have its
line-heightset in pixels. A relative value will not work. - There is also the [
-webkit-line-clamp](http:...
tel_to Rails helper for linking phone numbers
When putting phone numbers into web pages, you should use tel: links so smartphone users can click those numbers to call someone.
Here is a small helper method that you can use to simplify this:
def tel_to(text)
groups = text.to_s.scan(/(?:^\+)?\d+/)
link_to text, "tel:#{groups.join '-'}"
end
This will allow you to use human-readable numbers and get clean links:
>> tel_to '(01234) 555 6789'
=> <a href="tel:01234-555-6789">(01234) 555 6789</a>
>> tel_to '+1 555 123-456'
=> <a href="tel:+1-555-123-456...
igor-alexandrov/wiselinks
An amped-up alternative to Turbolinks that differs in points like this:
- It's opt-in instead of opt-out, so you explicitely mark up links that should use Wiselinks
- It can refresh content in a given selector only (think framesets), whereas Turbolinks always refreshes the whole body
- It can submit forms without a page reload
- It can follow redirects from an AJAX response
How to combine "change", "up", and "down" in a Rails migration
Rails migrations allow you to use a change method whose calls are automatically inverted for the down path. However, if you need to some path-specific logic (like SQL UPDATE statements) you can not define up and down methods at the same time.
If you were to define define all 3 of them, Rails would only run change and ignore up and down. However, Rails 4+ features a helper method called reversible:
class MyMigration < ActiveRecord::Migration
def cha...
PSA: Dont allow private gems to be pushed to rubygems.org
If you make a gem with Bundler, you will get a rake release task that will instantly publish your gem to rubygems.org for all the world to admire. For private gems this is very bad.
To make sure this cannot happen, rubygems 2.2+ allows you to restrict eligible push hosts:
Gem::Specification.new 'my_gem', '1.0' do |s|
# ...
s.metadata['allowed_push_host'] = 'https://gems.my-company.example'
end
In case you already messed up, [follow these instructions to get your gem removed](http://help.rubygems.org/kb/rubygems/removing-an-a...
Speed up JSON generation with oj
Using this gem I could get JSON generation from a large, nested Ruby hash down from 200ms to 2ms.
Its behavior differs from the default JSON.dump or to_json behavior in that it serializes Ruby symbols as ":symbol", and that it doesn't like an ActiveSupport::HasWithIndifferentAccess.
There are also some issues if you are on Rails < 4.1 and want it to replace #to_json (but you can always just call Oj.dump explicitely).
Security warning: Oj does not escape HTML entities in JSON
---------...
Angular: Binding an HTML value
To bind an HTML value to ng-bind-html, you need to mark it as "trusted" first. Among other ways, you can do so with a custom filter.
# Filter in Coffeescript...:
@app.filter 'htmlSafe', ['$sce', ($sce) -> $sce.trustAsHtml ]
# ...or JS:
app.filter('htmlSafe', [
'$sce', function($sce) {
return $sce.trustAsHtml;
}
]);
# Usage:
<div ng-bind-html="item.value | htmlSafe"></div>
This is a replacement for the ng-bind-html-unsafe directive which has been removed in Angular 1.2.
:party:
How to repair a corrupt PDF
If you have issues with PDFs, fix them like this: pdftk <corrupted_file>.pdf output <fixed_file>.pdf
Background
I had an issue where an included PDF would not show up in a document created with xelatex. This is the relevant line of LaTeX code:
\AddToShipoutPicture*{ \includegraphics[width=21cm]{/home/dominik/code/ihero/public/system/stationery/original/stationery.pdf} }
The included PDF is a stationery for invoices which users can upload themselves. It did work until someone updated their stationery with a nearly-iden...
bower-rails can rewrite your relative asset paths
The asset pipeline changes the paths of CSS files during precompilation. This opens a world of pain when CSS files reference images (like jQuery UI) or fonts (like webfont kits from Font Squirrel), since all those url(images/icon.png) will now point to a broken path.
In the past we have been using the vendor/asset-libs folder ...
Angular-xeditable :: Edit in place for AngularJS
Angular-xeditable is a bundle of AngularJS directives that allows you to create editable elements.
Such technique is also known as click-to-edit or edit-in-place.
It is based on ideas of x-editable but was written from scratch to use power of angular and support complex forms / editable grids.
When Sass-generated stylesheets print a Encoding::CompatibilityError
We upgraded a Rails 2 application to Rails 3.2 and Ruby 2.1, changed the mysql adapter from mysql to mysql2, but did not activitate the asset pipeline. Instead we used Sass the old-school way (stylesheets in public/sass/*.sass) and relied on stylesheet_link_tag to activate the Sass compiler.
Now all Sass-generated stylesheets inserted the following text into body:before:
Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT
I could get rid of this by removing all generated .css files in `...
HTML5: Allow to choose multiple files with a vanilla file picker
Modern browsers natively suppport file pickers that allow the user to choose multiple files at once. To activate this feature, set the multiple attribute:
<input type="file" name="images[]" multiple />
Or in a Rails view:
<%= file_field_tag "images[]", multiple: true %>
This works in IE10+.
Make sure that the field name ends in [] so your server-side code will parse the incoming files into an array. Obviously this naming convention is not compatible with default Rails nested attribute setters, so you'll need to write a form ...
How to fix: Font too small when reading e-mails in Thunderbird
In Thunderbird, you can set custom font faces and sizes for reading plain-text e-mails. However, Thunderbird sometimes "randomly" does not respect your choices.
This is actually not a bug, but a rather weird feature: Fonts are defined per encoding of e-mails.
Some e-mails will be considered Unicode, some Western (ISO 8859-1), and some maybe yet another encoding.
The advanced font settings dialog by default just opens on "Western". Choose a different encoding from the "Fonts for" dropdown menu and you'll see that your custom font sett...
pdfkit/wkhtmltopdf: When a header is invisible
If you're using the :header_html option in PDFKit (or the corresponding --header-html option in wkhtmltopdf), and the header remains invisible, you need to add this to your header HTML:
<!doctype html>
The same applies to footers via footer_html
I'm sorry.
Capybara will not find links without an href attribute
Capybara and most assistive technology will fail to find <a> tags that are missing an href attribute. This will probably happen to you every now and then on JavaScript-heavy applications.
An example would be an AngularJS application where the following HTML actually works. [1]
<a ng-click="hello()">Hello</a>
Capybara will fail to find that link, even though looking it up via the DOM shows it:
>> find_link("Hello")
Capybara::...
How Exchange handles multipart/alternative emails
In Rails, you can very easily send emails with HTML and plaintext bodies.
However, if you're trying to debug those using your normal email account, you might be out of luck: For some reason, Exchange servers will simply throw away the plaintext part of your mail, and just save the html part.
What we know about PDFKit
What PDFKit is
- PDFKit converts a web page to a PDF document. It uses a Webkit engine under the hood.
- For you as a web developer this means you can keep using the technology you are familar with and don't need to learn LaTeX. All you need is a pretty print-stylesheet.
How to use it from your Rails application
- You can have PDFKit render a website by simply calling
PDFKit.new('http://google.com').to_file('google.pdf'). You can then send the...
Transactional HTML Email Templates
Styling HTML email is painful. Tables, inline CSS, unsupported CSS, desktop clients, web clients, mobile clients, various devices, various providers. All these things have to be thought about and tested. It’s no surprise developers don’t want to deal with this when there is a backlog of more important priorities.
We’ve tried to remove some of the pain for you and open-sourced a collection of common templates for transactional email.
Alternative transactional email templates include
- [these ones...](https://www.sendwithus.com/resource...