rails-erd 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.4.1:
2
+
3
+ * Fix processing of associations with class_name set to absolute module paths.
4
+ * Adjust model loading process to include models in non-standard paths eagerly.
5
+
1
6
  === 0.4.0:
2
7
 
3
8
  * Support to optionally display single table inheritance relationships
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails-erd (0.3.0)
4
+ rails-erd (0.4.0)
5
5
  activerecord (~> 3.0)
6
6
  activesupport (~> 3.0)
7
7
  ruby-graphviz (~> 0.9.18)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -30,7 +30,7 @@ module RailsERD
30
30
  end
31
31
 
32
32
  def association_target(association)
33
- association.class_name
33
+ association.options[:polymorphic] ? association.class_name : association.klass.name
34
34
  end
35
35
  end
36
36
 
@@ -58,8 +58,8 @@ module RailsERD
58
58
  else
59
59
  # Many-to-many associations don't have a clearly defined direction.
60
60
  # We sort by name and use the first model as the source.
61
- source = associations.first.active_record
62
- associations.partition { |association| association.active_record == source }
61
+ source = associations.map(&:active_record).sort_by(&:name).first
62
+ associations.partition { |association| association.active_record != source }
63
63
  end
64
64
 
65
65
  assoc = @forward_associations.first || @reverse_associations.first
@@ -15,18 +15,17 @@ namespace :erd do
15
15
  end
16
16
 
17
17
  task :load_models do
18
- say "Loading Active Record models..."
19
-
18
+ say "Loading application environment..."
20
19
  Rake::Task[:environment].invoke
21
- Rails.application.config.paths.app.models.paths.each do |model_path|
22
- Dir["#{model_path}/**/*.rb"].sort.each do |file|
23
- require_dependency file
24
- end
25
- end
20
+
21
+ say "Loading code in search of Active Record models..."
22
+ Rails.application.eager_load!
23
+
24
+ raise "Active Record was not loaded." unless defined? ActiveRecord
26
25
  end
27
26
 
28
27
  task :generate => [:options, :load_models] do
29
- say "Generating Entity-Relationship Diagram..."
28
+ say "Generating Entity-Relationship Diagram for #{ActiveRecord::Base.descendants.length} models..."
30
29
 
31
30
  require "rails_erd/diagram/graphviz"
32
31
  file = RailsERD::Diagram::Graphviz.create
data/rails-erd.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rails-erd}
8
- s.version = "0.4.0"
8
+ s.version = "0.4.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Rolf Timmermans"]
12
- s.date = %q{2010-10-12}
12
+ s.date = %q{2010-10-14}
13
13
  s.description = %q{Automatically generate an entity-relationship diagram (ERD) for your Rails models.}
14
14
  s.email = %q{r.timmermans@voormedia.com}
