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 install fonts in Ubuntu
- Put the font files (e.g.
ttf
) into ~/.fonts - Run
fc-cache -v
Or, if you prefer to use the GUI, open each font file and click the "Install" button in the top right of the preview window.
New Firefox and gem versions for our Selenium testing environment (Ubuntu 14.04+)
Firefox 5.0.1, which we were using for most Rails 2.3 projects, does not run on Ubuntu 14.04 any more. Here is how to update affected projects.
-
Update (or create)
.firefox-version
with the content:24.0
If you haven't installed Firefox 24 yet, the next time you run tests with Geordi, it will tell you how to install it. -
On a Rails 2 project:
-
Update your Cucumber-related gems as described in Upgrading Cucumber and Capybara, including
cucumber_spinner
andlaunchy
. -
If you...
-
Fixing tlmgr cannot setup TLPDB
tlmgr
is the TeX Live Manager and responsible for the TeX installation on your (Linux) machine.
If you're getting the message:
(running on Debian, switching to user mode!)
cannot setup TLPDB in /home/dominik/texmf at /usr/bin/tlmgr line 5336.
... tlmgr
has not been initialized. Run this to initialize it:
tlmgr init-usertree
Then, you may set e.g. a global paper size standard: tlmgr paper a4
.
Pitfall: ResourceController overwrites where ActiveRecord enqueues
Defining one callback several times in the same class behaves different in ActiveRecord and ResourceController.
While in ActiveRecord the callbacks are enqueued, they overwrite each other in ResourceController.
ActiveRecord - a common practice
class Post < ActiveRecord::Base
does 'post/behavior'
before_validation :do_something
end
module Post::BehaviorTrait
as_trait do
before_validation :do_something_else
end
end
do_something_else
and do_something
are executed before validation in exactly this order
ResourceC...
Accept nested attributes for a record that is not an association
Note: Instead of using the method in this card, you probably want to use ActiveType's nested attributes which is a much more refined way of doing this.
The attached Modularity trait allows you to accept nested attributes for a record that is not an association, e.g.:
class Site < ActiveRecord::Base
def home_page
@home_page ||= Page.find_by_name('home')
end
does 'a...
How to install the `xelatex` binary on Ubuntu 14.04
Just install the texlive-xetex
package:
sudo apt-get install texlive-xetex
Running integration tests without texlive-xetex
will produce an error during xelatex
execution:
RTeX::Document::ExecutableNotFoundError
Prevent LibreOffice from changing standard styles when you format individual characters
You select some characters, make them bold and suddenly your entire document is bold?
Here's how to fix that:
- Right-click the style in the "Styles and Formatting" window
- Select "Modify"
- Go to the "Organizer" tab
- Unselect "AutoUpdate"
You're welcome.
Scroll a textarea to a given position with jQuery
Browsers make this very hard. Even when you explicitely set the selection inside the textarea (e. g. using jquery-fieldselection) the browser will not scroll the textarea to this selection.
What you can do instead is abuse browsers' behavior that they scroll to the end of a textarea when it gets focus. So we set the textarea's value to the text before the position, then focus it, then reset it to its original value:
function scrollTextareaToPosition($textarea, position) {
var text...
/usr/ports/converters/ruby-iconv This port is marked IGNORE
If you have this problem when you update your FreeBSD Ports:
===>>> Launching child to update ruby19-iconv-1.9.3.547,1 to ruby20-iconv-2.0.0.576,1
===>>> All >> ruby19-iconv-1.9.3.547,1 (17/17)
===>>> Currently installed version: ruby19-iconv-1.9.3.547,1
===>>> Port directory: /usr/ports/converters/ruby-iconv
===>>> This port is marked IGNORE
===>>> Not needed with Ruby 2.0 or newer
===>>> If you are sure you can build it, remove the
IGNORE line in the Makefile and try again.
===>>> Update for ruby19-iconv-1.9.3.547,1 f...
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 ...
JavaScript: How to check if an object is NaN
JavaScript's NaN
("Not a Number") is hard to compare against. It never equals anything, not even itself:
NaN === NaN; // false
Number.NaN === NaN; // false
There is the isNaN
method, but it is not really what you are looking for:
isNaN(NaN) // true
isNaN('hello') // true
Option 1: ES6
The Object.is()
method determines whether two values are the same value. It even works for NaN
:
Object.is(NaN, NaN) // true
Option 2: ES5
The example above shows that simply using isNaN
would match other ...
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.
jQuery: Run an event handler only once
Simply use one(...)
instead of on(...)
. It takes the same arguments.
Managing vendor assets in Rails with Bower
bower-rails
is a great solution for managing vendored assets in your Rails app. It feels especially much more convenient and easier to update assets when going this way.
bower-rails generates a Bowerfile
that works much like the Gemfile
you're used to. Just specify your dependencies and run rake bower:install
. You can find available packages here.
An example Bowerfile
:
# ./Bowerfile
asset 'angular'
asset 'angular-i18n'
asset 'angular-ui-router'
asset 'angu...
Dealing with "TypeError: Converting circular structure to JSON" on JavaScript
JavaScript structures that include circular references can't be serialized with a"plain" JSON.stringify
. Example:
a = { name: 'Groucho' };
b = { name: 'Harpo', sibling: a };
a.sibling = b;
Doing a JSON.stringify(a)
will throw an error:
TypeError: Converting circular structure to JSON
There is not much you can do about that except specifying a custom serializer function that detects and cleans up circular references. There are existing solutions so you do not need to think of one yourself, like <https://githu...
Returning an empty ActiveRecord scope
Returning an empty scope can come in handy, e.g. as a default object. In Rails 4 you can achieve this by calling none
on your ActiveRecord model.
MyModel.none # returns an empty ActiveRecord::Relation object
For older Rails versions you can use the attached initializer to get a none
scope.
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 `...
Reveal the indexes of an ActiveRecord table
p ActiveRecord::Base.connection.indexes(:table_name)
CoffeeScript: How to instantiate a class with an attributes hash
This may be hard to find in the docs, but if you want CoffeeScript classes that instantiate their properties from a hash of attributes (like ActiveRecord), do it like this:
class User
constructor: ({ @name, @email }) ->
# empty contstructor is fine, CoffeeScript will do the magic.
Example:
var batman = new User(name: 'Bruce Wayne', email: 'batman@example.com')
batman.name # => 'Bruce Wayne'
Sending raw JSON data to a member action in a controller spec
This is what worked for me in a Rails 4:
# JSON data as first argument, then parameters
patch :update, { some: 'data' }.to_json, id: id, format: :json
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 ...
Angular Filters
A collection of useful filters for AngularJS, e.g. for fuzzy string searching, displaying numbers as percentages an more.