modaldiagrams 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -1,6 +1,9 @@
1
1
  # ActiveRecord DB Diagrams
2
2
  # Configuration parameters can be changed by writing a file named config/modal_diagrams.yml
3
3
 
4
+ require 'modalsettings'
5
+ require 'modalsupport'
6
+
4
7
  module ModalDiagrams
5
8
 
6
9
  # Field type abbreviations
@@ -43,7 +46,13 @@ module ModalDiagrams
43
46
  cluster_classes = {} # assoc cluster name to array of class names
44
47
  relation_classes = [] # each element is the 2-element array of class names of the corresponding relation in relations
45
48
 
46
- models = dbmodels
49
+ model_selection_options = {
50
+ :all_models => cfg.include_all_models,
51
+ :dynamic_models => cfg.include_dynamic_models,
52
+ :include_files => false
53
+ }
54
+
55
+ models = dbmodels(model_selection_options.merge(:exclude_sti_models => true))
47
56
 
48
57
  models.each do |cls|
49
58
  if cls.respond_to?(:reflect_on_all_associations) && ActiveRecord::Base.connection.table_exists?(cls.table_name)
@@ -144,6 +153,7 @@ module ModalDiagrams
144
153
  end
145
154
 
146
155
  if cfg.show_sti
156
+ sti_classes = dbmodels(model_selection_options.merge(:exclude_non_sti_models => true))
147
157
  sti_classes.each do |sti_class|
148
158
  cls = sti_class.base_class
149
159
  if cls.respond_to?(:cluster)
@@ -202,26 +212,74 @@ module ModalDiagrams
202
212
 
203
213
  private
204
214
 
205
- # return ActiveRecord classes corresponding to tables, without STI derived classes, but including indirectly
206
- # derived classes that do have their own tables (to achieve this we use the convention that in such cases
207
- # the base class, directly derived from ActiveRecord::Base has a nil table_name)
208
- def dbmodels
209
- models = Dir.glob(File.join(Rails.root,"app/models/**/*.rb"))\
210
- .map{|f| File.basename(f).chomp(".rb").camelize.constantize}\
211
- .select{|c| has_table(c)}\
212
- .reject{|c| has_table(c.superclass)}
213
- models += ActiveRecord::Base.send(:subclasses).reject{|c| c.name.starts_with?('CGI::') || !has_table(c) || has_table(c.superclass)}
214
- models.uniq
215
- end
215
+ # Return the database models
216
+ # Options:
217
+ # :all_models # Return also models in plugins, not only in the app (app/models)
218
+ # :dynamic_models # Return dynamically defined models too (not defined in a model file)
219
+ # :exclude_sti_models # Exclude derived (STI) models
220
+ # :exclude_non_sti_models # Exclude top level models
221
+ # :include_files # Return also the model definition file pathnames (return pairs of [model, file])
222
+ # :only_app_files # But return nil for files not in the app proper
223
+ # :only_app_tree_files # But return nil for files not in the app directory tree (app, vendor...)
224
+ def dbmodels(options={})
225
+
226
+ models_dir = 'app/models'
227
+ if Rails.respond_to?(:application)
228
+ models_dir = Rails.application.paths[models_dir]
229
+ end
230
+ models_dir = Rails.root.join(models_dir)
231
+
232
+ if options[:all_models]
233
+ # Include also models from plugins
234
+ model_dirs = $:.grep(/\/models\/?\Z/)
235
+ else
236
+ # Only main application models
237
+ model_dirs = [models_dir]
238
+ end
239
+
240
+ models = []
241
+ files = {}
242
+ model_dirs.each do |base|
243
+ Dir.glob(File.join(base,"**/*.rb")).each do |fn|
244
+ model = File.basename(fn).chomp(".rb").camelize.constantize
245
+ models << model
246
+ files[model.to_s] = fn
247
+ end
248
+ end
249
+ models = models.sort_by{|m| m.to_s}
250
+
251
+ if options[:dynamic_models]
252
+ # Now add dynamically generated models (not having dedicated files)
253
+ # note that subclasses of these models are not added here
254
+ models += ActiveRecord::Base.send(:subclasses)
255
+ models = models.uniq
256
+ end
257
+
258
+ models = models.uniq.reject{|model| !has_table?(model)}
259
+
260
+ non_sti_models, sti_models = models.partition{|model| model.base_class==model}
216
261
 
