access control allow origin localhost 3000

To access stuff locally do something like:

1
2
3

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome  --disable-web-security

to turn off the Chrome security stuff and get the request out.

August 09, 2011

Focus on the unknown

Lean startups, the idea behind rails, most business books, and tons of other literature is basically just saying:

“Spend as much time as possible on what you are doing that is unique.”

It’s easy to forget this. I think it’s worthwhile to, at least once a week, have a retrospective where the entire team thinks about what they focused on over the past week that wasn’t unique to the problem the company is trying to solve. Once all those things are on the table, everyone can figure out ways to make it so that they don’t have to focus on those things.

E.g. Spending too much time on e-mail servers? Use sendgrid.

Spending too much time deploying? Use heroku.

Spending too much time managing someone? Setup a learning plan for them.

Spending too much time debugging? Make the code clearer.

Spending too much time arguing? Agree on a few core principles and then refer to them.

Spending too much time speculating? Find an expert and ask them. Go direct.

Etc.

February 24, 2011

Project search vs My kitchen

On small-medium sized codebases, project search should not be necessary. A commentator on my last post wrote: “Replace ’/’ with Ack.vim…Problem solved? ” I disagree.

The argument is that:

a) given that it is relatively trivial to search the entire project for any given line, method, or file, b) therefore it is ok to have things be slightly harder to find

Going further, the argument is that:

a) given project search b) code organization is less important

This reminds me of how I recently organized my kitchen. Initially, everything was in mostly random places. If I wanted to find a spatula, it could be in one of three drawers, and I had to walk around to find it. If I wanted a pan, I had to walk across the room from the stove to a drawer.

I reorganized it so that everything that I use at any position in the kitchen is in the drawer right in front of me. Spatulas and things I use on the stove are in a little stainless steel by the stove. Pots and pans are directly to the right of the stove. The cutting board is close to the knives. Etc.

This kitchen refactoring makes my life so much better. I can focus on cooking instead of on finding things.

Before, I was basically “project searching” around my kitchen. The problem is that when you’re looking for a knife, it’s hard to find if it’s in a drawer full of other things. That is generally how project searches happen, you search for something, then have to look through at least 2 files before you find what you’re looking for.

Furthermore, you lose context. In the same way that I had to walk across the kitchen to get a pan and then walk back to the stove, by having to go into another place that project search takes you, you end up having to cycle back and forth between files to see the context.

Obviously, there is a balance somewhere. I have been using ack.vim for about a year now.

Having all your code in one file would suck. The idea is that whenever you want to do something, the tools you need to do it, and the things you need to change should be easily accessible.

February 23, 2011

On partials

I am constantly going in and making changes.

My process is:

