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...
jQuery: Work with text nodes and comment nodes
Nearly all jQuery traversal functions ignore elements that are not HTML tags.
To work with other type of nodes (like text, comment or CDATA sections) you need to:
- Retrieve child nodes
contents()
(which behaves likechildren()
except that it returns all types of child nodes) - Filter manually using either plain Javascript or jQuery's
filter()
method
Example
Let's write a function that takes a jQuery element and returns an array of all child nodes that are text nodes:
function selectTextNodes($container) {
retu...
Vastly increase the usability of Thunderbird's search
These two addons will change your life:
- Search as list
-
This will always open search results in the list views instead of the barely usabely faceted search view.
- Sort search results by date not relevance
-
Does what it says.
MySql lost connection trouble
Directly from the MySql docs:
There are three likely causes for this error message.
Usually it indicates network connectivity trouble and you should check the condition of your network if this error occurs frequently. If the error message includes during query, this is probably the case you are experiencing.
Sometimes the during query form happens when millions of rows are being sent as part of one or more queries. If you know that this is happening, you should try increasing net_read_timeout from its default of 30 seconds to 60 s...
Cucumber: Skipping steps in a scenario outline, based on the current example
In Cucumber, scenario outlines help avoiding tests that are basically the same, except for a few variables (such as different inputs). So far, nothing new.
The problem
Now what if your test should (or should not) do something, like filling in a field only for some tests?
Scenario Outline: ...
When I open the form
And I fill in "Name" with "<name>" # <= we want to do this only occasionally
Then everybody should be happy
Examples:
| name |
| Alice |
| Bob |
You could o...
Fixing Ruby debugger: *** Unknown command: "something". Try "help".
So you have placed a breakpoint somewhere and now want to dig around, but not even inspecting variables is working:
(rdb:3) @order_item
*** Unknown command: "@order_item". Try "help".
The reason is, you must tell the debugger to evaluate your expression. One workaround is to call irb
to open an irb session at your breakpoint. Resume by sending Ctrl+D
twice or by returning to the outer irb with "exit
" and then continuing with "c
".
However, the native debugger command for your issue is eval
(or its shorter alias `e...
6 front-end techniques for Rails developers. Part I: From big ball of mud to separated concerns
Amazing guide how to divide a ball of Javascript spaghetti distinct separate layers (model, view, controller, backend adapter).
It does not use a Javascript framework.
RSpec 3 no longer chooses a spec's type based on its directory
While RSpec 1 and 2 decided that specs inside spec/model
are model specs, and those inside spec/features
are feature specs (and so on), RSpec 3 will no longer do that by default.
This will result in errors such as missing routing helpers, etc.
There are 2 ways to fix this:
-
Explicitly set the type on each spec. For example:
describe '...', type: 'feature' do # ... end
-
Add this to your
spec_helper.rb
(inside theRSpec.configure
block) to restore the old behavior:...
Howto respond html or json in the same controller action with Rails 2
Code snippet tested with Rails 2.3
def index
# ...
if request.xhr?
html = render_to_string(:partial => "list", :layout => false)
respond_to do |format|
format.html { render :text => html }
format.json { render :json => {:html => html, ... } }
end
end
end
Note: Perhaps you ran into ActionView::MissingTemplate
error and this card might help. If you call render_to_string
within the format.json
block, Rails will only look for an index.json
template, but not for an `index.erb...