Skip to content

Commit 13fe838

Browse files
committed
git-er-done
1 parent a58c73f commit 13fe838

File tree

4 files changed

+53
-56
lines changed

4 files changed

+53
-56
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,29 @@ class Document < ActiveRecord::Base
4747

4848
### Configuration
4949

50-
The annotation process can be configured via the `ActiveRecord::Annotate.configure` block which is handy to keep in the initializer.
50+
The annotation process can be configured via the `ActiveRecord::Annotate.configure` block which is handy to keep in an initializer.
5151

5252
You can generate the basic initializer with a built-in generator:
5353

5454
```sh
5555
$ rails generate active_record:annotate:install
5656
```
5757

58-
It creates an initializer at `config/initializers/annotate.rb` which contains descriptive comments about all settings (currently just one setting, `yard`).
59-
60-
### Ignoring Certain Models
61-
62-
If you would like to ignore certain models simply create a `.annotate_ignore` file in the root folder of your Rails project
58+
It creates an initializer at `config/initializers/annotate.rb` which contains descriptive comments about each setting.
6359

6460
```ruby
65-
# .annotate_ignore
66-
67-
SomeModel
68-
AnotherIgnoredModel
61+
require 'active_record/annotate'
62+
63+
if defined?(ActiveRecord::Annotate)
64+
ActiveRecord::Annotate.configure do |config|
65+
# # set this to true to wrap annotations in triple backticks (```)
66+
# # so YARD documentation can process the annotation as a code block
67+
# config.yard = false
68+
#
69+
# # Define any models to be skipped by Annotate
70+
# config.ignored_models = [SomeIgnoredModel, AnotherIgnoredModel]
71+
end
72+
end
6973
```
7074

7175
## Changelog

lib/active_record/annotate.rb

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,98 +9,74 @@ module Annotate
99
class << self
1010
def annotate
1111
processed_models = []
12-
12+
1313
models.each do |table_name, file_paths_and_classes|
1414
annotation = Dumper.dump(table_name)
15-
15+
1616
file_paths_and_classes.each do |path, klass|
1717
file = File.new(path)
1818
file.annotate_with(annotation.dup, configurator)
19-
19+
2020
if file.changed?
2121
file.write
2222
processed_models << "#{klass} (#{file.relative_path})"
2323
end
2424
end
2525
end
26-
26+
2727
unless processed_models.empty?
2828
puts 'Annotated models:'
2929
processed_models.each do |model|
3030
puts " * #{model}"
3131
end
3232
end
3333
end
34-
34+
3535
def models
3636
files_mask = models_dir.join('**', '*.rb')
37-
37+
3838
hash_with_arrays = Hash.new do |hash, key|
3939
hash[key] = []
4040
end
41-
41+
4242
Dir.glob(files_mask).each_with_object(hash_with_arrays) do |path, models|
4343
short_path = short_path_for(path)
4444
next if short_path.starts_with?('concerns') # skip any app/models/concerns files
45-
45+
4646
klass = class_name_for(short_path)
4747

4848
next unless klass < ActiveRecord::Base # collect only AR::Base descendants
4949
next if klass.respond_to?(:abstract_class?) && klass.abstract_class?
50-
next if annotate_ignore?(klass)
51-
50+
next if configurator.ignored_models.includes?(klass)
51+
5252
models[klass.table_name] << [path, klass]
5353
end
5454
end
55-
55+
5656
# .../app/models/car/hatchback.rb -> car/hatchback
5757
def short_path_for(full_path)
5858
full_path.sub(models_dir.to_s + '/', '').sub(/\.rb$/, '')
5959
end
60-
60+
6161
# car/hatchback -> Car::Hatchback
6262
def class_name_for(short_path)
6363
short_path.camelize.constantize
6464
end
65-
65+
6666
def configure(&block)
6767
configurator.tap(&block)
6868
end
69-
69+
7070
private
7171

7272
def models_dir
7373
Rails.root.join('app/models')
7474
end
75-
75+
7676
def configurator
7777
@configurator ||= Configurator.new
7878
end
7979

80-
def annotate_ignore?(klass)
81-
if @annotate_ignore.nil?
82-
filename = Rails.root.join(".annotate_ignore")
83-
84-
if File.exist?(filename)
85-
@annotate_ignore = []
86-
87-
File.foreach(filename) do |line|
88-
s = line.strip
89-
90-
if s.blank?
91-
@annotate_ignore << s
92-
end
93-
end
94-
else
95-
@annotate_ignore = false
96-
end
97-
end
98-
99-
if @annotate_ignore.is_a?(Array)
100-
return @annotate_ignore.includes?(klass.name)
101-
end
102-
end
103-
10480
end
10581
end
10682
end

lib/active_record/annotate/configurator.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,27 @@ class Configurator
55
attr_accessor setting
66
alias_method "#{setting}?", setting
77
end
8-
8+
9+
attr_accessor :ignored_models
10+
def annotate_ignore=(models)
11+
if models.is_a?(Array)
12+
@ignored_models = models
13+
else
14+
raise "ActiveRecord::Annotate.config.ignored_models must be an Array of model classes"
15+
end
16+
end
17+
918
def initialize
1019
reset
1120
end
12-
21+
1322
private
23+
1424
def reset
1525
@yard = false
26+
@annotate_ignore = []
1627
end
28+
1729
end
1830
end
1931
end
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
require 'active_record/annotate'
22

3-
ActiveRecord::Annotate.configure do |config|
4-
# set this to true to wrap annotations in triple backticks (```)
5-
# so YARD documentation can process the annotation as a code block
6-
# config.yard = false
7-
end if ActiveRecord.const_defined?(:Annotate)
3+
if defined?(ActiveRecord::Annotate)
4+
ActiveRecord::Annotate.configure do |config|
5+
# # set this to true to wrap annotations in triple backticks (```)
6+
# # so YARD documentation can process the annotation as a code block
7+
# config.yard = false
8+
#
9+
# # Define any models to be skipped by Annotate
10+
# config.ignored_models = [SomeIgnoredModel, AnotherIgnoredModel]
11+
end
12+
end

0 commit comments

Comments
 (0)