open up the page (e.g. contacts#show)

use vim’s ”/” key to find what I want to edit

edit it

When there are lots of partials this becomes

open up the page (e.g. contacts#show)

use vim’s ”/” key to find what I want to edit

realize that it is not on the page

think “where is it?”

search the page for partials

guess which partial what I want is in

if I am wrong, guess another partial

find the partial

use vim’s ”/” key to find what I want to edit

edit it

If a partial is only being used once, how is it adding value given the time increase?

January 28, 2011

Rails 3 Cucumber / Machinist Tutorial: Machinist with Cucumber in 10 minutes

Erwin was kind enough to update my cucumber and machinist tutorial for Rails 3. This should be really useful for people who want to use cucumber to test with Rails 3. He notes that this is current with Rails3.rc and ruby 1.9.2.rc.1.

You can find his full repo here.

Rails 3 cucumber tutorial

So, here’s how to start using machinist with cucumber in a rails 3 app. I’ll use a simple app that deals with strawberries.

Start a rails app

1
2
3
4
5
6
7
8
9
10
11
12
13

# sudo gem install rails --pre --source=http://gems.rubyonrails.org
# or use rvm to test Rails 3

rails new strawberries
cd strawberries
#  install http://github.com/ryanb/nifty-generators
#To use Nifty Generators with Rails 3 you will need to include it in your Gemfile.
  gem "nifty-generators"

rails g nifty:layout
# other generators :  nifty:scaffold, nifty:config, nifty:authentification

Add them to your Gemfile, so that other folks on your team can easily get them.

1
2
3
4
5
6
7
8
9
10
11

gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'rspec-rails'
gem 'spork'
gem 'launchy   # so we can use : Then show me the page

gem 'machinist', '>= 2.0.0.beta1'
gem 'faker'

1
2
3
4
5
6
7
8

#Then install the gems by running:
 bundle install

# generate schema
rake db:migrate
# or alter /config/boot.rb if you do not intend to use a database

Generate the files

From your rails app’s root, enter:

1
2
3
4
5
6
7
8
9


rails g cucumber:install --capybara
rails g machinist:install

touch features/support/blueprints.rb
# add this line in it
require 'machinist/active_record'

Write a story

Now that we have our environment setup, let’s add the stories. These are just sketches of how our app should behave.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

@wip : cucumber tag for work-in-progress


# features/strawberries.feature
Feature: Live
  In order to live
  people
  want to be able to eat strawberries
 
@wip 
  Scenario: Eating strawberries
    Given a strawberry that is "blue"
    When I go to the homepage
    Then I should see "There is 1 strawberry"
    When I follow "eat the blue strawberry"
    Then I should see "Strawberry eaten!"

Implement steps

It’s a good idea to implement steps first, so that you can have failing steps to work with. Cucumber will provide step definitions for us, so just enter:

1
2
3

rake cucumber:wip

Now we can add the step definition for the strawberries.

1
2
3
4
5
6
7
8


#features/step_definitions/strawberry_steps.rb
Given /^a strawberry that is "(.*)"$/ do |color|
  Strawberry.make!(:color => color)
end
  

Now let’s try rake cucumber:wip This time we get

1
2
3
4

1
uninitialized constant Strawberry (NameError)

Which means it’s time to start adding code.

Implement feature and add blueprints</h3

Now that we’ve written some tests, we can go ahead and build this.

1
2
3
4

rails g nifty:scaffold strawberry color:string
rake db:migrate

Again, I’m using the nifty generator, which saves lots of time for making quick demo apps. Also, we’re going to want to add a strawberry blueprint, so that we can use Machinist. So, again, let’s try

1
2
3
4

rake cucumber:wip
# => No master blueprint defined for class Strawberry

This time we get No blueprint for class Strawberry (RuntimeError). So, let’s add a blueprint:

1
2
3
4
5
6
7

#features/support/blueprints.rb

Strawberry.blueprint do
  color {Faker::Lorem.words(1).first.downcase}
end

This gives us the ability to say Strawberry.make, which will create a new strawberry with a random color. The nice part about machinist is that we can do both Strawberry.make and Strawberry.make(:color => ’red’), which becomes really useful when you have a model with a lot of validations that you don’t want to have to specify every time. Machinist lets you focus on specifying only what’s important to the specific test. Now, at this point, since I used the nifty_scaffold gem, we just have to change some text to make the feature pass, and we’re done.

So, now that we have a blueprint, let’s try rake:features again. We get undefined method `root_path’ for # (NoMethodError).

So, to fix this, let’s add a root path and delete public/index.html.

1
2
3
4
5

# config/routes.rb

root :to => "strawberries#index"

And then try rake features again. This time we get:

1
2
3
4
5

Then I should see "There is 1 strawberry"  # features/step_definitions/web_steps.rb:89
      expected: /There is 1 strawberry/m,
           got: "<! #etc etc

So, what’s happening here is that cucumber/webrat are going to our home page, but they aren’t finding the text that we wrote that we wanted to see in the story. So, let’s change it.

1
2
3
4

# app/views/strawberries/index.html
<h1>There is <%= "#{Strawberry.count}" -%> strawberry</h1>

Now if we try rake features again we get Could not find link with text or title or id “eat the blue strawberry” (Webrat::NotFoundError) So we add that link

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

<h1>There is <%= "#{Strawberry.count}" -%> strawberry</h1>
<table>
  <tr>
    <th>Color</th>
  </tr>
  <% @strawberries.each do |strawberry| %>
    <tr>
      <td><%=h strawberry.color %></td>
      <td><%= link_to "Show", strawberry %></td>
      <td><%= link_to "Edit", edit_strawberry_path(strawberry) %></td>
      <td><%= link_to " eat the #{strawberry.color} strawberry", strawberry, 
:confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
  <% end %>
</table>

If I try rake cucumber:wip again, I see that now only the last step is failing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Then I should see “Strawberry eaten!” # features/step_definitions/web_steps.rb
expected: /Strawberry eaten!/m,

000

So, let’s fix that too.

---RUBY

# app/controllers/strawberries_controller.rb

  def destroy
    @strawberry = Strawberry.find(params[:id])
    @strawberry.destroy
    flash[:notice] = "Strawberry eaten!" #change the flash
    redirect_to strawberries_url
  end

So, now we have 5 passing steps

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

…./strawberries$ rake cucumber:wip

Feature: Live
In order to live  # features/strawberries.feature
  people
  want to be able to eat strawberries
  Scenario: Eating strawberries                # features/strawberries.feature:5
    Given a strawberry that is "blue"          # features/step_definitions/strawberry_steps.rb:2
    When I go to the homepage                  # features/step_definitions/webrat_steps.rb:6
    Then I should see "There is 1 strawberry"  # features/step_definitions/webrat_steps.rb:89
    When I follow "eat the blue strawberry"    # features/step_definitions/webrat_steps.rb:14
    Then I should see "Strawberry eaten!"      # features/step_definitions/webrat_steps.rb:89


1 scenario
5 steps passed

Machinist note So, hopefully this is enough for you to get started with. The value of machinist really comes later, when you have a model with more than 1 field, and when you need to make a bunch without wanting to specify everything every time. For example:

1
2
3
4
5
6
7

User.blueprint do
  password {'password'}
  password_confirmation {'password'}
  name {Faker::Lorem.words(1).first}
end

Here, it would be annoying to have to specify the passwords each time, so it’s easier to let machinist do it, so that you can just say User.make!, or User.make!(:name => ’mischa’).

August 04, 2010