Friday, July 12, 2013

Getting my project to work with Capybara 2.0

My project was originally designed following Michael Hartl's excellent tutorial.  I was using a 1.x verison of Capybara and only had "cockpit error" types of problems - my own fault.

When I moved to Rails 4.0, my tests stopped working.  So I upgraded to Capybara 2.1 and dug in.  I must say I was surprised at the nature of the changes between 1.x and 2.1.

Some symptoms:

  • Accessors like "root_path" stopped working
  • visit stopped working
In general, you will need to modify your tests.
  • the requests directory should be named features
  • require 'spec_helper' needs to be require_relative '../spec_helper'
    • I haven't figured out how to adjust the search path to allow the original construct to work.
  • The outermost "describe" blocks should be "features"
I discovered that features don't nest, nor do scenarios.  See the references below.  In order to make 'visit' work, I turn the outermost 'describe' block into a feature block.

I tried using scenarios for inner blocks.  This was very painful and I went back to describe for inner blocks.

Reference links

An incomplete list of what I changed

Added to spec_helper:

require 'capybara/rspec' require 'capybara/rails' ... config.include Rails.application.routes.url_helpers


I had several checks for title that needed to be updated. They were
it { should have_selector('title', text: 'Sign up') }
Now:
it { should have_title('Sign up') }
An update to this post will follow as i organize my notes.

Wednesday, July 10, 2013

Note to Self: Rails Generate Scaffold

I learned about Rails by working through Michael Hartl's tutorial.  Generating scaffolding is not something that I do every day or even every week, so I rely on my notes.

I recently shifted to Rails 4.0 and constructed a scaffold per Hartl's tutorial.  I added the pages to my so I could use the generated MVC.  I discovered that while I could create objects, the contents/attributes were blank.

I traced it back and discovered that my attributes were not added to the model as "attr_accessible".  Added those and the MVC now works.

Monday, July 1, 2013

Rubygems, Heroku, Upgrading to Rails 4.0

School of hard knocks

In a previous post, I had mentioned that I hosting my gem(s) on RubyForge. Well, that didn't work out for me as couldn't get "git" to push to the repository. I've managed to get Heroku working, starting with a project on GitHub. But no luck with RubyForge. I had opened a support ticket some time ago & no response. So I will be moving my public projects to RubyGems.
Rubygems has some tooling and documentation for Gem building. I installed the tooling and my development environment broke. Yes, likely my fault as I was lazy and ignored all of the "wax-on/wax-off" about setting up a Ruby environment.
Lesson #1: Use rvm (Ruby Version Manager). This tool installs ruby to your own private area so you don't mess up your system installation. Particularly good advice if you are on a Macintosh. For me, I'm running Fedora and I had to do the standalone install of rvm. It worked, although I had to properly modify the search path. rvm is truly private to you.
Lesson #2: Install gems locally, not into your system. When things are looking very confused and your development environment is messed up, I find it useful to delete my '~/.gem' directory and use bundle install to rebuild it.
Lesson #3: Use gem version numbers in your Gemfile. Things aren't as stable as one would like, so pinning things down is helpful.

Upgrading to Rails 4.0

I had followed this post: http://blog.barbershoplabs.com/blog/2013/02/27/upgrading-from-rails-32-to-rails-40

The biggest culprit was in my config/application.rb file.   require "active_resource/railtie" doesn't work and I just commented it out. Rake now works and I'm keeping my fingers crossed.

I'm now just cleaning up after the new set of error messages.  Those seem obvious.

Update:
I generated a new Rails 4.0 project. I wrote a small ruby script to compare and modify my existing project based upon the new reference projects. It added missing directories and copied over any missing ruby files. For ruby files that existed in both and were different, I had the script invoke the merge tool "meld". The deltas weren't all that bad to merge.
#! /usr/bin/env ruby

require 'fileutils'

ref_dir = "reference/"
src_dir = "target_project/"

# okay, let's start with directories

l4 = Dir[ "#{ref_dir}**/**/**/**" ]

# puts l4

l4.each do |n|
  result =  File.stat( n)
  if File.stat(n).directory? then

    # Take off the front part
    p = n[(ref_dir.length..-1)]
    # puts "subpath #{p}"

    d = File.join( src_dir, p)
    if !File.directory?( d) then
      puts "Directory #{d} does not exist, creating it"
      `mkdir -p #{d}`
    end

    next
  end

end


l4.each do |n|
  result =  File.stat( n)
  if File.file?(n) then

    # Take off the front part
    p = n[(ref_dir.length..-1)]
    # puts "subpath #{p}"

    d = File.join( src_dir, p)
    if !File.file?( d) then
      puts "File #{d} does not exist, copying it over"
      `cp #{n} #{d}`
    end

    next
  end

end

l4.each do |n|
  result =  File.stat( n)
  if File.file?(n) then

    # Take off the front part
    p = n[(ref_dir.length..-1)]
    # puts "subpath #{p}"

    d = File.join( src_dir, p)
    if File.file?( d) then
      if d.match( /\.rb$/) then
        result = FileUtils.cmp( n, d)
        if( !result) then
          puts "Ruby file #{d} does not match"
          r = `diff #{n} #{d}`
          `meld #{n} #{d}`
        end
      end
    end

    next
  end

end

Saturday, June 22, 2013

Rakefiles and Generated Sources

A quick note on something that confused me.  I was working on a Rakefile for a project that has generated sources.  Generated sources aren't that unusual.  For example, people will often write a gemspec file from their Rakefile.

