SpicyCode

The ramblings of Chad Humphries

Posts Tagged ‘ruby

Installing Merb 1.0

with 4 comments

I saw that merb 1.0 came out and decided to install it. I jumped, nay leaped to the terminal and ran:

sudo gem install merb

I saw the following:


Successfully installed extlib-0.9.8
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.2
Successfully installed json_pure-1.1.3
Successfully installed mime-types-1.15
Successfully installed thor-0.9.8
Successfully installed merb-core-1.0
Successfully installed merb-action-args-1.0
Successfully installed merb-assets-1.0
Successfully installed merb-slices-1.0
Successfully installed merb-auth-core-1.0
Successfully installed merb-auth-more-1.0
Successfully installed merb-auth-slice-password-1.0
Successfully installed merb-auth-1.0
Successfully installed merb-cache-1.0
Successfully installed merb-exceptions-1.0
Successfully installed diff-lcs-1.1.2
Successfully installed templater-0.3.5
Successfully installed merb-gen-1.0
Successfully installed haml-2.0.4
Successfully installed merb-haml-1.0
Successfully installed merb-helpers-1.0
Successfully installed mailfactory-1.4.0
Successfully installed merb-mailer-1.0
Successfully installed merb-param-protection-1.0
Successfully installed addressable-1.0.4
Successfully installed data_objects-0.9.6
Successfully installed dm-core-0.9.6
Successfully installed dm-migrations-0.9.6
Successfully installed merb_datamapper-1.0
Successfully installed merb-more-1.0
Successfully installed do_sqlite3-0.9.6
Successfully installed dm-timestamps-0.9.6
Successfully installed uuidtools-1.0.4
Successfully installed dm-types-0.9.6
Successfully installed dm-aggregates-0.9.6
Successfully installed dm-validations-0.9.6
Successfully installed randexp-0.1.4
Successfully installed dm-sweatshop-0.9.6
Successfully installed merb-1.0

I’m all for modular, but ouch. 40 gems is quite a lot.

Written by spicycode

November 10, 2008 at 5:57 pm

Posted in programming

Tagged with ,

A serialized mystery

Ehren and I recently had to spec out a discussion filter to return only items selected by the user. While testing all things were golden. However, when we tried to step through the implementation in the browser we would have issues. After some investigation in script/console we finally found the culprit.

The breakdown of our model

The model we are filtering contains a serialized column of integers to aid with the filtering. This is a low-tech version of a full-blown association. The purpose was to insulate us from yet more expensive sub queries as the full page load can be quite intensive. (I jest not when I say we have a model named “Item” in this project.)

class Item < ActiveRecord::Base
  serialize :used_by_filter_ids
end

Calling Scotland Yard

Our investigation had us discover that the serialized column was returning an array of integers as Strings, versus actual Numerics which our callers expected. A simple tweak to the implementation to call to_i prior to handing the results back and all is good.

class Item < ActiveRecord::Base
  serialize :used_by_filter_ids

  def used_by_filter_ids
    read_attribute(:used_by_filter_ids).each(&:to_i)
  end
end

This discovery and subsequent fix explained why we were getting the errors through the browser but none during the specifications. The browser passes params around as strings, and the tests submit actual integers.

A lesson to learn

Always verify you are not relying on conversion magic at some level when serializing, or testing in general. If it needs to be an integer, use an integer. If rails will pass an integer as a string in params, you should do the same in your tests and specifications.

I estimate the time we spent in proportion to the code of size n was about O(n), but the constant hidden by the big-o notation was rather large.

Written by spicycode

September 15, 2007 at 6:07 pm

Posted in programming

Tagged with , ,

Open Source Repo Moved

The open source repo that the RSpec Expectation Matchers and others are in has moved. The new location is:

http://edgerepo.com/svn/opensource

If you would like a shiny Warehouse interface just head over to:

http://opensource.edgerepo.com/

Written by spicycode

September 15, 2007 at 6:06 pm

Posted in programming

Tagged with ,

RSpec 1.0

RailsConf was quite fun this and I finally got a chance to meet David Chelimsky and Aslak Hellesøy on Saturday. Ken and I sat down with them and coded a patch or two for RSpec 1.0.

We got the “not implemented” spec feature added during this time. It worked like this:

describe YourClass do

  it "should do something awesome later"

end

By not giving the example (‘it’) a block you get a Not Implemented spec notice in all the various outputs. This will be yellow in the console output when color is on, and the same in the html output. Having not implemented specs will not break your build, and they do not count as errors or failures.

All in all a very fun time.

Written by spicycode

May 22, 2007 at 6:10 pm

Posted in programming

Tagged with , ,

Bitten by the hard breaks again

Another turn around the textile wheel of fortune in a rails application today yielded a lesson learned long ago but forgotten.

The textilize helper included with rails enables the hard_breaks mode by default. Hard Breaks converts 1 new line to a br and 2 newlines to a p tag. When you disable hard breaks mode you only get p tags.

