tag:blogger.com,1999:blog-11513971788648896642023-11-15T09:31:55.530-05:00Bryan AshBryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.comBlogger21125tag:blogger.com,1999:blog-1151397178864889664.post-28253346587914786032013-08-05T23:22:00.000-04:002013-08-06T00:24:52.511-04:00When RVM can't fetch from http://ftp.ruby-lang.org/I wanted to install Ruby 1.9.2. Easy, I thought, that's as simple as <b>rvm install 1.9.2</b>. Unfortunately, it looks like http://ftp.ruby-lang.org/ is having a bad day.<br />
<br />
<a href="http://rvm.io/rvm/offline" target="_blank">http://rvm.io/rvm/offline</a> provides helpful instructions assuming you are starting with nothing. I already have several Ruby versions installed, so my process was reduced to:<br />
<br />
<pre class="shell" courier="" monospace="" name="code" new="" ourier="" style="background-color: black; color: white;">$ curl -L http://www.mirrorservice.org/sites/ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-p320.tar.bz2 -o ruby-1.9.2-p320.tar.bz2
$ mv ruby-1.9.2-p320.tar.bz2 ~/.rvm/archives/
$ rvm install 1.9.2-p320 --disable-binary
$ rvm use ruby-1.9.2 --default
</pre>
<br />
Not too shabby! Many thanks to the RVM team.Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com2tag:blogger.com,1999:blog-1151397178864889664.post-5552360527495712402010-12-05T22:45:00.010-05:002010-12-07T21:16:52.862-05:00Providing markdown engine options in HAMLI've started looking into using <a href="http://daringfireball.net/projects/markdown/">Markdown</a> in <a href="http://www.rubyonrails.org/">Rails</a>. I use <a href="http://haml-lang.com">HAML</a> for rendering views, so markdown is handled by a HAML filter:<br />
<br />
<pre class="xml" name="code">%h1 Here's some rendered Markdown
:markdown
= @model.markdown
</pre><br />
This worked great, but I was concerned about javascript injection since the markdown would be provided by a user using a <a href="https://github.com/derobins/wmd">WMD editor</a>. I found that I could explicitly call RDiscount, with the :filter_html option, to render the markdown myself:<br />
<br />
<pre class="xml" name="code">%h1 Here's some rendered Markdown
!= RDiscount.new(@model.markdown, :filter_html).to_html
</pre><br />
This worked great, but I didn't want to have to remember this incantation every time I want to render some Markdown. After some discussion with <a href="https://github.com/nex3">Nathan Weizenbaum</a> the current maintainer of Haml, I realized that the answer is actually presented (somewhat indirectly) through the documentation. The section on <a href="http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#filters">Custom Filters</a> says "You can also define your own filters. See <a href="http://haml-lang.com/docs/yardoc/Haml/Filters.html">Haml::Filters</a> for details.". You have to then follow on to <a href="http://haml-lang.com/docs/yardoc/Haml/Filters/Base.html">Haml::Filters::Base</a> for the full story.<br />
<br />
In my Rails app, I created a custom Haml filter that overrides the original :markdown (so I don't accidentally forget and use an unsafe :markdown) config/initializers/haml.rb:<br />
<br />
<pre class="ruby" name="code">module MyApp
module Filters
module Markdown
include Haml::Filters::Base
lazy_require 'rdiscount'
def render(text)
::RDiscount.new(text, :filter_html).to_html
end
end
end
end
</pre><br />
Now, whenever HAML renders a :markdown filter, it will filter the HTML and protect me against javascript injection attacks.Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com1tag:blogger.com,1999:blog-1151397178864889664.post-8855931720000813712010-12-02T20:57:00.000-05:002010-12-02T20:57:49.541-05:00Stop deploying unneccessary Gems in your Heroku slug<a href="http://heroku.com/">Heroku</a> published a handy tip in their newsletter today:<br />
<pre class="shell" name="code" style="background-color: black; color: white; font-family: "Courier New,Courier,monospace;">$ heroku config:add BUNDLE_WITHOUT=development:test
</pre>Having set this and pushed a change to my Gemfile, my slug size went from 39.4MB down to 10.9MB.<br />
<br />
Smaller slugs compile and load faster.<br />
<br />
Thank you Heroku!Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-86721898566382032322010-11-22T16:44:00.001-05:002010-12-02T20:22:44.235-05:00Rake duplicate task descriptionsUsing Rake I can define tasks that get added to a collection of tasks for execution. For example:<br />
<pre class="ruby" name="code">desc "one"
task :one do
puts "one"
end
desc "all"
task :all => [:one]
desc "two"
task :two do
puts "two"
end
desc "all"
task :all => [:two]
</pre><br />
Now, if I look at <b>rake --tasks</b> I will see:<br />
<pre class="shell" name="code" style="background-color: black; color: white; font-family: "Courier New,Courier,monospace;">> rake --tasks
rake one # one
rake two # two
rake all # all / all</pre><br />
Rake has duplicated the description for the :all task. Looking at the code for Rake I discovered that this can be avoided by terminating the :all task description with a period:<br />
<pre class="ruby" name="code">desc "one"
desc "all."
task :all => [:one]
desc "one"
desc "all."
task :all => [:one]
</pre><br />
<pre class="shell" name="code" style="background-color: black; color: white; font-family: "Courier New,Courier,monospace;">> rake --tasks
rake one # one
rake two # two
rake all # all.</pre>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-53883377644059854622009-11-07T14:25:00.002-05:002009-11-07T14:59:25.698-05:00Blogging software for Mac<p>I used to use Windows Live Writer in the old days but now I'm a trendy Mac user I need something suitable. Seems like there are a number of paid options that people rate quite highly but I'm too cheap to pay for software.
</p>
<p>
I just tried Quamana and it looked promising. Unfortunately it kept throwing errors, didn't download tags from Blogger and then failed to actually publish the post!
</p>
<p>
I'm now trying Flock which is a full featured browser built on the same stuff as Firefox, that happens to have a blog editor built in. I don't think it's as clean as Live Writer, but appears to have enough functionality for my basic use.
</p>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-81872785697777461312009-11-07T10:13:00.001-05:002010-03-19T14:35:46.860-04:00MacBook Pro AirPort autoconnectWe just installed a new wireless router with WPA security (with the last one we were using WEP). My MacBook Pro connected just fine first time. The problem was that whenever I close the lid and reopen, the Airport would not automatically connect, it would ask me which network to connect to.<br />
<br />
The surprising solution was to move <b>/Applications/Utilities/</b><b>System Preferences.app</b> into <b>/Applications/System Preferences.app</b><br />
<br />
Now AirPort reconnects all by itself every time.Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com2tag:blogger.com,1999:blog-1151397178864889664.post-23187140718413952372009-06-27T00:23:00.001-04:002009-06-27T00:23:15.809-04:00Using simple_auto_complete<p>I'm working on my first public Rails application for managing small skydiving businesses.  The primary feature is to track when a person gets in an aircraft to make a skydive.  In skydiver speak, that's: "Jumpers are manifested in a Slot on a Load". I'm using a model named Account to hold Jumpers, Pilots and in fact anyone who does business with the dropzone.</p> So, My models look like: <pre class="ruby" name="code">class Load < ActiveRecord::Base
has_many :slots, :dependent => :destroy
end</pre>
and:
<pre class="ruby" name="code">class Slot < ActiveRecord::Base
belongs_to :account
end</pre>
The form for editing a load contains:
<pre class="html" name="code"><%= render :partial => 'slot', :collection => @load.slots %></pre>
and the _slots.html.erb partial contains something like:
<pre class="html" name="code"><% fields_for "load[slot_attributes][]", slot do |slot_form| -%>
<%= slot_form.label :account_name, 'Jumper:' %>
<%= slot_form.text_field :account_name %>
<% end -%></pre>
<p>This all works great, so it's time for a little flair ... what I'd like is for the Account.name field to present a list of accounts that match the text that I've typed so far and allow me to pick one (like the Google search field does these days).</p>
<h3>Enter <a href="http://github.com/grosser/simple_auto_complete/tree">simple_auto_complete</a>.</h3>
<p>The instructions in the README describe the steps to get the simplest example working but left me scratching my head.  What I needed was the following...</p>
<p>In SlotsController (something I didn't even need before):</p>
<pre class="ruby" name="code">class SlotsController < ApplicationController
autocomplete_for :account, :name, :order => 'name ASC'
end</pre>
<p>in _slot.html.erb, I changed to: </p>
<pre class="html" name="code"><% fields_for "load[slot_attributes][]", slot do |slot_form| -%>
<%= slot_form.label :account_name, 'Jumper:' %>
<%= slot_form.text_field :account_name, :class => 'autocomplete',
:autocomplete_url => autocomplete_for_account_name_slots_path %>
<% end -%></pre>
I updated my routes.rb to include:
<pre class="ruby" name="code">map.resources :slots, :collection => { :autocomplete_for_account_name => :get}</pre>
Finally, I added to my application.html.erb layout:
<pre class="ruby" name="code"><%= stylesheet_link_tag 'site', 'jquery.autocomplete' %>
<%= javascript_include_tag 'jquery', 'jquery.autocomplete', 'application', 'prototype' %></pre>
And it works like a champ!
Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com3tag:blogger.com,1999:blog-1151397178864889664.post-62962800931497672392009-02-28T03:07:00.001-05:002009-02-28T03:07:15.320-05:00Permission denied - db/test.sqlite3I'm investigating creating a plugin for <a href="http://www.redmine.org/">Redmine</a> and having a good old time when I decide to clear out the test database: <p>C:\dev\redmine>rake db:test:purge <br />(in C:/dev/redmine) <br />rake aborted! <br /><font color="#ff0000">Permission denied - db/test.sqlite3</font></p> <p>Boo!  </p> <p>I don't know what made me think of it but I tried unsetting my RAILS_ENV environment variable (I had it set to "test"):</p> <p>C:\dev\redmine>set rails_env= </p> <p>C:\dev\redmine>rake db:test:purge <br />(in C:/dev/redmine) </p> <p>C:\dev\redmine></p> <p>Hurrah!</p> Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com5tag:blogger.com,1999:blog-1151397178864889664.post-8525254747205280662009-02-22T11:06:00.001-05:002009-02-22T11:06:51.747-05:00Formatting README.rdoc on githubGithub has a neat feature that displays a README file, that it finds in the root directory of a project, on the project page. The <a href="http://github.com/guides/readme-formatting">README formatting</a> page describes a number of different formats that it will render based on the file extension. I tried renaming to README.rdoc but it still rendered as plain text. The trouble was that the file was intended to be used with a full RDOC site for the source. When I removed the "link" tags from the file and pushed ... Github renders it correctly ... Hurrah! Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com6tag:blogger.com,1999:blog-1151397178864889664.post-70137013488522493762009-02-14T22:57:00.002-05:002009-02-14T23:00:09.975-05:00BDD WxRuby applications with Cucumber and Nobbie<p>For a long time now, I've been wanting to write some <a href="http://github.com/aslakhellesoy/cucumber/tree">Cucumber</a> features to describe a <a href="http://wxruby.rubyforge.org/wiki/wiki.pl">WxRuby</a> application I have. A couple of weeks ago, I found a library called wx-nobbie on Rubyforge that seems to be almost exactly what I was looking for.  It provides a simple, high level interface for driving WxRuby applications through commands like "click", "choose" and "type".  I had a little trouble getting the tests to pass to start with, but with a few minor tweaks I got there.  I contacted the original author and got his permission to take it forward.</p> <p>I created a GitHub repository for <a href="http://github.com/bryan-ash/wx-nobbie/tree">Wx-Nobbie</a> and have made a few updates over the last week.  Currently, I've got 4 feature scenarios with 10 steps:</p> <pre>C:\dev\wx-nobbie>rake features
(in C:/dev/wx-nobbie)
In order to test drive a WxRuby application # features/acceptance_test.feature
As a developer
I want Nobbie to provide acceptance test access to the application
<font color="green">Scenario: Choosing a radio button</font> # features/acceptance_test.feature:5
<font color="green">Then "radio_button" is not chosen</font># features/step_definitions/acceptance_test_steps.rb:21
<font color="green">When I choose "radio_button"</font> # features/step_definitions/acceptance_test_steps.rb:1
<font color="green">Then "radio_button" is chosen</font> # features/step_definitions/acceptance_test_steps.rb:17
<font color="green">Scenario: Choosing a check box</font> # features/acceptance_test.feature:10
<font color="green">Then "check_box" is not chosen</font># features/step_definitions/acceptance_test_steps.rb:21
<font color="green">When I choose "check_box"</font> # features/step_definitions/acceptance_test_steps.rb:1
<font color="green">Then "check_box" is chosen</font> # features/step_definitions/acceptance_test_steps.rb:17
<font color="green">Scenario: Type into a text control</font> # features/acceptance_test.feature:15
<font color="green">When I type "123" into "text_ctrl"</font> # features/step_definitions/acceptance_test_steps.rb:5
<font color="green">Then I should see "123" in "text_ctrl"</font> # features/step_definitions/acceptance_test_steps.rb:9
<font color="green">Scenario: Type into a combo box</font> # features/acceptance_test.feature:19
<font color="green">When I type "456" into "combo_box"</font> # features/step_definitions/acceptance_test_steps.rb:5
<font color="green">Then I should see "456" in "combo_box"</font> # features/step_definitions/acceptance_test_steps.rb:9
4 scenarios
<font color="green">10 steps passed</font></pre>
<p>With just these 4 scenarios, RCov tells me I have 62.9% coverage. </p>
<p>Hurrah! </p>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-45882631507852485452009-01-25T10:29:00.002-05:002009-03-28T20:07:57.012-04:00Building a Ruby Extension With Visual C++ Express 2008<span style="font-style:italic;">Edit March 29, 2009: While the instructions below describe how to build the Win32::GuiTest extension, I didn't get very far with using it before I found <a href="http://github.com/bryan-ash/wx-nobbie/tree">Wx::Nobbie</a> which I much prefer and have taken up the maintenance of at Github.</span>
<p>I'd like to be able to use Cucumber to drive the development of Windows applications so I went looking for something like Webrat for desktop GUI's. I found <a href="http://raa.ruby-lang.org/project/win32-guitest/">Win32::GuiTest</a>.  There is a project of the same name on RubyForge that contains the same source but that's as far as that went.  </p> <p>Win32::GuiTest is a C extension that was compiled with Cygwin, so I wanted to rebuild a native version.  I found some instructions on <a href="http://www.kleinfelter.com/build-ruby-guitest-win32">Kevin Kleinfelter's blog</a> that look much like below, but using VC++ 2008 I got to skip a couple of steps:</p> <ol> <li>Download and install <a href="http://www.microsoft.com/express/vc/">Visual C++ Express Edition</a> (this is free, I got the 2008 version) </li> <li>Edit $RUBY_HOME/lib/ruby/1.8/i386-mswin32/config.h and delete the “#error MSC version unmatch” line </li> <li>Open a Visual Studio 2008 Command Prompt <ul> <li>Start </li> <li>All Programs </li> <li>Microsoft Visual C++ 2008 Express Edition </li> <li>Visual Studio Tools </li> </ul> </li> <li>cd C:\temp\guitest020218\ext\cguitest <ul> <li>I'm building the guitest extension </li> </ul> </li> <li>ruby extconf.rb </li> <li>nmake </li> <li>mt.exe -manifest cguitest.so.manifest -outputresource:cguitest.so;2 <ul> <li>Copy and paste the command above. You need it all from the 'mt' to the ';2' </li> </ul> </li> <li>nmake install </li> <li>cd \temp\guitest020218 </li> <li>ruby install.rb config </li> <li>ruby install.rb install </li> <li>To test: <ul> <li>irb </li> </ul> <ul> <li>require 'win32/guitest' </li> <li>check response is '=> true' </li> </ul> </li> </ol>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-80459401599351741062009-01-14T23:01:00.002-05:002009-01-15T08:19:27.116-05:00My first Rails bug – accepted!<p>At the time that I posted about <a href="http://bryan-ash.blogspot.com/2008/09/my-first-rails-bug.html">My first Rails bug</a> the tests that existed for Action Pack contained failures and the only test in integration_upload_test.rb was:</p> <pre class="ruby" name="code">assert_equal(:multipart_form, SessionUploadTest.last_request_type)</pre>
<p>I felt that my change could be submitted with no tests because:</p>
<ul>
<li>The change was trivial (6 characters) </li>
<li>The change was for a well known Windows file issue </li>
<li>The tests that existed didn’t pass </li>
<li>There were no example tests for me to build on </li>
</ul>
<p>Three months after I submitted the <a href="http://rails.lighthouseapp.com/projects/8994/tickets/1065-multipart_body-truncates-file-in-windows">ticket</a>, <a href="http://rails.lighthouseapp.com/users/1366">Pratik</a> dismissed it by changing its state to <strong>incomplete</strong> with the comment “Missing a test case.”  Are these guys CRAZY?  No, no they are not.  Just because there were <a href="http://www.pragprog.com/the-pragmatic-programmer/extracts/software-entropy">broken windows</a> in the neighborhood did not give me the right to break any more.</p>
<p>So, I sucked it up, cloned the latest rails repository again and to my surprise … all the Action Pack tests passed! … and there was a nice simple example in multipart_params_parsing_test.rb:</p>
<pre class="ruby" name="code"> test "uploads and reads file" do
with_test_routing do
post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain")
assert_equal "File: Hello", response.body
end
end</pre>
<p>This is too easy!  I added a test for my issue:</p>
<pre class="ruby" name="code"> test "uploads and reads a binary file in windows" do
with_test_routing do
fixture_file = FIXTURE_PATH + "/mona_lisa.jpg"
post '/read', :uploaded_data => fixture_file_upload(fixture_file, "image/jpg")
assert_equal 'File: '.length + File.size(fixture_file), response.content_length
end
end</pre>
<p>Watched it fail.  Added the magic 6 characters (“, ‘rb’”).  Watched it pass.  Wrapped it up and shipped it off.  The following morning I pick up an email letting me know that Pratik has reopened the ticket and assigned it to <a href="http://rails.lighthouseapp.com/users/424">Joshua Peek</a> for review.  The change was committed before the day was out!</p>
<p>Many thanks to Pratik and Josh for attending to this so promptly.</p>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com1tag:blogger.com,1999:blog-1151397178864889664.post-67102610440718473902009-01-12T22:13:00.002-05:002009-01-13T07:19:57.886-05:00Restful-Authentication with Cucumber features (Step 2)<p>Knowing that all the stories that ship with restful-authentication pass, I’m ready to convert to Cucumber features.  The process is actually fairly simple and is described quite clearly on this Cucumber <a href="http://wiki.github.com/aslakhellesoy/cucumber/migration-from-rspec-stories">wiki page</a>.  I did have to tweak a couple of the step matchers and create ra_env.rb:</p> <pre class="ruby" name="code">Before do
Fixtures.reset_cache
fixtures_folder = File.join(RAILS_ROOT, 'spec', 'fixtures')
Fixtures.create_fixtures(fixtures_folder, "users")
end
# Make visible for testing
ApplicationController.send(:public, :logged_in?, :current_user, :authorized?)</pre>
<p>I’d have liked to put this in the support directory but Cucumber loaded it before env.rb, so I put in with the step_definitions.</p>
<p>Once that was done, all that was left was to update authenticated_generator.rb to copy the template features instead of stories.</p>
<p>The result is available from <a href="http://github.com/bryan-ash/restful-authentication/tree">my fork</a> on github.</p>
<p>Enjoy!</p>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-34957492427502194342009-01-10T12:18:00.004-05:002009-01-10T13:10:23.423-05:00Restful-Authentication with Cucumber features (Step 1)My latest exercise is to update the <a href="http://github.com/technoweenie/restful-authentication/tree">restful-authentication</a> Rails plugin to generate <a href="http://github.com/aslakhellesoy/cucumber/tree">Cucumber</a> features instead of RSpec stories.
There is already a <a href="http://rails_security.lighthouseapp.com/projects/15332/tickets/23-use-cucumber-features-instead-of-rspec-stories">Lighthouse ticket</a> open for this, so I guess I'm not the first one to think of it.
The first steps make sure that restful-authentication is set up correctly:<ol>
<li>Create an empty Rails application</li><li>Put in version control (git init, git add ., git commit -a -m "init")</li><li>Add plugins as shown on the Cucumber <a href="http://wiki.github.com/aslakhellesoy/cucumber/ruby-on-rails">Rails wiki page</a></li><li>Add <a href="http://github.com/bryan-ash/restful-authentication/tree">my fork</a> of restful-authentication in the same manner</li><li>Complete the restful-authentication installation per the directions</li><li>Create the database tables (rake db:migrate)</li><li>Run the RSpec stories (stories\rest_auth_stories.rb)</li></ol>
Stories won't even run, let alone pass! I'm not going to show all the errors along the way, but the steps to get to passing stories were:<ol><li>Added AthenticatedSystem into ApplicationController with the following 2 lines:<pre name="code" class="ruby"> include AuthenticatedSystem
helper :all # include all helpers, all the time</pre></li><li>Uncommented the routes.rb map.root line(map.root :controller => "welcome")</li><li>Removed line 1 of user_steps.rb (require File.dirname(__FILE__) + '/../helper')</li><li>Created a minimal WelcomeController with an index method</li><li>Created a minimal layout</li></ol>
<pre name="code" class="ruby">16 scenarios: 16 succeeded, 0 failed, 0 pending</pre>
Hurrah!Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-32079137812989107412008-10-19T21:18:00.007-04:002008-10-19T22:35:56.099-04:00Cucumber scenarios need a titleSo I'm playing with <a href="http://dannorth.net/introducing-bdd">Behavior Driven Development</a> (BDD) and Ruby on Rails using <a href="http://github.com/aslakhellesoy/cucumber/wikis/home">Cucumber</a> and <a href="http://github.com/brynary/webrat">Webrat</a>. I thought I'd start with the simplest feature I could think of, "I want to see the application name in the title". So, off we go with features/site_layout.feature:
<pre name="code" class="feature">
Feature: Site Layout
In order to build familiarity
As a user
I want to see the application name in the title
Scenario:
Given I am on the home page
Then the title tag should be "WOW App"
</pre>
I create features/steps/site_steps.rb:
<pre name="code" class="ruby">
Given /^I am on (.*)$/ do |page|
visits case page
when "the home page"
"/"
else
raise "Can't find mapping from \"#{page}\" to a path"
end
end
Then /^the (.*) tag should be "(.*)"$/ do |tag, content|
response.should have_tag(tag, content)
end
</pre>
And run it with "rake features". What I get is:
<pre>
C:\dev\temp>rake features
(in C:/dev/temp)
Feature: Site Layout # features/site_layout.feature
In order to build familiarity
As a user
I want to see the application name in the title
<font color="green">Scenario: Given I am on the home page</font> # features/site_layout.feature:6
<font color="red">Then the title tag should be "WOW App"</font> # features/steps/site_steps.rb:10
<font color="red">You have a nil object when you didn't expect it!
The error occurred while evaluating nil.content_type (NoMethodError)
...
./features/steps/site_steps.rb:11:in `Then /^the (.*) tag should be "(.*)"$/'
features/site_layout.feature:8:in `Then the title tag should be "WOW App"'
1 steps failed</font>
rake aborted!
</pre>
Not quite what I had in mind, I was expecting a failure, but got an error. Nevermind, this is all very new to me, so I'll push on and it'll all become clear right?
I create an app/views/layouts/application.html.erb:
<pre name="code" class="html">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>WOW App</title>
</head>
<body>
</body>
</html>
</pre>
Run the feature again ... no joy, same error. Hours of web searching leads me nowhere. Finally while looking at some examples I noticed that my Scenario doesn't have a description, so I add one on line 6:
<pre name="code" class="feature">
Feature: Site Layout
In order to build familiarity
As a user
I want to see the application name in the title
Scenario: On the home page
Given I am on the home page
Then the title tag should be "WOW App"
</pre>
Now I get:
<pre>
C:\dev\temp>rake features
(in C:/dev/temp)
Feature: Site Layout # features/site_layout.feature
In order to build familiarity
As a user
I want to see the application name in the title
<font color="green">Scenario: On the home page</font> # features/site_layout.feature:6
<font color="red">Given I am on the home page # features/steps/site_steps.rb:1
No route matches "/" with {:method=>:get} (ActionController::RoutingError)
...
features/site_layout.feature:7:in `Given I am on the home page'</font>
<font color="blue">Then the title tag should be "WOW App"</font> # features/steps/site_steps.rb:10
<font color="red">1 steps failed</font>
<font color="blue">1 steps skipped</font>
rake aborted!
</pre>
A quick addition to config/routes.rb:
<pre name="code" class="ruby">
map.root :controller => 'example'
</pre>
And a stub controller from "ruby script\generate controller Example index", I get:
<pre>
C:\dev\temp>rake features
(in C:/dev/temp)
Feature: Site Layout # features/site_layout.feature
In order to build familiarity
As a user
I want to see the application name in the title
<font color="green">Scenario: On the home page</font> # features/site_layout.feature:6
<font color="green">Given I am on the home page</font> # features/steps/site_steps.rb:1
<font color="green">Then the title tag should be "WOW App"</font> # features/steps/site_steps.rb:10
<font color="green">2 steps passed</font>
</pre>
Hurrah!Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-22668818328859916862008-10-05T21:21:00.003-04:002008-10-05T23:01:20.515-04:00Where to put sqlite3.dll on WindowsWhen I first started using SQLite3 on Windows, most people suggested downloading sqlite3.dll from <a href="http://www.sqlite.org/download.html">www.sqlite.org</a> and storing it in ruby/bin so Windows could find it. For a while, this strategy worked fine but I really didn't like the application requiring users to put that DLL into an acceptable place.
I don't know why it didn't occur to me earlier, but, given that this is only intended to be a Windows application, why not just modify the PATH environment variable within the application. So that's what I've done. In config/boot.rb:
<pre name="code" class="ruby">
# Make sqlite3.dll available
ENV['PATH'] += ";#{RAILS_ROOT}/vendor/bin/sqlite3"
</pre>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com1tag:blogger.com,1999:blog-1151397178864889664.post-54479891346448274922008-10-02T13:08:00.008-04:002009-01-12T07:43:40.382-05:00class << selfI've come across this construct many times while browsing a number of open source projects:
<pre name="code" class="ruby">
class MyClass
class << self
def a_class_method
...
end
end
def an_instance_method
...
end
end
</pre>
I finally got it today ... there's no black magic ... all "class << self" means is:
<blockquote>add the following class method definitions to the class MyClass.</blockquote>
Or,
<blockquote>open the class MyClass, and add the following class methods.</blockquote>
Now that I'm in the gang and understand this incantation, I'm not sure I like it. It seems like a nice way to group all your class methods together, but if you define several class methods then it becomes harder to recognize whether a method belongs to the class or an instance. I think it's cleaner to explicitly state the ownership of each method:
<pre name="code" class="ruby">
class MyClass
def self.a_class_method
...
end
def an_instance_method
...
end
end
</pre>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-49889915601152958322008-09-20T22:46:00.012-04:002009-01-19T20:27:27.938-05:00My first Rails bug<p><em>UPDATE January 14, 2009 -> <a href="http://bryan-ash.blogspot.com/2009/01/my-first-rails-bug-accepted.html">The patch for this bug was accepted!</a></em></p> <p>I'm pretty new to <a href="http://rubyonrails.org/">Rails</a> and my first project included uploading videos. Shouldn't be too difficult I thought, after a little Google searching, I came up with the perfect example by Jim Neath: <a title="Permanent Link: Converting Videos with Rails: Converting the Video" href="http://jimneath.org/2008/06/03/converting-videos-with-rails-converting-the-video/" rel="bookmark">Converting Videos with Rails: Converting the Video</a> Wanting to practice new skills with <a href="http://rspec.info/">RSpec</a> and <a href="https://github.com/aslakhellesoy/cucumber/tree">Cucumber</a> I wrote my first feature spec: </p> <pre class="cucumber" name="code">Feature: Upload videos
In order to provide videos to users after hours
As a videographer
I want to upload videos
Scenario: A valid filename is provided
Given I go to the new video page
And I browse to the file "Movie_0001.avi"
When I submit the upload
Then I should see "success"
And the file should be uploaded</pre>
and the supporting steps file, upload_steps.rb:
<pre class="ruby" name="code">require 'ftools'
require 'mime/types'
When /I browse to the file \"(.+)\"/ do |path|
@original_filepath = File.join('features/fixtures/', path)
mime_types = MIME::Types.of(@original_filepath)
attach_file 'video[source]', @original_filepath, mime_types[0].content_type
end
When 'I submit the upload' do
click_button 'Create'
end
def uploaded_filepath
uploaded_basename = File.basename(@original_filepath)
File.join(RAILS_ROOT, "public/videos/1", uploaded_basename)
end
Then /the file should be uploaded/ do
assert File.compare(@original_filepath, uploaded_filepath)
end</pre>
what I got was:
<pre>...
<font color="red">And the file should be uploaded
<false> is not true. (Test::Unit::AssertionFailedError)
c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:48:in `assert_block'
c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:500:in `_wrap_assertion'
c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:46:in `assert_block'
c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:63:in `assert'
c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:495:in `_wrap_assertion'
c:/ruby/lib/ruby/1.8/test/unit/assertions.rb:61:in `assert'
./features/upload/steps/upload_steps.rb:22:in `And /the file should be uploaded/'
features/upload/upload.feature:14:in `And the file should be uploaded'</font>
...</pre>
On closer inspection, the test was failing because the uploaded file was truncated in some bizarre way. However, if I ran the application and manually upload a file from the browser, everything worked fine. After much hunting I ended up in rails/actionpack/lib/action_controller/integration.rb where in multipart_body, the mode is not specified in the call to File.open, so it defaults to "r". This is all well and good on anything but Windows, which I happen to be using! Windows requires that the mode be specified as "rb" to ensure the file is read as binary. I submitted a <a href="http://rails.lighthouseapp.com/projects/8994/tickets/1065-multipart_body-truncates-file-in-windows">patch</a> but I'm not holding my breath for it to be pulled in anytime soon.
Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-49515812636017312692008-09-20T20:34:00.001-04:002008-10-05T21:13:23.137-04:00Ruby PCAN DLL WrapperInspired by <a href="http://www.atomicobject.com/pages/System+Testing+in+Ruby">System Testing In Ruby (Systir)</a>, I have published a small Ruby <a href="http://rubyforge.org/projects/pcanusb/">PCAN USB DLL Wrapper.</a>
Now with a small <a href="http://www.peak-system.com/db/gb/pcanusb_gb.html">device</a> available for less than $300 and some free software, I can write automated system test scripts of the form:
<blockquote>send tftp_rrq("autoexec.bat").with(blksize(2036))
verify_target_sends oack(blksize(2036)
send ack(0)
verify_target_sends autoexec_bat
send ack(1)
</blockquote>If you have <a href="http://rubygems.org/">rubygems</a> installed, PCAN DLL Wrapper is easily obtained:
<blockquote>set http_proxy=http://my_proxy_host.com:80
gem install pcanusb
</blockquote>and then use:
<blockquote>require 'pcan_usb'
PCAN_USB.init(PCAN_USB::BAUD_1M)
PCAN_USB.write(0x0E100501, "Hello World!")
PCAN_USB.close
</blockquote>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-42270489639610340662008-09-20T20:29:00.000-04:002008-09-20T20:30:04.621-04:00Collection o quotesRicky Hustler, a friend of mine, used to use this one on a regular basis:<b>
</b><span style="font-style: italic;"><blockquote>"You never see a luggage rack on a hearse"
</blockquote></span><b><span style="font-style: italic;">
</span></b>I came across this on <a href="http://butunclebob.com/ArticleS.JamesGrenning">James Grenning's</a> blog:<b>
</b><blockquote><span style="font-style: italic;">"It's easier to act your way into thinking differently than to think your way into acting differently"</span>
</blockquote><span>
From a friend's coffee cup:
<blockquote><span style="font-style: italic;">"Lead, follow, or get out of my way"</span>
</blockquote>
<a href="http://www.susanjeffers.com/">Susan Jeffers</a>:
<blockquote><span style="font-style: italic;">"Feel the fear and do it anyway"
</span></blockquote>
</span>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0tag:blogger.com,1999:blog-1151397178864889664.post-64088742484214430082008-06-10T20:40:00.004-04:002008-10-05T21:13:46.423-04:00ActiveRecord requires RAILS_ROOT for relative Sqlite pathI'm writing a little time tracking tool for Windows in Ruby. The data is stored in a database so I figured I'd use ActiveRecord and maybe learn something about Rails along the way. Everything I found about ActiveRecord tells me that it "can be used independently outside of <a href="http://api.rubyonrails.org/classes/Rails.html">Rails</a>". One minor detail that I just figured out:
<blockquote>If you want to use a relative path for a sqlite3 database in your database.yml, you have to define RAILS_ROOT.
</blockquote>For example, if config/database.yml =>
<pre name="code" class="yaml">
production:
adapter: sqlite3
database: db/production.sqlite
</pre>
ActiveRecord initialization (mine's in config/boot.rb) looks like:
<pre name="code" class="ruby">
RAILS_ROOT = "#{File.dirname(File.expand_path(__FILE__))}/.."
RAILS_ENV = ENV['RAILS_ENV'] || 'production'
$LOAD_PATH.unshift "#{RAILS_ROOT}/vendor/sqlite3"
config = YAML::load(IO.read("#{RAILS_ROOT}/config/database.yml"))
ActiveRecord::Base.configurations = config
ActiveRecord::Base.establish_connection(config[RAILS_ENV])
</pre>Bryan Ashhttp://www.blogger.com/profile/16035145599734362152noreply@blogger.com0