lib/ar2gostruct.rb in ar2gostruct-0.0.5 vs lib/ar2gostruct.rb in ar2gostruct-0.1.0
- old
+ new
@@ -30,11 +30,16 @@
Rails.application.eager_load!
end
def self.get_schema_info(klass)
info = "// Table name: #{klass.table_name}\n"
- info << "type #{klass.table_name.camelize} struct {\n"
+ if ENV['plural']
+ struct_name = klass.table_name.camelize
+ else
+ struct_name = klass.to_s.tr_s('::', '')
+ end
+ info << "type #{struct_name} struct {\n"
max_size = klass.column_names.collect{|name| name.size}.max + 1
klass.columns.each do |col|
tags = []
@@ -54,10 +59,16 @@
end
# default value
if col.default
orm_option << "default:'#{col.default}'"
end
+ # set timestamp
+ if col.name == "created_at"
+ orm_option << "created"
+ elsif col.name == "updated_at"
+ orm_option << "updated"
+ end
if orm_option.present?
tags << "qbs:\"#{orm_option.join(",")}\""
end
end
@@ -93,16 +104,14 @@
end
models
end
def self.convert!
- annotated = []
self.get_model_names.each do |m|
class_name = m.sub(/\.rb$/,'').camelize
begin
klass = class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) }
if klass < ActiveRecord::Base && !klass.abstract_class?
- annotated << class_name
self.convert_to_gostruct(klass)
end
rescue Exception => e
puts "// Unable to convert #{class_name}: #{e.message}"
end