Here’s how the source for that method looks:

def textilize(text)
  if text.blank?
    ""
  else
    textilized = RedCloth.new(text, [ :hard_breaks ])
    textilized.hard_breaks = true if textilized.respond_to?("hard_breaks=")
    textilized.to_html
  end
end

Here’s a simple override of it you can put in your application_helper.rb if you want the no hard breaks behaviour.

def textilize(text)
  if text.blank?
    ""
  else
    RedCloth.new(text).to_html
  end
end

Nothing new, just posting it here as a reminder to myself and a hopefully helpful tip to anyone not yet familiar with this difference.

Written by spicycode

May 13, 2007 at 6:12 pm

Posted in programming

Tagged with , ,

RSpec Extensions: Now in plugin form

After releasing the first version of the spicycode rspec extensions as a gem I’ve determined my development style (mainly controlled by free time) is currently much more suited to plugins.

So with that revelation at hand I’m here to announce the latest version of my RSpec Extensions featuring:

  • Several bug fixes, and expanded usage versions of existing validation matchers
  • New expectation matchers added in for basic be_valid, uniqueness, and work on the generic base validation expectation matcher.
  • Working association validation matchers

Install if you dare from the following subversion repository: http://svn.theedgecase.com/edgecase/goodies/plugins/rspec_expectation_matchers/

Many of the new extensions are from various blogs about the internet, and are clearly attributed as such. The goal of this project is to provide a complete suite of expectation matchers for common activerecord behaviour and operations.

Written by spicycode

April 22, 2007 at 5:51 pm

Posted in programming

Tagged with ,

Rspec Expectation Matchers – Part II

The new release of my RSpec extensions gem (along with a newly minted plugin version) will be out within a day or two. Barring any major show-stoppers I just need to finish adding :message => specification support so you can indicate what error message you expect to receive if it is invalid (if you choose to). Once that is in I’ll let the release notifications fly.

The goal of these expectation matchers is to allow you to test validations, and associations easily. Whether or not you wish to test these items I will leave up to you, but if you choose to this will definitely help keep things simple and quick.

Poor mans code outline

validate_presence_of(attr, options = { })
#    ex:  model.should validate_presence_of(:name)
#           model.should validate_presence_of(:name, :valid_value => 'Bob', :invalid_value => nil)
validate_confirmation_of(attr, options = { })

validate_uniqueness_of(attr, options = { })

validate_acceptance_of(attr, options = { })

validate_associated(associated_model, options = { })

validate_exclusion_of(attr, options = { })

validate_format_of(attr, options = { })

validate_inclusion_of(attr, options = { })

validate_length_of(attr, options = { })
alias_method :validate_size_of, :validate_length_of
#    ex:  model.should validate_length_of(:name, :within => 3..30)
#          model.should validate_length_of(:name, :maximum => 100)

validate_numericality_of(attr, options = { })

have_association(type, name, options = {})

belong_to(name, options = {})
#  ex: model.should belong_to(:parent)
#             (or more detail) model.should belong_to(:parent, :class_name => 'People')
have_one(name, options = {})

have_many(name, options = {})

have_and_belong_to_many(name, options = {})

Example usage:

context "A User" do

  setup do
    @user = User.new(valid_user_attributes)
  end

  specify do
    @user.should validate_presence_of(:first_name)
  end

  specify do
    @user.should validate_length_of(:first_name, :in => 1..50)
  end

  specify do
    @user.should validate_presence_of(:last_name)
  end

  specify do
    @user.should validate_length_of(:last_name, :in => 1..50)
  end

  specify do
    @user.should validate_presence_of(:username)
  end

  specify do
    @user.should validate_length_of(:username, :in => 3..40)
  end

  specify do
    @user.should validate_presence_of(:email)
  end

  specify do
    @user.should validate_length_of(:email, :in => 3..100)
  end

  specify do
    @user.should validate_presence_of(:password)
  end

  specify do
    @user.should validate_confirmation_of(:password)
  end

  specify do
    @user.should validate_presence_of(:account_id)
  end

  specify do 
    @user.should belong_to(:account)
  end

  specify do 
    @user.should have_many(:project_assignments)
  end  

  specify do 
    @user.should have_many(:projects, :source => :project, :through => :project_assignments, :order=>"projects.name")
  end

end

Example output:

A User
- should validate presence of first_name
- should validate length of first_name
- should validate presence of last_name
- should validate length of last_name
- should validate presence of username
- should validate length of username
- should validate presence of email
- should validate length of email
- should validate presence of password
- should validate confirmation of password
- should validate presence of account_id
- should have a belongs_to association called :account
- should have a has_many association called :project_assignments
- should have a has_many association called :projects

Written by spicycode

April 1, 2007 at 5:55 pm

Posted in programming

Tagged with , ,