# Level Up With Vim

How much time do you spend in your text editor? We spend most of our time reading through text and jumping between files. My goal is to find a tool in order to help me do this more efficiently. I have found that learning Vim is not overwhelming or difficult if you break the units of work down into manageable pieces. The ultimate goal is to use Vim fulltime. Below are my curated steps:

##### Vimtutor
• Comprehensive introduction to Vim
• Type vimtutor in a Terminal session
• Complete a few sections per day

# Filtering Ack Results

While setting up my VIM environment, I read a blogpost which states ack can ignore directories. For cleaner ack output you can setup the file below:

# Rails before_action Method

I did not know you could give a before_action a block argument. This is a benefit because you do not have to create a ruby method and pass the method into the before_action.

This got me thinking, what does before_action look like in Rails? The following is the source code from Rails 4.2.7, from AbstractController::Callbacks::ClassMethods.

And I can do cool things like:

I would like to point out the set_callback method.

# Extend and Include in Ruby

I have been trying to clean up some old code with Ruby modules. This post is to help me remember the differences between include and extend in Ruby.

# Find and Replace Text

If you find yourself in the situation of needing to find and replace text in multiple files, use the Command Line Interface. I wanted to rename a Phoenix application and ran the below command.

ack pivotal_commentor -l | xargs sed -i '' 's/pivotal_commentor/commentor/g'

Let’s break this command down with a simple example.

And lets add the text ‘hello’ to both text files.

ack is like grep and found the files containing the string hello. The -l flag “Only print filenames containing matches”

xargs is a Unix utility that constructs argument lists and is smashing the filenames into one line.

Finally, we pipe the arguments from xargs to sed.

The -i flag allows for in place editing on the file. In the regex, the s replaces hello with bye and the g indicates globally, in case hello is found more than once in the file. The ‘’ is sending the change into the correct output file.

# JQuery beforeSend Function

I have a form which submits a comment via AJAX. The app was throwing validation errors if there was no value in the comment field. My initial solution was to disable the submit button if the comment is blank. However, using the beforeSend function provides a cleaner solution.

With the above code, the comment form is never posted if the comment is blank.

# ActiveRecord None

The .none class method was introduced in Rails 4.0.2 and is helpful when returning a blank array will break.

none

# Submitting a Get Request in Rails

For the error “WARNING: Can’t verify CSRF token authenticity”, add a CSRF token.

The top-level block contains method: :get which doesn’t auto-add the needed authenticity_token.

Understanding the Rails Authenticity Token

# RSpec Example Blocks

I recently discovered an interesting test pattern; defining variables in before and after hooks in the rails helper.

### A Real Example

Let’s say we are testing a controller and want to make sure only an admin has access to specific views.

When setting admin_signed_in to true in a describe block, we have access to an admin user. I think it is cleaner to set up these users in a hook instead of a shared context or explicitly in the spec.

Hooks