# 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

# Using Mocks to Test Mailers in RSpec

I was in the situation where I needed to test the delivery of emails based on specific sets of events. I began testing by inspecting sent emails in ActionMailer::Base.deliveries. However, this method of testing felt verbose and sloppy.

RSpec mocks is a great solution when you need to test that a specific method was called. In my case, I want a fundraiser to receive an email after a campaign goal is reached.

The definition for the method and_call_original is, “You can use and_call_original on the fluent interface to "pass through” the received message to the original method.“ What exactly does this mean? It means that when I call funder.fund, I expect CampaignMailer.notify_curator_campaign_goal_reached to be called.

Calling the original method

# ENV Variables in XCode

Setting ENV variables in XCode in not straighforward. I have broken down how to accomplish setting up an ENV variable in the following videos and code example.

Make a new conifuration file and set any ENV variables you need.

In the debug environment, use the debug.xcconfig file. Then, assign the value to something, in this case, SignUpUrl corresponds with the SIGN_UP_URL, which is set to localhost:4567/sign_up.

The sign_up_url variable is set to localhost:4567/signup.

Configuring ENV Variables in XCode

# Shared Contexts With RSpec

In the past, I used traits with my factories. Below is a simple example of defining a trait:

### Using traits

An example spec:

However, I believe that using a shared context is a clearer approach. Defining the amount in a shared context and overriding the amount allows more control over testing use cases.

### Using a shared context

I especially like the shared context approach because you can have shared context and redefine attributes as needed in specs.

# Auto Layout Xcode for Navigation Bar

Finding auto layout information is difficult. The steps below pin a navigation bar to the top of the screen for all iOS devices.

### Steps

• Make sure “Navigation Bar” is selected on the left menu
• Click the “Pin” button on the bottom right corner
• Click the top, left and right constraints
• Click the Height constraint
• Add the 4 contraints

The above steps “pin” the navigation bar object to the top of the view and stretch the navigation bar to the sides of the view. The height constraint keeps a static height for the navigation bar.