0

Fixing inital Rubocop errors on Rails app

Posted by Jason Noble on 03/25/2014 in Uncategorized |

tl;dr: Solution

I’m an Ruby on Rails instructor for the DaVinci Coders program in Louisville, CO. I teach a class called Building the Toolbelt of a Junior Ruby on Rails Developer, where we teach students how to use Ruby on Rails, as well as the other tools software engineers use in their daily work.

Recently, we started working on the group projects. The group projects are the capstone project where each student pairs with one or more other students in implementing a fully developed Ruby on Rails application. This provides students the experience of coding a project from scratch, including Inception, adding/writing Tracker stories, and collaborating with others.

The first project is Geoviz. This project is going to allow users to upload datasets to the site, and allow for the creation of graphs, maps and data display.

One of the gems that they added to the project was Rubocop, which they tied into their Guard via Guard-Rubocop.

I was amazed to see A LOT of “offenses”, i.e. “39 offenses detected”. This is an app that has nothing more than the output of “rails new _____”, so that was rather surprising. Here’s how we fixed it:

Solution

We first started with running `rubocop –auto-correct`. This corrected 14 offenses by changing double quoted strings to single quoted strings. You could disable this test as well, but I think it’s “good” to use single quotes.

The next one we needed to solve:

app/controllers/application_controller.rb:1:1: C: Missing top-level class documentation comment.
class ApplicationController < ActionController::Base
^^^^^

We decided this wasn’t really that helpful of a test, so we disabled it by creating a .rubocop.yml file

Documentation:
  Enabled: false

This got rid of another 4 offenses.

The rest of the offenses were lines being too long:

config/routes.rb:15:80: C: Line is too long. [80/79]
  # Example of named route that can be invoked with purchase_url(id: product.id)

I guess the Rails core team likes LOOOOOOONG lines. I simply edited the files and split the long lines into two < 80 character lines.

24 files inspected, no offenses detected

Yay!

1

Solution: Gem::RemoteFetcher::FetchError: SSL_connect issues

Posted by Jason Noble on 10/08/2013 in Uncategorized |

One of my previous students, Ross Kinney recently blogged about an error he encountered:

Gem::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://s3.amazonaws.com/production.s3.rubygems.org/gems/multi_json-1.7.7.gem)

It turns out that some of the OS X SSL Certs were out of date and he fixed it by running:

rvm osx-ssl-certs update all

I had initially filed that away under “Good to know”, but after this weekend and yesterday, I’ve used this solution a half dozen or more times. Thanks Ross Kinney!

1

Updating requested_by for a bunch of Pivotal Tracker stories

Posted by Jason Noble on 07/18/2013 in Pivotal Tracker, ruby |

I recently imported a bunch of stories into Pivotal Tracker via the Import CSV function. This worked like a charm, the problem was I wanted all the stories to be requested by a different user than what was in the CSV file.

I could use the web browser to do this, but that would take a while to update 85 stories.

I pulled out the Pivotal Tracker gem (Github | RubyGems) to see if it could help.

$ gem install pivotal-tracker
$ irb
>> require 'pivotal-tracker'
 => true
>> PivotalTracker::Client.token = '12345678asdfghj'
 => "12345678asdfghj"
>> project = PivotalTracker::Project.find(456789)
 => #<PivotalTracker::Project:0x007fd128b9fe50 ... >
>> project.stories.all(:requested_by => 'John Doe').count
 => 85
