Skip to content

Commit

Permalink
git-er-done
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Feb 7, 2019
1 parent a58c73f commit 13fe838
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 56 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,29 @@ class Document < ActiveRecord::Base

### Configuration

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

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

```sh
$ rails generate active_record:annotate:install
```

It creates an initializer at `config/initializers/annotate.rb` which contains descriptive comments about all settings (currently just one setting, `yard`).

### Ignoring Certain Models

If you would like to ignore certain models simply create a `.annotate_ignore` file in the root folder of your Rails project
It creates an initializer at `config/initializers/annotate.rb` which contains descriptive comments about each setting.

```ruby
# .annotate_ignore

SomeModel
AnotherIgnoredModel
require 'active_record/annotate'

if defined?(ActiveRecord::Annotate)
ActiveRecord::Annotate.configure do |config|
# # set this to true to wrap annotations in triple backticks (```)
# # so YARD documentation can process the annotation as a code block
# config.yard = false
#
# # Define any models to be skipped by Annotate
# config.ignored_models = [SomeIgnoredModel, AnotherIgnoredModel]
end
end
```

## Changelog
Expand Down
54 changes: 15 additions & 39 deletions lib/active_record/annotate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,98 +9,74 @@ module Annotate
class << self
def annotate
processed_models = []

models.each do |table_name, file_paths_and_classes|
annotation = Dumper.dump(table_name)

file_paths_and_classes.each do |path, klass|
file = File.new(path)
file.annotate_with(annotation.dup, configurator)

if file.changed?
file.write
processed_models << "#{klass} (#{file.relative_path})"
end
end
end

unless processed_models.empty?
puts 'Annotated models:'
processed_models.each do |model|
puts " * #{model}"
end
end
end

def models
files_mask = models_dir.join('**', '*.rb')

hash_with_arrays = Hash.new do |hash, key|
hash[key] = []
end

Dir.glob(files_mask).each_with_object(hash_with_arrays) do |path, models|
short_path = short_path_for(path)
next if short_path.starts_with?('concerns') # skip any app/models/concerns files

klass = class_name_for(short_path)

next unless klass < ActiveRecord::Base # collect only AR::Base descendants
next if klass.respond_to?(:abstract_class?) && klass.abstract_class?
next if annotate_ignore?(klass)
next if configurator.ignored_models.includes?(klass)

models[klass.table_name] << [path, klass]
end
end

# .../app/models/car/hatchback.rb -> car/hatchback
def short_path_for(full_path)
full_path.sub(models_dir.to_s + '/', '').sub(/\.rb$/, '')
end

# car/hatchback -> Car::Hatchback
def class_name_for(short_path)
short_path.camelize.constantize
end

def configure(&block)
configurator.tap(&block)
end

private

def models_dir
Rails.root.join('app/models')
end

def configurator
@configurator ||= Configurator.new
end

def annotate_ignore?(klass)
if @annotate_ignore.nil?
filename = Rails.root.join(".annotate_ignore")

if File.exist?(filename)
@annotate_ignore = []

File.foreach(filename) do |line|
s = line.strip

if s.blank?
@annotate_ignore << s
end
end
else
@annotate_ignore = false
end
end

if @annotate_ignore.is_a?(Array)
return @annotate_ignore.includes?(klass.name)
end
end

end
end
end
16 changes: 14 additions & 2 deletions lib/active_record/annotate/configurator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ class Configurator
attr_accessor setting
alias_method "#{setting}?", setting
end


attr_accessor :ignored_models
def annotate_ignore=(models)
if models.is_a?(Array)
@ignored_models = models
else
raise "ActiveRecord::Annotate.config.ignored_models must be an Array of model classes"
end
end

def initialize
reset
end

private

def reset
@yard = false
@annotate_ignore = []
end

end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
require 'active_record/annotate'

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

0 comments on commit 13fe838

Please sign in to comment.