Created
January 16, 2016 06:40
-
-
Save pocke/5d73ab5ba6e8011c38ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :model_from_schema do | |
desc 'Generate model files from db schema' | |
task gen: :environment do | |
module ModelGenerator | |
Models = {} | |
module Evaluator | |
module_function | |
def create_table(table_name, *) | |
Models[table_name.classify] = { | |
has_many: [], | |
belongs_to: [], | |
} | |
end | |
# 無視 | |
def add_index(*) end | |
def add_foreign_key(from, to) | |
fromc, toc = from.classify, to.classify | |
Models[fromc][:belongs_to].push to.singularize | |
Models[toc][:has_many].push from | |
end | |
end | |
def self.save | |
Models.each do |klass, data| | |
code = [ | |
"class #{klass} < ActiveRecord::Base", | |
data[:has_many].map{|x| " has_many :#{x}"}, | |
data[:belongs_to].map{|x| " belongs_to :#{x}"}, | |
"end\n", | |
].flatten.join("\n") | |
path = Rails.root.join('app', 'models', "#{klass.underscore}.rb") | |
File.write(path, code) | |
end | |
end | |
end | |
s = ActiveRecord::Schema | |
def s.define(*, &block) | |
ModelGenerator::Evaluator.class_eval(&block) | |
end | |
load Rails.root.join('db', 'schema.rb') | |
ModelGenerator.save | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment