Posts mit dem Label rake werden angezeigt. Alle Posts anzeigen
Posts mit dem Label rake werden angezeigt. Alle Posts anzeigen

Montag, 6. Oktober 2008

Loading Tables with Defauls Application Data in Migrations

1. Rake a migration to make the table
2. Make a new directory 'development_data' in app/db/migrate/
3. Make a YML file with the "tablename.yml" (check pural!) containing the default data for the table you need for the application (don't mix wiht test data)
4. Run rake db:migrate to create the tables and load them with the default data from the YML

==== Table Create Migration example

> ruby script/generate migration Certificates title:string description:text

==== YML example


certificate:
..id: 1
..title: "All About This Certificate"
..description: "Blah Blah."

..id: 2
..title: "All About This Certificate"
..description: "More Blah Blah."

==== Create Load Migration example

> ruby script/generate migration Certificates title:string description:text

==== Load Data example


class LoadCertificatesData <>
..def self.up
....down
....directory=File.join(File.dirname(__FILE__),"development_data")
....Fixtures.create_fixtures(directory,"certificates")
..end

..def self.down
....Certificate.delete(:all)
..end
end


==== Load Data example

This strategy might fail over time if you are adding and deleting existing colums in migrations, so you might have to end up doing all your load migrations after all the structural migrations have finished.

However, this might fail to if table relations ships are complex - so then you might have to fall back to an administration interface in the application where you build the tables from the YML file with your own programming.

Dienstag, 30. September 2008

Rake migrations effecting exisitng tables

Things that can done:

Adding a new table, Removing a table, Adding a column to an existing table, Removing a column, Renaming a column, Renaming a table, Adding an index, Adding a primary key.

>ruby script/generate migrtion add_price

This creates a new migration file in db/migrations

class AddPrice
...def self.up
.....add_column :products, :price, decimal, :precision => 8, :scale => 2, :default => 0
...end

...def self.down
......remove_column :products, :price
...end
end

Samstag, 30. August 2008

RSpec CheatSheet



















>rake -T spec (output of the available rake tasks for rspec)
>ruby script/spec -h (output availble plugin rspec options)
>ruby script/generate (shows the rspec generators that replace the normal rails generators)
(i.e. ruby script/generate rspec_scaffold will show the available options)
>rake doc:plugins (generate the rspec documentation in doc/plugins/rspec-rails/index.html)
>rake spec (sync the development environement with the test environment before running autotest)
>autotest (starts the background testing)

IMPORTANT: Set the test rspec it set up to work in the test environment, so make sure it has been set
Also see extention spec helper methods post

Freitag, 20. Juni 2008

Rake Scaffold

ruby script/generate scaffold SingularModelName data_field_name:type -c

The -c adds the file generated to the svn repository.