In my case, it started with a generated source file that needed to be added to my gemspec.  I had done the usual "file = Dir["**/*/*.rb"] and assumed that the file would be picked up when the :gem task was run. In other words, when the gem task is finally called it should figure out what should be included.  Yet it didn't do that.  Why?

My code to fill in the gemspec is an anonymous function (aka do-block) that is getting passed into the constructor for the gem task.  That function is being evaluated when the object is being constructed.  As a result, my generated sources didn't exist and thus weren't making it into the gem.

Rakefile Snippets

First, I explicitly constructed the list of generated sources:
GSRC_VERSION = File.join('lib', 'amatchpp', 'version.rb') GSRC_PROTODOC = "protodoc.rb" GSRCS = [GSRC_VERSION, GSRC_PROTODOC]

Then I construct a file-rule to construct a file
file GSRC_VERSION => "Rakefile" do desc m = "Writing version.rb information for #{PKG_VERSION}" puts m File.open(GSRC_VERSION, 'w') do |v| v.puts version_text end end
I add a task which depends on the file.
task :version => GSRC_VERSION

I have a :generated task that depends on all constructed sources.
task :generated => GSRCS

I add the :generated task as a dependency to other tasks as needed. For example, I add it to the :doc task as show below.
############################################################ # Documentation ############################################################ RDoc::Task.new(:doc) do |rd| rd.main = "README" rd.title = "#{PKG_NAME} - Enhanced Approximate Matching" rd.rdoc_files.include( PKG_DOC_FILES) rd.rdoc_dir = "doc" end task :doc => :generated

Wednesday, June 12, 2013

Ruby Project Directory Organization

NOTE: This post will be updated from time-to-time as I learn more.  Right now, it is collection of tidbits gathered together.  There is some conflicting information between a Rails project and a Ruby project.

TODO:  References

Ruby Project

A good Ruby project is organized into one or more modules.  A module's component files are placed into the project hierarchy by what they do.

project
  lib
    module1
      class_a.rb
      class_b.rb
      ...
    module1.rb

  test
    module1
      class_a_test.rb
      class_b_test.rb

We see that a module's files aren't kept together.  Copying a module from project-to-project will involve copy multiple directories and a file or two.

The motivation behind this organization appears to be search path and file name globbing.  One usually runs Ruby with the "-Ilib" command line option set.  A require statement for a module is:

    require 'module1'

Ruby will then locate and identify the code file as:

    lib/module1.rb

The file module1.rb will either be a complete implementation or simply a set of require statements:

    require 'module1/class_a'
    require 'module1/class_b'

Globbing is important as it allows other utilities to act on the appropriate subset of files.  For example, RDoc should be run on all Ruby source files under the lib/ directory but we don't want it to publicly document our test code.  Test runners are similar.

Files are organized into the directory structure for easy and consistent globbing by scripts and other utilities.

Sunday, June 9, 2013

Distributed your Git repository to RubyForge (Linux)


There are some notes on the RubyForge site but it wasn't obvious to someone has doesn't live and breath SCM.  I found the following useful:
NOTE:  If you are already using GitHub, you probably already have a ~/.ssh/id_rsa.pub key.  That didn't work.  My suspicion is that RubyForge wants your account name in the key, not your e-mail address.

Part 1: Make a RubyForge key
ssh-keygen -t rsa -C your-account-name -f id_rubyforge
Part 2: Add the key to your account
  • You add the key to your account using the high level "My Account"
    • edit keys is at the bottom of the screen
  • I use xclip to copy the contents of a file to the clipboard:
xclip ~/.ssh/id_rubyforge.pub
Part 3:  Add a rubyforge.org section to your ~/.ssh/config file

Host rubyforge.org
     user your-account-name
     IdentityFile ~/.ssh/id_rubyforge.pub
Part 4: Verify that sftp works
sftp -v rubyforge.org
The -v option produces useful trace information about the connection process.  An ssh guru/wizard may be able to help you if they have that output.

Once that is done, you can add RubyForge as a remote repository:

git remote add rubyforge gitosis@rubyforge.org:YOURPROJECT.git

and then:
git push rubyforge master

Gem Hosting

Gem Hosting is confusing.  There is a lot of conflicting information out there and it is very hard to tell what is the "right thing" to do.  I use GitHub by default for my projects as I can keep them private and GitHub focuses on version control.  When I'm working in Ruby, there are things I'd like to contribute to the community as a gem.  What is the "right way" to do this?

I looked at various sites as they had mentions when Google'd:
  • GitHub
  • RubyForge
  • RubyGems
GitHub is no longer building gems (whatever that means exactly) so they are out of the running.  There is no obvious way to mark your repository as being a Ruby Gem so other can find it.

RubyGems.org sounded like they had an easier to use interface.  However, parts of the site haven't been updated in a while and the single-signon with a RubyForge didn't work.

That left RubyForge which is the default hosting site for "gem install".  Account creation worked and the blogs/forums look current.  I do like having the projects moderated and approved.  RubyForge it is!

I filled a project request for my C++/Rice documentation tool.  That got approved on a Sunday morning in about 10 minutes.

There are a couple of next steps for me.  I will be keeping my project on GitHub and publishing to RubyForge.  This is akin to using Heroku for application hosting.

To do:
  • Making the gem/package structure standards-compliant
  • Figure out how to push to RubyForge
  • Public documentation