Home
The oracle-model-generator library is a Ruby library for generating an ActiveRecord model from an existing Oracle table. This will install an "omg" executable that you can use from the command line.
This is aimed at Rails users who are not developing green field applications, but instead are constrained by an existing Oracle database they must conform to.
Using the command line tool:
omg -d your_database -t locations -u some_user -p some_password
The above command results in a file called "location.rb". This is an ActiveRecord model declaration, with all validations, primary keys, table name and belongs_to relationships defined.
If your LOCATIONS table looks like this:
create table locations(
location_id number(4,0) primary key,
street_address varchar2(40),
postal_code varchar2(12),
city varchar2(30) not null
state_province varchar2(25),
country_id CHAR(2),
constraint "LOC_C_ID_FK" FOREIGN KEY (country_id)
references COUNTRIES (country_id)
)
The omg library will generate this:
class Location < ActiveRecord::Base
set_table_name :locations
set_primary_key :location_id
# Table relationships
belongs_to :countries
# Validations
validates :location_id, :presence => true, :numericality => {
:less_than_or_equal_to => 9999,
:greater_than_or_equal_to => -9999,
:only_integer => true
}
validates :street_address, :length => {:maximum => 40}
validates :postal_code, :length => {:maximum => 12}
validates :city, :length => {:maximum => 30}, :presence => true
validates :state_province, :length => {:maximum => 25}
validates :country_id, :length => {:maximum => 2}
end
It will also generate a corresponding test file using test-unit 2 by default. For the above example you will see some tests like this:
class TC_Location < Test::Unit::TestCase
def setup
@location = Location.new
end
test 'table name is locations' do
assert_equal('locations', Location.table_name)
end
test 'primary key is location_id' do
assert_equal('location_id', Location.primary_key)
end
test 'location_id basic functionality' do
assert_respond_to(@location, :location_id)
assert_nothing_raised{ @location.location_id }
assert_kind_of(Numeric, @location.location_id)
end
test 'location_id must be a number' do
@location.location_id = 'test_string'
assert_false(@location.valid?)
assert_true(@location.errors[:location_id].include?('is not a number'))
end
test 'location_id cannot exceed the value 9999' do
@location.location_id = 10000
assert_false(@location.valid?)
assert_true(@location.errors[:location_id].include?('must be less than or equal to 9999'))
end
# ... and so on.
end
I haven't used Oracle in years, and would like to turn it over to someone who is has use for it and is willing to maintain and update it as needed.