View

Middleman: Use pretty URLs without doubling requests

By default Middleman generates files with a .html extension. Because of this all your URLs end in /foo.html instead of /foo, which looks a bit old school.

To get prettier URLs, Middleman lets you activate :directory_indexes in config.rb. This makes a directory for each of your pages and puts a single file index.html into it, e.g. /foo/index.html. This lets you access pages with http://domain/foo.

Don't double your requests!

Unfortunately you are now forcing every br…

Quickly printing data in columns on your Ruby console

Dump this method into your Ruby console to quickly print data in columns. This is helpful for e.g. comparing attributes of a set of Rails records.

def tp(objects, *method_names)
  terminal_width = `tput cols`.to_i
  cols = objects.count + 1 # Label column
  col_width = (terminal_width / cols) - 1 # Column spacing

  Array(method_names).map do |method_name|
    cells = objects.map{ |o| o.send(method_name).inspect }
    cells.unshift(method_name)

    puts cells.map{ |cell| cell.to_s.ljust(col_width) }.join ' '
  end

  nil
end

Usag…

External content

Git: undo delete

Assuming you're wanting to undo the effects of git rm or rm followed by git add -A or something similar:

This restores the file status in the index:

git reset -- <file>

then check out a copy from the index

git checkout -- <file>

To undo git add , the first line above suffices, assuming you haven't committed yet.


Note:

