ConventionalModels

Generate ActiveRecord models automatically with basic relationships based on conventions.

Install

gem install conventional_models

Example

We have a table called Page and another called ContentItems (not following ActiveRecord conventions).

# point active_record to a database
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(:database => 'db.sqlite', :adapter => 'sqlite3')

require 'conventional_models'
ConventionalModels.configure do
  primary_key_name "Id"
end

# use the models
page = Page.create :Name => 'test'
page.content_items.create :Name => 'content1'

# have a look at the generated code
puts Page.model_code
puts ContentItem.model_code

Output:

class ::Page < ::ActiveRecord::Base
  set_primary_key "Id"
  set_table_name "Page"
  has_many :content_items, :class_name => 'ContentItem', :primary_key => 'Id', :foreign_key => 'Page_id'
end
class ::ContentItem < ::ActiveRecord::Base
  set_primary_key "Id"
  set_table_name "ContentItem"
  belongs_to :page, :class_name => 'Page'
end

Default conventions

ConventionalModels.configure do
  belongs_to_matcher {|column| column.name.end_with? "_id"}
  belongs_to_name {|column| column.name.gsub(/_id$/, "")}
  primary_key_name "id"
  class_name {|table_name| table_name.singularize.camelize}
  ignore_tables "schema_migrations", "sqlite_sequence", "sysdiagrams"
end

cmconsole command

Starts an IRB session and configures activerecord for you based on what is in config/database.yml.

Usage

cmconsole [options]

For help use: cmconsole -h

Options

-h, --help            Displays help message
-v, --version         Display the version, then exit
-e, --environment     Specify the database env to use, default to development
-c, --config          Where database.yml lives
-s, --skip-configure  Don't configure ConventionalModels automatically
-V, --verbose         Verbose

Rake

You can use this from rake as follows:

# Confiigure active_record as you would normally

task :console do
  require 'conventional_models'
  ConventionalModels.configure do
    primary_key_name "Id"
  end
  puts ConventionalModels.model_code
  IRB.start
end

Multiple databases

Given a table called pages in both development.sqlite and test.sqlite:

require 'rubygems'
require 'active_record'
require 'conventional_models'
config = {
  "development" => {
    "database" => 'development.sqlite',
    "adapter" => 'sqlite3'
  },
  "test" => {
    "database" => 'test.sqlite',
    "adapter" => 'sqlite3'
  }
}
ConventionalModels.configure do
  connection config["development"]
  module_name "Development"
end
ConventionalModels.configure do
  connection config["test"]
  module_name "Test"
end
puts ConventionalModels.model_code
# => class ::Development::Page < Development::Base
# => 
# => end
# => class ::Test::Page < Test::Base
# => 
# => end
Development::Page.create!
Development::Page.create!
Test::Page.create!
puts "Number of development records: #{Development::Page.count}"
# => 2
puts "Number of production records: #{Test::Page.count}"
# => 1

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2011 Steve Hodgkiss. See LICENSE for details.