>> project.stories.all(:requested_by => 'John Doe').each{|story| story.update(:requested_by => 'Jane Doe') }
 => [#<PivotalTracker::Story:0x007fd12a28fa48 ... ]
>> project = PivotalTracker::Project.find(456789)
 => #<PivotalTracker::Project:0x007fd128b9fe50 ... >
>> project.stories.all(:requested_by => 'John Doe').count
 => 0
>>

Note, I had to reload the project because the stories get cached by the pivotal-tracker gem.

Tags: , , ,

Mysql 5.6.12 and mysql2 ruby gem 0.3.12

Posted by Jason Noble on 07/16/2013 in mysql, ruby with Comments closed |

I recently updated my Homebrew install and installed the latest mysql (brew update && brew upgrade mysql) to 5.6.12.

However, when I tried to upgrade my mysql2 gem from 0.3.11 to 0.3.12, I was getting the following error:

*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
	--with-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/Users/jasonn/.rvm/rubies/ruby-1.9.3-p392/bin/ruby
	--with-mysql-config


Gem files will remain installed in /Users/jasonn/.rvm/gems/ruby-1.9.3-p392@rails3.1/gems/mysql2-0.3.12 for inspection.
Results logged to /Users/jasonn/.rvm/gems/ruby-1.9.3-p392@rails3.1/gems/mysql2-0.3.12/ext/mysql2/gem_make.out

An error occurred while installing mysql2 (0.3.12), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.12'` succeeds before bundling.

After some googling and stack overflow answers that didn’t help, I ran across #20788, which referenced 9dd1c38 as a possible fix.

It appears that mysql 5.6.12 has an issue (69645) with compiler flags that won’t work on OS X. The fix was to edit the mysql_config file (Around line 119) and remove the -W :

cflags="-I$pkgincludedir  -Wall -Wno-null-conversion -Wno-unused-private-field -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!
4:31
cxxflags="-I$pkgincludedir  -Wall -Wno-null-conversion -Wno-unused-private-field -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!

becomes

cflags="-I$pkgincludedir  -Wall -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!
cxxflags="-I$pkgincludedir  -Wall -Os -g -fno-strict-aliasing -DDBUG_OFF " #note: end space!

After this change I was able to install the mysql2 0.3.12 rubygem without issue.

Tags: , , ,

Github unable to reopen a closed pull request

Posted by Jason Noble on 09/11/2012 in Uncategorized with Comments closed |

Ran into an interesting issue at work today. My pair (Dave) and I opened a pull request yesterday and I checked the status this morning. It showed that Dave had closed it this morning. I asked him if he had intended to merge it, and he said he hadn’t closed it.

The weird thing was that when looking at the pull request, reopening the pull request was not an option. I had not seen that before, so I started asking around.

So, long story short, it turns out that Dave was cleaning up his branches and accidentally deleted the branch that the pull request was based on. This caused Github to close the pull request because the branch it was based on no longer existed.

I fixed this by re-pushing the branch and refreshing the pull request page. At that point, I had the ability to re-open the pull request.

How do I turn off logging in Rails 3?

Posted by Jason Noble on 05/22/2012 in Uncategorized with Comments closed |

I have a script I want to run in rails console, but I don’t want to log the SQL and such. So I googled how to turn off all logging in Rails 3 console. Unfortunately, all the answers were old, so here’s how I did it:

Logging.logger.root.level = 'off'

Rubymine print current line

Posted by Jason Noble on 01/28/2012 in Uncategorized with Comments closed |

One way of debugging your ruby scripts is to print out the value of variables in different sections of your code.

Rubymine has a feature called live templates that allows you to type a short command and have RubyMine extract it in to full blown code. Some built in shortcuts exist, such as def (creates a method), desc (create a RSpec test), it (RSpec test), etc.

To create your own, go to RubyMine -> Preferences -> Live Templates. Click one of the existing templates and hit Copy.

I named mine pl (Put Line Number) and put the following content in:

puts “#{__FILE__:#{__LINE__}”

Now when I’m typing code, I can hit the following:

pl<TAB>
p “Hello”

And it will print out the following:

/Users/jasonn/source/project/junk.rb:1
“Hello”

I’ve found this helpful for tracking down where a specific print statement is located.

Updated: Rails3 Custom Environment Variables

Posted by Jason Noble on 12/03/2011 in ruby on rails with Comments closed |

A while back, I wrote a blog post on adding custom environment variables to Rails3.

This has gotten much easier with a change made by Jose Valim back in March.

The change allows you to create your own custom configuration variables like so:

# config/environments/development.rb
Demo::Application.configure do
  [...]
  config.json_url = "http://www.domain.com"
  [...]
end
# config/environments/test.rb
Demo::Application.configure do
  [...]
  config.json_url = "http://test.domain.com"
  [...]
end

This allows you to add any configuration variables you want to your Rails app. What if you have a bunch of configuration values? You don’t want to pollute your config.* namespace. Wouldn’t it be helpful to have config.my_app.json_url and config.my_app.other_url?

That’s easy enough:

# config/environments/application.rb
Demo::Application.configure do
  config.my_app = ActiveSupport::OrderedOptions.new
end
# config/environments/development.rb
Demo::Application.configure do
  [...]
  config.my_app.json_url = "http://www.domain.com"
  [...]
end
# config/environments/test.rb
Demo::Application.configure do
  [...]
  config.my_app.json_url = "http://test.domain.com"
  [...]
end

Now you can do the following in the Rails console:

$ rails console
>> Demo::Application.config.my_app.json_url
"http://www.domain.com"
>>
$ rails console test
>> Demo::Application.config.my_app.json_url
"http://test.domain.com"
>>

Now your Rails application will have access to all the configuration you need.

6

Visiting Pivotal Labs in Boulder, CO

Posted by Jason Noble on 02/17/2011 in Uncategorized |

I recently had the opportunity to visit Boulder, CO (Wiki, Map). The reason for my visit was to visit the Pivotal Labs office and spend a day pairing with them.

One of the cool/interesting features of the Pivotal office is that the company provides breakfast in the morning. I walked into the office to find two quiches, pears, strawberries, Orange Juice, Milk, Coffee and several choices of breakfast cereal.

The day started out with a standup. We then split off into pairs and worked on our respective projects. It was interesting to be thrown into the fire and within a half hour I was working on a large, well known website, implementing features that are now live on that site.

We split for lunch around 11:30. After lunch we switched pairs and I worked on a different portion of the code. Pivotal has a strong TDD/BDD approach to coding. We wrote tests that failed, then wrote the code to make them pass. After all the tests passed, we refactored. This is what Rails development is supposed to be.

The afternoon/evening was rounded out with a team retrospective meeting. After the meeting, the office all joined in for a game of StarCraft II.

I thought my interview/pairing day went really well, and I’m happy to say that I’m moving to Boulder to start working for Pivotal Labs March 14th.

Here’s some pics:

The Snow was falling as I arrived at Denver International Airport


The Pivotal office has a full size, full time ping-pong table


This is the view from the office.


The spotting scope is from a Russian Destroyer. One of the Pivots said during the summer you can see mountain climbers climbing the distant mountains. The large grill is for hosting summer parties.


This is from across the street. Pivotal Labs is on the second floor. Ted’s is an easy lunch.

3

Rails3 Custom Environment Variables

Posted by Jason Noble on 02/14/2011 in ruby, ruby on rails |

I recently saw a question on Stack Overflow regarding custom variables that have different values based on which environment you are running for Rails.

It turns out it’s very easy to accomplish.

# config/initializers/configuration.rb
class Configuration
  class << self
    attr_accessor :json_url
  end
end

This gives you access to a Rails variable called Configuration.json_url. Now we just need to initialize this value in our environment files.

# config/environments/development.rb
#   Put this inside the ______::Application.configure block
config.after_initialize do
  Configuration.json_url = "http://test.domain.com"
end
# config/environments/test.rb
config.after_initialize do
  Configuration.json_url = "http://www.domain.com"
end

Now you can test it in the Rails console.

$ rails console
>> Configuration.json_url
"http://test.domain.com"
>>
$ RAILS_ENV=production rails console
>> Configuration.json_url
"http://www.domain.com"
>>

Now your Rails app has access to these variables, so you can use them in your Controllers, Models or Views as needed.

Copyright © 2008-2014 Jason Noble's Technical Adventures All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.2.3, from BuyNowShop.com.