Skip to content

Commit

Permalink
moved the ar connection code into its own file so I can re-use it in …
Browse files Browse the repository at this point in the history
…the loader code. Cleaned up the migration_generator class to remove duplicated code
  • Loading branch information
rippinrobr committed Oct 13, 2011
1 parent 390f81e commit 807ae99
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
31 changes: 18 additions & 13 deletions lib/generator/languages/ruby/migration_generator.rb
Expand Up @@ -25,24 +25,29 @@ def clean_db_data_types(db_type)
end

def generate_migration
template = File.join(File.dirname(__FILE__),
"templates/migration.erb")
b = binding
engine = ERB.new(File.read(template), 0, '-%>')
write_class_file(@options[:migration_output_dir],
@options[:migration_file_name],
engine.result(b),
"rb")
process_template("templates/migration.erb",
@options[:migration_output_dir],
@options[:migration_file_name])
end

def generate_ar_model
process_template("templates/ar_model.erb",
@options[:migration_model_dir],
"#{@options[:db_table_name]}_source")
end

def generate_ar_connection
process_template( "templates/ar_connection.erb",
@options[:migration_output_dir],
"ar_connection")
end

private
def process_template(template_path, output_dir, file_name)
template = File.join(File.dirname(__FILE__),
"templates/ar_model.erb")
template_path)
b = binding
engine = ERB.new(File.read(template), 0, '-%>')
write_class_file(@options[:migration_model_dir],
"#{@options[:db_table_name]}_source",
engine.result(b),
"rb")
write_class_file(output_dir, file_name, engine.result(b), "rb")
end
end
10 changes: 10 additions & 0 deletions lib/generator/languages/ruby/templates/ar_connection.erb
@@ -0,0 +1,10 @@
#---------------------------------------------------------------------
# Generated on <%= Time.new.inspect -%> by <%= Etc.getlogin %>
# Using Generator 0.1.1
#---------------------------------------------------------------------
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => '<%= @options[:db_adapter] -%>',
:database => '<%= @options[:db_path] -%>'
)
7 changes: 2 additions & 5 deletions lib/generator/languages/ruby/templates/migration.erb
Expand Up @@ -4,14 +4,11 @@
#---------------------------------------------------------------------
require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => '<%= @options[:db_adapter] -%>',
:database => '<%= @options[:db_path] -%>'
)
load File.join(File.dirname(__FILE__), 'ar_connection.rb')

class CreateMigration < ActiveRecord::Migration
def change
create_table :<%= @options[:db_table_name] -%>_source do |t| t.integer :id, :null => false
create_table :<%= @options[:db_table_name] -%>_sources do |t| t.integer :id, :null => false
<% @columns.map do |col| -%>
t.<%= clean_db_data_types(col.db_data_type) %> :<%= col.db_name %>
<% end %>
Expand Down

0 comments on commit 807ae99

Please sign in to comment.