Make sure to use double dashes -- to tell git to checkout a file instead of a branch. This only is relevant for [files having the same name as a branch](https://git-scm.com/docs/git-ch…

Repeats

Casting ActiveRecord scopes or instances to ActiveType extended model classes

When working with ActiveType you will often find it useful to cast an ActiveRecord instance to its extended ActiveType::Record variant.

Starting with active_type 0.4.0 you can use ActiveType.cast for this:

class User < ActiveRecord::Base
  ...
end

class SignUp < ActiveType::Record[User]
  ...
end

user = User.find(1)
sign_up = ActiveType.cast(user, SignUp)
sign_up.is_a?(SignUp) # => true

This is basically like [ActiveRecord#becomes](http://apidock.com/rails/v4.2.1/ActiveRecord/Pers…

Howto: Require gem that is not in Gemfile

Example: Require awesome_print in the console while it is not in Gemfile or .gemspec.

require 'awesome_print' => LoadError: cannot load such file -- awesome_print

gem install 'awesome_print'

$: << `/home/xxx/.rvm/rubies/ruby-x.x.x/lib/ruby/x.x.x/gems/awesome_print-x.x.x/lib`
require 'awesome_print' => true

Note: To find out the path quickly just look at $: to get the base path of your ruby dir and then append the gem with version of the output of gem install.


Alternatives:
Adding the gem to the developme…

Repeats

Use a global .gitignore file to ignore stuff from your machine

Sometimes you want git to ignore certain files that appear on your machine. You can do this in 3 ways:

  • Per project, in the project's .gitignore file
  • Per project, in a local exclude file
  • Globally on your machine

Downsides of per-project .gitignore entries

While it might be tempting to set it per project (other devs might benefit from it), you

  • need to do it each time for every project
  • "pollute" a project's .gitignore file with stuff…

Middleman for Rails Developers

Middleman is a static page generator that brings many of the goodies that Rails developers are used to.

Out of the box, Middleman brings Haml, Sass, helpers etc. However, it can be configured to do even better. This card is a list of improvement hints for a Rails developer.

Gemfile

Remove tzinfo-data and wdm unless you're on Windows. Add these gems:

```
gem 'middleman-livereload'
gem 'middleman-sprockets' # Asset pipeline!

gem 'bootstrap-sass' # If you want to use Bootstrap

gem 'byebug'

gem 'capistrano'
gem 'capistrano-mid…

Repeats

Your database tables should always have timestamps

Whenever you create a table from a database migration, remember to add updated_at and created_at timestamps to that table. Without those timestamps, investigating future bug reports will be hell. Always have timestamps.

Adding timestamps to new tables

When you create a table using create_table, you can add timestamps by using the timestamps shortcut:

```
class CreateEpisode < ActiveRecord::Migration
def change
create_table :episodes do |t|
t.string :name
t.timestamps

Repeats

Faking and testing the network with WebMock

An alternative to this technique is using VCR. VCR allows you to record and replay real HTTP responses, saving you the effort to stub out request/response cycles in close details. If your tests do require close inspection of requests and responses, Webmock is still the way.


WebMock is an alternative to FakeWeb when testing code that uses the network. You sh…

External contentRepeats

Howto: Write a proper git commit message

Seven Rules

  1. Separate subject from body with a blank line
  2. Limit the subject line to 50 characters (max. 72)
  3. Capitalize the subject line
  4. Do not end the subject line with a period
  5. Use the imperative mood in the subject subject line
  6. Wrap the body at 72 characters
  7. Use the body to explain what and why vs. how

5. Use the imperative mood in the subject subject line (partially extracted)

If applied, this commit will your subject line here

Good:

  • If applied, this commit will **refactor subsystem X for readabili…
Repeats

Touch devices don't have mouseover events

It might sound trivial, but there is no such thing as a "hover" or "mouseover" state on touch devices. If your application is supposed to work on iPads, smartphones, etc., don't hide information behind a tooltip, and don't make controls appear when hovering over another element.

Generally, things that happen/appear when you hover an element should do the same when you click the element.

Repeats

Storing trees in databases

This card compares patterns to store trees in a relation database like MySQL or PostgreSQL. Implementation examples are for the ActiveRecord ORM used with Ruby on Rails, but the techniques can be implemented in any language or framework.

We will be using this example tree (from the acts_as_nested_set docs):

```text
root
|
+– Child 1
| |
| +– Child 1.1
| |
| +– Child 1.2
|
+– …

Capistrano 3: How to deploy when a firewall blocks your git repo

Sometimes, through some firewall or proxy misconfiguration, you might have to deploy to a server that cannot access the git repository. You should have this fixed, but as a temporary workaround, you can tunnel git access through SSH.

Follow these steps:

  1. Log in to the remote server and delete the cached repo (in project_root/repo).
  2. Open a separate SSH connection for each target server and forward an SSH port like this:

    ssh deploy-user@server -R 1222:git.host:22

  3. Set the repo url in your deploy.rb to `ssh://git@loca…
Repeats

Cucumber: Prevent outstanding AJAX requests to bleed into other scenarios

When using Cucumber with Selenium or some other Javascript-enabled browser, AJAX request can sometimes "bleed" into the next scenario. This happens when a scenario triggers an AJAX request near the end, but does not wait for it to complete.

To avoid this, add the attached code to your cucumber's /features/support directory. It will disable AJAX requests at the end of the scenario and wait for outstanding requests to complete.

Requires spreewald and only works for requests triggered by jQuery.

Repeats

Cucumber step to match table rows with Capybara

These steps are now part of Spreewald.

This note describes a Cucumber step that lets you write this:

Then I should see a table with the following rows:
  | Bruce Wayne       | Employee    | 1972 |
  | Harleen Quinzel   | HR          | 1982 |
  | Alfred Pennyworth | Engineering | 1943 |

If there are additional columns or rows in the table that are not explicitely expected, the step won't complain. It does however expect the rows to be ordered as stat…

Repeats

RSpec: Debug flickering test suites with rspec --bisect

In modern default RSpec configurations, your tests are usually run in random order. This helps to detect "flickering" tests that only fail when run in a certain order.

The reason for this are tests that have side effects causing other tests to fail later. The hard part is to find the offending test.

Enter rspec --bisect:

  1. Say you have a flickering test that passes on its own, but you just saw it fail in a full test run. At the top of the RSpec output, you will see a message like Randomized with seed 12345. Take a note of the number….
Repeats

Cucumber pitfall: "Around" does not apply to your "Background" steps

Around will not happen until after a feature's Background has been processed. Use Before and After to avoid that.

Details

Consider this Cucumber feature file:

Feature: Something that needs to be tested

  Background:
    Given a user
      And I sign in        
    
  Scenario: Sign out
    When I sign out
    Then I should see "Signed out"
  
  Scenario: Something else
    # ...

Now, assume you have these step definitions:

Around do
  puts "** Around: before yield"    ...

Classic CSS problems that are easy with flexbox

Solved with flexbox is a collection of css problems which were hard or impossible to solve without flexbox:

  • Better, Simpler Grid Systems
  • Holy Grail Layout
  • Input Add-ons
  • Media Object
  • Sticky Footer
  • Vertical Centering
View
3345 cards