public
Description: Annotate ActiveRecord models as a gem
Homepage: http://agilewebdevelopment.com/plugins/annotate_models
Clone URL: git://github.com/ctran/annotate_models.git
Search Repo:
Prior to this commit there was no support for models in subdirectories 
that weren't namespaced.
It's become a convention to clean up massive numbers of models by grouping 
them logically into
subdirectories (e.g. putting everything related to finances under 
app/models/finances).
This commit refactors the single line that retrieves the model class into 
a method that can
properly check for alternative class names.
JackDanger (author)
Sun Mar 09 12:27:43 -0700 2008
commit  a7e0b8cb6af6063c30b9208aba5b57dc6f8152a8
tree    239c38f2435c99056a8808f60856694a69b0e2fd
parent  c783e1b152c62f057ea23cf9a42403c5eb609e4f
...
65
66
67
68
 
69
70
71
 
72
73
74
...
80
81
82
83
 
84
85
86
...
91
92
93
 
 
 
 
 
 
 
 
 
 
 
 
 
94
95
96
97
98
99
100
101
102
...
105
106
107
108
109
 
110
111
 
112
113
114
 
 
115
116
117
 
118
119
120
121
122
...
65
66
67
 
68
69
70
 
71
72
73
74
...
80
81
82
 
83
84
85
86
...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
 
112
113
114
...
117
118
119
 
 
120
121
 
122
123
 
 
124
125
126
127
 
128
129
 
130
131
132
0
@@ -65,10 +65,10 @@ module AnnotateModels
0
   # on the columns and their types) and put it at the front
0
   # of the model and fixture source files.
0
 
0
- def self.annotate(klass, header)
0
+ def self.annotate(klass, file, header)
0
     info = get_schema_info(klass, header)
0
 
0
- model_file_name = File.join(MODEL_DIR, klass.name.underscore + ".rb")
0
+ model_file_name = File.join(MODEL_DIR, file)
0
     annotate_one_file(model_file_name, info)
0
 
0
     fixture_file_name = File.join(FIXTURE_DIR, klass.table_name + ".yml")
0
@@ -80,7 +80,7 @@ module AnnotateModels
0
   # the underscore or CamelCase versions of model names.
0
   # Otherwise we take all the model files in the
0
   # app/models directory.
0
- def self.get_model_names
0
+ def self.get_model_files
0
     models = ARGV.dup
0
     models.shift
0
 
0
@@ -91,12 +91,24 @@ module AnnotateModels
0
     end
0
     models
0
   end
0
+
0
+ # Retrieve the classes belonging to the model names we're asked to process
0
+ # Check for namespaced models in subdirectories as well as models
0
+ # in subdirectories without namespacing.
0
+ def self.get_model_class(file)
0
+ model = file.gsub(/\.rb$/, '').camelize
0
+ parts = model.split('::')
0
+ begin
0
+ parts.inject(Object) {|klass, part| klass.const_get(part) }
0
+ rescue LoadError
0
+ Object.const_get(parts.last)
0
+ end
0
+ end
0
 
0
   # We're passed a name of things that might be
0
   # ActiveRecord models. If we can find the class, and
0
   # if its a subclass of ActiveRecord::Base,
0
   # then pas it to the associated block
0
-
0
   def self.do_annotations
0
     header = PREFIX.dup
0
     version = ActiveRecord::Migrator.current_version rescue 0
0
@@ -105,18 +117,16 @@ module AnnotateModels
0
     end
0
 
0
     annotated = []
0
- self.get_model_names.each do |m|
0
- class_name = m.sub(/\.rb$/,'').camelize
0
+ self.get_model_files.each do |file|
0
       begin
0
- klass = class_name.split('::').inject(Object){ |klass,part| klass.const_get(part) }
0
+ klass = get_model_class(file)
0
         if klass < ActiveRecord::Base && !klass.abstract_class?
0
- annotated << class_name
0
- self.annotate(klass, header)
0
+ annotated << klass
0
+ self.annotate(klass, file, header)
0
         end
0
       rescue Exception => e
0
- puts "Unable to annotate #{class_name}: #{e.message}"
0
+ puts "Unable to annotate #{file}: #{e.message}"
0
       end
0
-
0
     end
0
     puts "Annotated #{annotated.join(', ')}"
0
   end

Comments

    No one has commented yet.