217
- def sti_classes
218
- models = Dir.glob(File.join(Rails.root,"app/models/**/*.rb"))\
219
- .map{|f| File.basename(f).chomp(".rb").camelize.constantize}\
220
- .select{|c| has_table(c) && c.base_class!=c}
221
- models += ActiveRecord::Base.send(:subclasses).reject{|c| c.name.starts_with?('CGI::') || !has_table(c)}.select{|c| has_table(c) && c.base_class!=c}
222
- models.uniq
262
+ models = []
263
+ models += non_sti_models unless options[:exclude_non_sti_models]
264
+ models += sti_models unless options[:exclude_sti_models]
265
+ if options[:include_files]
266
+ models = models.map{|model| [model, files[model.to_s]]}
267
+ if options[:only_app_files] || options[:only_app_tree_files]
268
+ if options[:only_app_files]
269
+ suffix = models_dir.to_s
270
+ else
271
+ suffix = Rails.root.to_s
272
+ end
273
+ suffix += '/' unless suffix.ends_with?('/')
274
+ models = models.map{|model, file| [model, file && (file.starts_with?(suffix) ? file : nil)]}
275
+ end
276
+ end
277
+ models
223
278
  end
224
279
 
280
+ def has_table?(cls)
281
+ (cls != ActiveRecord::Base) && cls.respond_to?(:table_name) && cls.table_name.present?
282
+ end
225
283
  def assoc_foreign_key(assoc)
226
284
  # Up to ActiveRecord 3.1 we had primary_key_name in AssociationReflection; not it is foreign_key
227
285
  assoc.respond_to?(:primary_key_name) ? assoc.primary_key_name : assoc.foreign_key
@@ -3,6 +3,8 @@ module ModalDiagrams
3
3
  def self.parameters
4
4
  @settings ||= Settings[
5
5
  # Default values
6
+ :include_all_models => false, # include models defined in plugins
7
+ :include_dynamic_models => false, # include models defined dynamically (not in model files)
6
8
  :max_attributes => 24, # maximum number of attributes shown in a class (table)
7
9
  :clusters_not_shown_on_main_diagram => [], # clusters not shown in the main diagram
8
10
  :show_external => true, # show associations to classes from other clusters in cluster diagrams
@@ -0,0 +1,11 @@
1
+ namespace :db do
2
+ namespace :diagrams do
3
+
4
+ desc "Clean all db:diagrams generated fiels"
5
+ task :clean => 'environment' do
6
+ diagrams_dir = Rails.root.join("db/diagrams")
7
+ rm_rf diagrams_dir if File.exists?(diagrams_dir)
8
+ end
9
+
10
+ end
11
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "modaldiagrams"
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Javier Goizueta"]
12
- s.date = "2012-09-05"
12
+ s.date = "2012-09-06"
13
13
  s.description = "modaldiagrams provides Rake tasks for diagramming ActiveRecord databases. It generates Graphviz dot files."
14
14
  s.email = "jgoizueta@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/modaldiagrams/tasks.rb",
32
32
  "lib/tasks.rb",
33
33
  "lib/tasks/diagrams.rake",
34
+ "lib/tasks/diagrams_clean.rake",
34
35
  "lib/tasks/diagrams_pdf.rake",
35
36
  "lib/tasks/diagrams_png.rake",
36
37
  "lib/tasks/diagrams_ps.rake",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modaldiagrams
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-05 00:00:00.000000000 Z
12
+ date: 2012-09-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: modalsettings
@@ -146,6 +146,7 @@ files:
146
146
  - lib/modaldiagrams/tasks.rb
147
147
  - lib/tasks.rb
148
148
  - lib/tasks/diagrams.rake
149
+ - lib/tasks/diagrams_clean.rake
149
150
  - lib/tasks/diagrams_pdf.rake
150
151
  - lib/tasks/diagrams_png.rake
151
152
  - lib/tasks/diagrams_ps.rake
@@ -165,7 +166,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
165
166
  version: '0'
166
167
  segments:
167
168
  - 0
168
- hash: -2378591910060166729
169
+ hash: -996022215717107908
169
170
  required_rubygems_version: !ruby/object:Gem::Requirement
170
171
  none: false
171
172
  requirements: