Module: MongoidSchemaDump
- Defined in:
- lib/mongoid_schema_dump.rb,
lib/mongoid_schema_dump/engine.rb,
lib/mongoid_schema_dump/version.rb,
lib/rails/generators/mongoid_schema_dump/task_generator.rb
Defined Under Namespace
Modules: Generators Classes: Engine
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
-
.all_belongs_to(from_klass) ⇒ Object
returns all belongs_to class names.
- .dump(reference_klass_name) ⇒ Object
- .export_options(klass_name, ref) ⇒ Object
-
.has_many(from_klass, to_klass) ⇒ Object
returns all has_many relation class names.
- .models ⇒ Object
-
.paths_to_klass(klass, reference_klass) ⇒ Object
Computes all paths to reference class.
- .shortest_array(arrays) ⇒ Object
Class Method Details
.all_belongs_to(from_klass) ⇒ Object
returns all belongs_to class names
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/mongoid_schema_dump.rb', line 42 def all_belongs_to(from_klass) monomorphic_relations = from_klass.relations.map(&:second).select do |r| r.macro == :belongs_to && !r[:polymorphic] end.map do |relation| relation.class_name end polymorphic_relations = from_klass.relations.map(&:second).reduce([]) do |memo, r| if r.macro == :belongs_to && r[:polymorphic] == true models.each do |model_klass_name| next unless has_many(model_klass_name.constantize, from_klass).any? next if monomorphic_relations.include? model_klass_name memo << model_klass_name end end memo end monomorphic_relations + polymorphic_relations end |
.dump(reference_klass_name) ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/mongoid_schema_dump.rb', line 112 def dump(reference_klass_name) #path_to_reference_klass(model_name.constantize, "Guest") schema = [] models.each do |model_name| res = (model_name, reference_klass_name) schema << res if res.present? end return schema end |
.export_options(klass_name, ref) ⇒ Object
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 |
# File 'lib/mongoid_schema_dump.rb', line 75 def (klass_name, ref) path = shortest_array(paths_to_klass(klass_name.constantize, ref)) return {} if path.count <= 1 dependency_klass = path.second #1. Standard belongs_to relation = klass_name.constantize.relations.map(&:second).select do |r| r.macro == :belongs_to && !r[:polymorphic] && r.class_name == dependency_klass.to_s end.first if relation.present? return { "collection" => klass_name.constantize.collection.name, "dependency" => dependency_klass.collection.name, "foreign_key" => relation.key } end #2. Handle polymorphic relations hm_names = dependency_klass.relations.map(&:second).select do |r| r.macro == :has_many && r.class_name == klass_name end.map(&:as) relation = klass_name.constantize.relations.map(&:second).select do |r| r.macro == :belongs_to && r[:polymorphic] == true && hm_names.include?(r.name) end.first return nil if relation.blank? return { "collection" => klass_name.constantize.collection.name, "dependency" => dependency_klass.collection.name, "foreign_key" => relation.key, "filters" => { "#{relation.name}_type" => dependency_klass.to_s } } end |
.has_many(from_klass, to_klass) ⇒ Object
returns all has_many relation class names
35 36 37 38 39 |
# File 'lib/mongoid_schema_dump.rb', line 35 def has_many(from_klass, to_klass) all_has_many = from_klass.relations.map(&:second).select do |r| r.macro == :has_many && r.class_name == to_klass.to_s end.map(&:class_name) end |
.models ⇒ Object
62 63 64 |
# File 'lib/mongoid_schema_dump.rb', line 62 def models Mongoid.models.map &:to_s end |
.paths_to_klass(klass, reference_klass) ⇒ Object
Computes all paths to reference class
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 |
# File 'lib/mongoid_schema_dump.rb', line 8 def paths_to_klass(klass, reference_klass) displayed_for_klass = false paths = [] to_visit = [[klass, []]] current_klass = klass loop do current_klass, path = to_visit.pop #FIFO break unless current_klass path << current_klass if current_klass.to_s == reference_klass paths << path next end break if current_klass.nil? next_klasses = all_belongs_to(current_klass) next_klasses.each do |next_klass| next if next_klass == current_klass.to_s to_visit << [next_klass.constantize, path.clone] end break if to_visit.empty? end return paths end |
.shortest_array(arrays) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/mongoid_schema_dump.rb', line 66 def shortest_array(arrays) result = arrays.reduce({path: [], size: 1000000}) do |memo, current| new_size = [current.count, memo[:size]].min memo = {path: current, size: new_size} if new_size < memo[:size] memo end result[:path] end |