Search_spinner Cross_grey Magnifier
help

Search syntax

  • Search for repeating cards with repeats:yes
  • Search for unfinished cards with unfinished:yes
  • Search for cards in a certain topic with topic:"name"
  • Search for phrases with "a phrase"
  • Exclude a word from search: -word
  • Exclude a phrase from search: -"a phrase"
  • Exclude a topic from search: -topic:"name"
External content

Understanding Ruby Singleton Classes

Good article about ruby singleton classes.

Repeats

Use Rails URL helpers in any Ruby class

The attached Modularity trait will give any Ruby class a method #url_writer, on which you can call URL helpers:

class Foo
  does 'write_urls'
  def self.class_method
    url_writer.session_path
  end
  def instance_method
    url_writer.project_path(5)
  end
end

It works for Rails 2 and Rails 3.

Auto-destruct in 52 days

Updated: Managing vendor libraries with the Rails asset pipeline

Added a notice about how to fix libraries bringing their own (icon) fonts.

Ruby regular expression start/end line vs. start/end string

tl;dr: Most often you want to use \A and \z. Take care of using ^ and $!

^ Start of line
$ End of line

!!("image/jpeg" =~ /^image\/(jpeg|png)$/) => true
!!("image/png" =~ /^image\/(jpeg|png)$/) => true
!!("application/javascript" =~ /^image\/(jpeg|png)$/) => false

!!("application/javascript\nimage/jpeg\nfoo" =~ /^image\/(jpeg|png)$/) => true

\A Start of string
\z End of string

```
!!(“image/jpeg” =~ /\Aimage\/(jpeg|png)\z/) => true
!!(“image/png” =~ /\Aimage\/(jpeg|png)\z/) => true
!!(“application/javascript” =~ /\Aimage\/(…

Repeats

Gatekeeping: Guide for gatekeeper

Note: This has been a private card for some time, because it is heavily tailored to our specific needs and tools. While it will certainly not apply to all (especially larger teams), we thought it might still be helpful as a starting point, and so made it public.


In order to reduce the number of rejects we get from clients, we want to review all code written before it goes to the staging server.

If you’re responsible for gatekeeping in a projects, this is what to do:

First, read the [Gatekeeping for developers](https://makandra…

Manually trigger a delegated DOM event

When you register a delegated event using on (or the deprecated delegate / live), it is somewhat hard to manually trigger these events manually, e.g. for testing.

After trying jQuery’s trigger to no avail, I had success by using native Javascript methods to create and dispatch an event. For instance, to trigger a mousedown event:

```
element = $(‘…’).get(0);
event = new MouseEvent(‘mousedown’, { view: window, cancelable: true, bubbles: true }…

Querying model errors in Rails 4

ActiveModel supplies an errors object that behaves similar to a Hash. It can be used to add errors to a record, as well as to query the record for registered errors. This object is returned when calling <object>.errors:

errors = User.new.errors # => #<ActiveModel::Errors ...>

Here are some helpful messages of its API:


[<attribute name>]
:Returns an array of error messages on that attribute. Example: errors[:name] => ['is missing']


add_on_blank(<attribute list>) (similarly add_on_empty)
Registers an error …
Repeats

How to stub class constants in RSpec

Hint: There’s another card with this helper for Cucumber features.


Sometimes you feel like you need to stub some CONSTANT you have defined in an other class. Since actually constants are called constants because they’re constant, there’s no
way to easily stub a constant.

Here are three solutions for you.

#Easiest solution
Rethink! Do you really need CONSTANT = %w[foo bar] to be constant? In many cases, setting it as a sim…

Beautiful and simple signatures in your browser

Signature Pad is a JavaScript library for drawing smooth signatures. It’s HTML5 canvas based and uses variable width Bézier curve interpolation based on Smoother Signatures post by Square. It works in all modern desktop and mobile browsers and doesn’t depend on any external libraries.

Demo

Repeats

Test if two date ranges overlap in Ruby or Rails

A check if two date or time ranges A and B overlap needs to cover a lot of cases:

  • A partially overlaps B
  • A surrounds B
  • B surrounds A
  • A occurs entirely after B
  • B occurs entirely after A

One trick to cover all these cases with a single expression is to see if the start date of each range is looking at the end date of the other range in the same direction.

The code below shows how to implement this in Ruby on Rails. The example is a class Interval, which has two attributes #start_date and #end_date. These dates are considere…

Repeats

sessionStorage: Per-window browser storage

All major browsers (IE8+, FF3.5+, Safari 4+, any Chrome) support sessionStorage, a JavaScript storage object that

  • survives page reloads and browser restores,
  • but is different per new tab/window (in contrast to localStorage).

MDN says:

The sessionStorage object is most useful for hanging on to temporary data that should be saved and restored if the browser is accidentally refreshed

Demo

Example usage:

```
// Save data to the current sess…

Repeats

Javascript: Compare two arrays for equality

Don’t Google this, you will lose all will to live. Instead use Object#isEqual from Underscore.js:

_.isEqual([1, 2], [2, 3]) // => false
Auto-destruct in 52 days

Updated: Asset Pipeline Basics

Highlighted that any required path must be relative to either the current directory OR a Rails asset path.

External content

RegExr: Learn, Build, & Test RegEx

RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

Reverse-proxying web applications with Apache 2.4+

A reverse proxy is a “man in the middle” server that tunnels requests to another server. You can use for things like:

  • Expose a local service that you cannot directly reach over the internet
  • “Change” the domain or path of a web application by rewriting them on the fly
  • Instantly change servers that respond to a name or IP, without relying on DNS TTL

The following describes various Apache 2.4+ VHost directives to reverse-proxy an application that does not want to be reverse-proxied. The exam…

Repeats

The many gotchas of Ruby class variables

TLDR: Ruby class variables (@@foo) are dangerous in many ways. You should avoid them at all cost.

Class variables are shared between a class hierarchy

When you declare a class variable, it is shared between this and all descending (inheriting) classes. This is rarely what you want.

Class variables are bound at compile-time

Like unqualified constants, class variables are bound to your current scope when the .rb file is being parsed.
This wi…

Show details of TLS/SSL connections of remote hosts

sslscan is a nice tool to show details about TLS/SSL connections:

```
~> sslscan some-host-at.makandra.de

Testing SSL server some-host-at.makandra.de on port 443

Supported Server Cipher(s):
Failed SSLv3 256 bits ECDHE-RSA-AES256-GCM-SHA384
Failed SSLv3 256 bits ECDHE-ECDSA-AES256-GCM-SHA384
Failed SSLv3 256 bits ECDHE-RSA-AES256-SHA384
Failed SSLv3 256 bits ECDHE-ECDSA-AES256-SHA384
Rejected SSLv3 256 bits ECDHE-RSA-AES256-SHA …

Prefered Server Cipher(s):
TLSv1 128 bits ECDHE-RSA-A…

2809 cards