lib/dread/graph.rb in dread-0.0.6 vs lib/dread/graph.rb in dread-0.0.7

- old
+ new

@@ -1,22 +1,25 @@ require 'dread/error' require 'dread/console_output' module Dread class Graph - attr_reader :clazz, :dependable_collection + attr_reader :clazz, :dependable_collection, :collected_clazzes - def initialize(clazz_data, pluralized=false) + def initialize(clazz_data, pluralized=false, collected_clazzes=[]) set_and_verify_clazz_and_relation(clazz_data) @pluralized = pluralized - @@tracker ||= [] + @collected_clazzes = collected_clazzes end # { user: { tweets: { comments: {} }, comments: {}, setting: {} } } def dependable_collection - @dependable_collection ||= + @dependable_collection ||= if self.clazz.in? collected_clazzes + { @relation.to_sym => { '...'.to_sym => {} } } + else { @relation.to_sym => collect_dependables } + end end def draw(output='console_output') case output when 'console_output' @@ -24,18 +27,10 @@ end end private - def track!(reflection) - @@tracker << reflection - end - - def tracked?(reflection) - @@tracker.include? reflection - end - def set_and_verify_clazz_and_relation(clazz_data) begin set_clazz_and_relation(clazz_data) rescue NameError => e raise Error.new("Unable to find class called #{clazz_data.classify}") @@ -43,15 +38,15 @@ end def set_clazz_and_relation(clazz_data) case clazz_data when ActiveRecord::Reflection::AssociationReflection - @clazz = (clazz_data.class_name || clazz_data.table_name).constantize + @clazz = (clazz_data.klass.to_s || clazz_data.table_name).constantize @relation = clazz_data.name when String @clazz = (clazz_data.classify).constantize - @relation = clazz_data + @relation = clazz_data.underscore when NilClass raise Error.new('Please pass a env var called class to proceed. E.g: rake dread class=user') else raise Error.new("Unable to proceed with #{clazz_data.class}") end @@ -62,14 +57,11 @@ @clazz.reflections.each do |assoc_name, assoc_data| case assoc_data.options[:dependent] when :delete relation_hash[assoc_name] = {} when :destroy - # unless tracked?(assoc_data) - # track!(assoc_data) - relation_hash.merge!( - Graph.new(assoc_data, assoc_data.macro == :has_many).dependable_collection) - # end + relation_hash.merge!( + Graph.new(assoc_data, assoc_data.macro == :has_many, collected_clazzes + [self.clazz]).dependable_collection) end end end end \ No newline at end of file