Module: SchemaToYaml
- Defined in:
- lib/restfulx/rails/schema_to_yaml.rb,
lib/restfulx/rails/schema_to_yaml/settings/core.rb,
lib/restfulx/rails/schema_to_yaml/settings/config.rb
Overview
SchemaToYaml settings
Defined Under Namespace
Modules: Settings
Class Method Summary collapse
-
.schema_to_yaml ⇒ Object
SchemaToYaml.schema_to_yaml - set of commands that introspects your database and formats your model.yml for export.
Class Method Details
.schema_to_yaml ⇒ Object
SchemaToYaml.schema_to_yaml
- set of commands that introspects your database and formats your model.yml for export
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/restfulx/rails/schema_to_yaml.rb', line 7 def self.schema_to_yaml # Iterates through your database, and sets up table_arr with all columns # - excludes schema_info/schema_migrations/and any other tables you specify in restfulx.yml table_arr = ActiveRecord::Base.connection.tables - %w(schema_info schema_migrations).map - RxSettings.ignored.tables[0].split # Excludes columns from each table disregarded_columns = %w(id created_at updated_at) + RxSettings.ignored.fields[0].split # Schema variable is appended to throughout, and is the variable exported to model.yml schema = [] # Set up empty array for has_many relationships @array_of_has_manies = [] # Iterates through each table, and checks for any database field ending in _id table_arr.each do |table| column_arr = ActiveRecord::Base.connection.columns(table) column_arr.each do |col| col_name = col.name.to_s @array_of_has_manies << "#{col_name.gsub(/_id\b/,'')}_#{table}" if col_name[-3,3]=='_id' end end table_arr.each do |table| # Set up empty arrays for other associations belong_tos = [] has_manies = [] polymorphics = [] # Append table name to schema # - project: schema << "#{table.singularize}:\n" column_arr = ActiveRecord::Base.connection.columns(table) column_arr.each do |col| col_name = col.name.to_s # Ignore certain columns columns_check = [] disregarded_columns.each {|dc| columns_check << col_name.include?(dc) } # Sets up polymorphics array, we'll need to check for duplicates below polymorphics << col_name.gsub('_id','PMCHECK').gsub('_type','PMCHECK') # Appends each column under respective table schema << " - #{col_name}: #{col.type}\n" unless columns_check.include?(true) # Checks for certain column names # - If it finds parent_id it sets up a tree_model for generation # - If it finds _file_size it sets up attachment for generation # - Sets up belong_tos if col_name == 'parent_id' schema << " - tree_model: [#{col_name.gsub(/_id\b/,'')}]\n" elsif col_name =~ /_file_size$/ schema << " - attachment_field: [#{col_name.gsub(/_file_size$/,'')}]\n" else belong_tos << col_name.gsub(/_id\b/,', ') if col_name[-3,3]=='_id' && !disregarded_columns.include?(col_name) end end # Checks for duplicates in the polymorphics array (used for error checking) if polymorphics.dups.size > 0 schema << " - polymorphic: [#{polymorphics.dups.first.gsub('PMCHECK','')}]\n" @polymorphic = polymorphics.dups.first.gsub('PMCHECK','') end # Cleans up has many @array_of_has_manies.each do |hm| sanity_check = hm.gsub(/^#{table.singularize}_/,'') if hm =~ /^#{table.singularize}_/ && table_arr.include?(sanity_check) has_manies << hm.gsub(/^#{table.singularize}_/,'') + ', ' end end # Appends belong_to's to schema ##Patch here ## http://gist.github.com/151713 if all of the belongs_to on a particular table are polymorphic, then belong_tos is empty, so belong_tos.last is nil, so 87 (before patch) blows up if belong_tos.size > 0 belong_tos = belong_tos.delete_if {|x| x == "#{@polymorphic}, " } if belong_tos.size > 0 last_in_array_fix = belong_tos.last last_in_array_fix = last_in_array_fix.gsub(', ','') belong_tos.pop belong_tos << last_in_array_fix schema << " - belongs_to: [#{belong_tos}]\n" end end # Appends has_manies' to schema if has_manies.size > 0 last_in_array_fix = has_manies.last last_in_array_fix = last_in_array_fix.gsub(', ','') has_manies.pop has_manies << last_in_array_fix schema << " - has_many: [#{has_manies}]\n" end schema << "\n" end # Writes model.yml file yml_file = File.join(RAILS_ROOT, "db", "model.yml") File.open(yml_file, "w") { |f| f << schema.to_s } puts "Model.yml created at db/model.yml" end |