Angular Filters
A collection of useful filters for AngularJS, e.g. for fuzzy string searching, displaying numbers as percentages an more.
Tagging in Rails 4 using Postgres Arrays
Usage:
class Document < ActiveRecord::Base
  scope :any_tags, -> (tags){ where('tags && ARRAY[?]', tags) }
  scope :all_tags, -> (tags){ where('tags @> ARRAY[?]', tags) }
end
Document.create(title: "PostgreSQL", tags: ["pg","rails"])
Document.any_tags('pg')
Document.all_tags(['pg', 'rails'])
Migration:
class CreateDocuments < ActiveRecord::Migration
  def change
    create_table :documents do |t|
      t.string :title
      t.string :tags, array: true, default: []
      t.timestamps
    end
    add_index  :documents, :ta...
Iterate over any enumerable with an index
tl;dr: Use with_index
ActiveRecord's find_each with index
If you do not provide a block to find_each, it will return an Enumerator for chaining with other methods:
Person.find_each.with_index do |person, index|
  person.award_trophy(index + 1)
end
Ruby's map with index
Similarly, you may need an index when using other methods, like map, flat_map, detect (when you need the index for detection), or similar. Here is an example for map:
people...
rails/activeform
A different take on what we're doing with ActiveType. Since it lives under the rails organization it might be part of Rails 5?
Wrapping Your API In A Custom Ruby Gem
Nice tutorial about packaging Ruby bindings to your API in a Ruby gem, with tests using VCR casettes.
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...
Backup an entire website using wget
You need to copy an entire website? Use wget like this:
wget -kr http://host.tld/
This will fetch all content (-r for recursive) and rewrite links inside the documents to make them suitable for local viewing (-k).
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.
Eager-loading polymorphic associations
To avoid n+1 queries, you want to eager-load associated records if you know you need to access them later on.
The Rails docs say:
Eager loading is supported with polymorphic associations.
This is true, but has some caveats.
Example
Consider the following models:
class Image < ActiveRecord::Base; end
class Video < ActiveRecord::Base; end
class PageVersion < ActiveRecord::Base
  belongs_to :primary_medium, polymorphic: true # may be Image or Video
end
class Page < ActiveRecord::Base
  belongs_to ...
Rails 4.1+: New locale files require a Spring restart
So you want to organize your I18n using multiple .yml files but your Rails 4.1 application simply won't use any extra files in development? Spring is to blame.
Halt spring by running:
spring stop
The next time you spawn a bin/rails console or similar, your new translations will work.
You will need to repeat the above every time you create a new .yml file or rename existing ones.
:(
Pete Hunt: React - Rethinking Best Practices
Great introduction to React.js and the ideas behind it.
Use a Bash function to alias the rake command to Spring binstubs or "bundle exec" fallback
There are different ways to run rake:
- On Rails 4.1+ projects, you have Spring and its binstubs which dramatically improve boot-up time for Rake and similar. You need to run bin/raketo use them.
- On older projects, you want to run "bundle exec rake" to avoid those ugly "already activated rake x.y.z" errors that hit you when different rake versions are installed for your current Ruby.
Here is a solution that gives you a plain rake command which uses a binstubbed bin/rake if available and falls back to bundle exec rake if necessar...
Linux: Running a program with a different locale than your default
When your system is not running on English, you may sometimes want to run some applications and not use your system locale.
Use cases are scripts that parse output, or just using the (possibly) more common English labels or error messages. Here is how to do that.
I will use the date command and print the current weekday, just for the sake of an example.
Changing the locale using environment variables
Most often, setting LC_ALL for your command should be enough. The following was run on a system using a German locale.
$ date +%...
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.
Change the existing order of an ActiveRecord scope
Use reorder to replace an existing order clause with a new expression.
The Curious Case of the Flip-Flop
The flip-flop operator is a hotly contested feature of Ruby. It's still struggling to find an idiomatic use case, except for a few very rarely needed things. It's not something you'll likely reach for on a daily, weekly or even monthly basis. The only thing you really need to know about it is what it does, and that's only in case you encounter it in someone else's code. Many even go as far to say not to use the flip-flop operator, that it only adds confusion.
My brain just melted.
Using rbenv on Ubuntu 18.04+
We will be installing rbenv and ruby-build from our own fork, not from the Ubuntu sources.
Installing rbenv
- 
Install rbenv: git clone https://github.com/rbenv/rbenv.git ~/.rbenvFor Bash: echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(rbenv init -)"' >> ~/.bashrcFor ZSH: echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc echo 'eval "$(rbenv init -)"' >> ~/.zshrcNow reinitialize ... 
Perform Sidekiq jobs immediately in development
# config/initializers/sidekiq.rb
# Perform Sidekiq jobs immediately in development,
# so you don't have to run a separate process.
# You'll also benefit from code reloading.
if Rails.env.development?
  require 'sidekiq/testing'
  Sidekiq::Testing.inline!
end
RSpec example groups can be named using symbols
Though nowhere to be found in the official docs, this works just fine.
describe Facebook::Post do
  it_behaves_like :time_series
end
shared_examples_for :time_series do
  # shared example code
end
Using the Facebook Graph API
App tokens
For server-to-server requests to the Facebook Graph API you can skip requesting an Oauth token, an instead use the combination of app_id|app_secret as your access token. This token will never expire, and should suffice for retrieving basic information from the Graph API.
http://graph.facebook.com/endpoint?key=value&access_token=app_id|app_secret
Since you don't make requests for a certain user, the Graph API might respond with an error in case you're requesting a resource that requires authenticating as a human...
Upgrade to LibreOffice 5.x on Ubuntu
Getting rid of your old LibreOffice
Remove your old LibreOffice:
sudo apt-get remove libreoffice*
You probably also want to get rid of the dead PPAs you might have installed for LibreOffice 4.x:
- Open *Ubuntu Software Center
- Go to Edit / Software Source ... / Other Software
- Find and uncheck lines like http://ppa.launchpad.net/libreoffice/libreoffice-4-3/ubuntu. Note that you will probably have at least two lines pertaining to libreoffice.
Installing a new LibreOffice
-------------...
Git: Diff per word or character
By default git diff highlights whole lines as changes.
To diff on a word-by-word basis you can say:
git diff --color-words
To diff on a character-by-character basis you can say:
git diff --color-words=.
Ubuntu: Fix "An error occurred while installing pg"
If you get an error like this:
An error occurred while installing pg (0.17.1), and Bundler cannot continue.
Make sure thatgem install pg -v '0.17.1'succeeds before bundling.
Then do this:
sudo apt-get install libpq-dev
... and run Bundler again.
Paperdragon: Explicit Image Processing
It's like Paperclip or CarrierWave, but without any automagic integration.