15
15
  s.extra_rdoc_files = [
data/test/test_helper.rb CHANGED
@@ -109,14 +109,16 @@ class ActiveSupport::TestCase
109
109
  private
110
110
 
111
111
  def reset_domain
112
- ActiveRecord::Base.descendants.each do |model|
113
- Object.send :remove_const, model.name.to_sym
114
- end
115
- ActiveRecord::Base.connection.tables.each do |table|
116
- ActiveRecord::Base.connection.drop_table table
112
+ if defined? ActiveRecord
113
+ ActiveRecord::Base.descendants.each do |model|
114
+ Object.send :remove_const, model.name.to_sym
115
+ end
116
+ ActiveRecord::Base.connection.tables.each do |table|
117
+ ActiveRecord::Base.connection.drop_table table
118
+ end
119
+ ActiveRecord::Base.direct_descendants.clear
120
+ Arel::Relation.send :class_variable_set, :@@connection_tables_primary_keys, {}
121
+ ActiveSupport::Dependencies::Reference.clear!
117
122
  end
118
- ActiveRecord::Base.direct_descendants.clear
119
- Arel::Relation.send :class_variable_set, :@@connection_tables_primary_keys, {}
120
- ActiveSupport::Dependencies::Reference.clear!
121
123
  end
122
124
  end
@@ -127,6 +127,18 @@ class DomainTest < ActiveSupport::TestCase
127
127
  assert_equal [Domain::Relationship] * 2, Domain.generate.relationships.collect(&:class)
128
128
  end
129
129
 
130
+ test "relationships should use model name first in alphabet as source for many to many relationships" do
131
+ create_table "many_more", :many_id => :integer, :more_id => :integer
132
+ create_model "Many" do
133
+ has_and_belongs_to_many :more
134
+ end
135
+ create_model "More" do
136
+ has_and_belongs_to_many :many
137
+ end
138
+ relationship = Domain.generate.relationships.first
139
+ assert_equal ["Many", "More"], [relationship.source.name, relationship.destination.name]
140
+ end
141
+
130
142
  # Specialization processing ================================================
131
143
  test "specializations should return empty array for empty domain" do
132
144
  assert_equal [], Domain.generate.specializations
@@ -29,6 +29,42 @@ class RakeTaskTest < ActiveSupport::TestCase
29
29
  assert !File.exists?("ERD.dot")
30
30
  end
31
31
 
32
+ test "generate task should eager load application environment" do
33
+ eager_loaded, environment_loaded = nil
34
+ Object::Quux = Module.new
35
+ Object::Quux::Application = Class.new
36
+ Object::Rails = Struct.new(:application).new(Object::Quux::Application.new)
37
+ Rails.application.class_eval do
38
+ define_method :eager_load! do
39
+ eager_loaded = true
40
+ end
41
+ end
42
+ Rake::Task.define_task :environment do
43
+ environment_loaded = true
44
+ end
45
+ create_simple_domain
46
+ Rake::Task["erd:generate"].invoke
47
+ assert_equal [true, true], [eager_loaded, environment_loaded]
48
+ end
49
+
50
+ test "generate task should complain if active record is not loaded" do
51
+ Object::Quux = Module.new
52
+ Object::Quux::Application = Class.new
53
+ Object::Rails = Struct.new(:application).new(Object::Quux::Application.new)
54
+ Rails.application.class_eval do
55
+ define_method :eager_load! do end
56
+ end
57
+ Rake::Task.define_task :environment
58
+ Object.send :remove_const, :ActiveRecord
59
+ message = nil
60
+ begin
61
+ Rake::Task["erd:generate"].invoke
62
+ rescue => e
63
+ message = e.message
64
+ end
65
+ assert_equal "Active Record was not loaded.", message
66
+ end
67
+
32
68
  # Option processing ========================================================
33
69
  test "options task should ignore unknown command line options" do
34
70
  ENV["unknownoption"] = "value"
@@ -34,6 +34,15 @@ class RelationshipTest < ActiveSupport::TestCase
34
34
  assert_equal [domain.entity_by_name("Foo")], domain.relationships.map(&:destination)
35
35
  end
36
36
 
37
+ test "destination should return relationship destination if specified with absolute module path" do
38
+ create_model "Foo", :bar => :references
39
+ create_model "Bar" do
40
+ has_many :foos, :class_name => "::Foo"
41
+ end
42
+ domain = Domain.generate
43
+ assert_equal [domain.entity_by_name("Foo")], domain.relationships.map(&:destination)
44
+ end
45
+
37
46
  # Relationship properties ==================================================
38
47
  test "mutual should return false for one way relationship" do
39
48
  create_model "Foo", :bar => :references do
@@ -261,7 +270,7 @@ class RelationshipTest < ActiveSupport::TestCase
261
270
  More.class_eval do
262
271
  validates_length_of :many, :maximum => 29, :minimum => 7
263
272
  end
264
- assert_equal [Domain::Relationship::Cardinality.new(3..18, 7..29)], domain_cardinalities
273
+ assert_equal [Domain::Relationship::Cardinality.new(7..29, 3..18)], domain_cardinalities
265
274
  end
266
275
 
267
276
  test "cardinality should be n-m to n-m for limited many to many associations with multiple validations" do
@@ -278,7 +287,7 @@ class RelationshipTest < ActiveSupport::TestCase
278
287
  validates_length_of :many, :minimum => 9
279
288
  validates_length_of :many, :maximum => 17
280
289
  end
281
- assert_equal [Domain::Relationship::Cardinality.new(3..20, 9..17)], domain_cardinalities
290
+ assert_equal [Domain::Relationship::Cardinality.new(9..17, 3..20)], domain_cardinalities
282
291
  end
283
292
 
284
293
  # Cardinality for non-mutual relationships =================================
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 0
9
- version: 0.4.0
8
+ - 1
9
+ version: 0.4.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Rolf Timmermans
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-12 00:00:00 +02:00
17
+ date: 2010-10-14 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency