How to add a boolean column to a table in rails

Leave a comment

As a reminder to myself, here are some notes on how to add a boolean column to a table in rails3.

Step 1: Create migration

rails generate migration add_public_to_listings admin:boolean

Step 2: Write unit tests on model

listing_spec.rb

    describe "admin attribute" do

      before(:each) do
        @listing = @user.listings.create!(@attr)
      end

      it "should respond to public" do
        assert_respond_to(@listing, :public)
      end

      it "should not be public be default" do
        assert !@listing.public
      end

      it "should be converible" do
        @listing.toggle!(:public)
        assert @listing.public
      end
    end

Step 3: Update test data

populate.rake

def make_listings
  puts "----------------"
  puts "--- listings ---"
  puts "----------------"

  myUser = User.find_by_email("a@a.com")

  ls1 = myUser.listings.create!(:title => "The Mona Lisa",
                               :description => "Master peice",
                               :category_id => 2,
                               :address => "T3H0E2",
                               :location => "Calgary, Alberta, Canada")
  ls1.toggle!(:public) 

Note: We set the :public value through the toogle for security reasons. Only those variables exposed via attr_accessible

class Listing < ActiveRecord::Base
attr_accessible :title, :description, :category_id, :address, :location

can be set through mass assignment. This prevents bad guys from changing values like this:

put /listings/17?public=1

Thanks again Michael for the great example.

How to setup Jasmine Rails31 directory structure

Leave a comment

Following are instructions on how to setup/configure jasmine for use with rails31. It’s mostly straight forward, just one gotcha with regards to how to tell jasmine where your source files are.

1. Add jasmine gem

http://asciicasts.com/episodes/261-testing-javascript-with-jasmine has a nice summary of how to setup and install the rails gem for jasmine.

Gemfile

gem 'jasmine', :group => [:development, :test]

> bundle install
> rails g jasmine:install
> rake jasmine

2. Create your test.

spec/javascripts/listings_model_spec.js

describe("ListingModelSpec", function() {

  beforeEach(function() {
    setFixtures('<input type="hidden" id="city_name" value="Calgary" />');
  });

  it("gets the city name", function() {
    var model = new ListingsModel();
  });

});

Here I am writing a test that verifies I can read a value from a hidden field. To do that, jasmine will need jquery, and another useful helper library called jasmine-jquery (for that setFixture method).

Download and place them both in the /spec/javascripts/helpers dir.

4. Tell jasmine where to find your javascript files.

jasmine knows where to find your src files via it’s jasmine.yml file.

Assuming you want your javascript files to live under apps (where rails31 like them)

Edit the jasmine.yml file to look something like this:

/spec/javascripts/support/jasmine.yml

# src_files
#
# Return an array of filepaths relative to src_dir to include before jasmine specs.
# Default: []
#
# EXAMPLE:
#
# src_files:
#   - lib/source1.js
#   - lib/source2.js
#   - dist/**/*.js
#
src_files:
  - public/javascripts/prototype.js
...
  - app/assets/javascripts/**/*.js

See that last line at he bottom? That’s the one you need to point to the rails3 javascript directory. The other files above it points to where rails used to share it’s javascript files but everything is now stored under apps so just point there.

With that you should now be able to run your own javascript tests by firing up the jasmine server:

> rake jasmine

Going to localhost:8888 and seeing your output there.

More resources

http://railscasts.com/episodes/261-testing-javascript-with-jasmine

Rails3.1 link problem with strange hrefs

2 Comments

If you recently upgrade your rails app and you are using blueprint, you may have an issue with your links.

Due to the new way rails handles style sheets, certain blueprint styles that used to work may clash or cause strange behaviour (like all your hyperlinks suddenly having the href link itself showing up in the text of the link on the page).

To stop it delete the following lines out of your blueprint print.css file:

a:link:after, a:visited:after {
  content: " (" attr(href) ")";
  font-size: 90%;
}

Here is a similar question asked on stackoverflow.

%d bloggers like this: