diff --git a/README.md b/README.md index a1e33f9..f23abf2 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ 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: @@ -55,7 +55,22 @@ You can generate the basic initializer with a built-in generator: $ 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`). +It creates an initializer at `config/initializers/annotate.rb` which contains descriptive comments about each setting. + +```ruby +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 diff --git a/lib/active_record/annotate.rb b/lib/active_record/annotate.rb index 623513c..5ffedd1 100644 --- a/lib/active_record/annotate.rb +++ b/lib/active_record/annotate.rb @@ -9,21 +9,21 @@ 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| @@ -31,48 +31,52 @@ def annotate 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 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 + end end end diff --git a/lib/active_record/annotate/configurator.rb b/lib/active_record/annotate/configurator.rb index 295198c..12d2c82 100644 --- a/lib/active_record/annotate/configurator.rb +++ b/lib/active_record/annotate/configurator.rb @@ -5,15 +5,27 @@ class Configurator attr_accessor setting alias_method "#{setting}?", setting end - + + attr_accessor :ignored_models + def ignored_models=(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 + @ignored_models = [] end + end end end diff --git a/lib/generators/active_record/annotate/install/templates/initializer.rb b/lib/generators/active_record/annotate/install/templates/initializer.rb index ce9ddb7..16701c9 100644 --- a/lib/generators/active_record/annotate/install/templates/initializer.rb +++ b/lib/generators/active_record/annotate/install/templates/initializer.rb @@ -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 diff --git a/spec/active_record/annotate/configurator_spec.rb b/spec/active_record/annotate/configurator_spec.rb index 09c1d77..48d6c51 100644 --- a/spec/active_record/annotate/configurator_spec.rb +++ b/spec/active_record/annotate/configurator_spec.rb @@ -4,6 +4,7 @@ describe "#initialize" do it "resets all settings to their default values" do expect(subject.yard).to be_falsy + expect(subject.ignored_models).to eq([]) end end @@ -13,6 +14,16 @@ subject.yard = true expect(subject.yard).to be_truthy + + expect(subject.ignored_models).to eq([]) + + subject.ignored_models = [:foobar] + subject.ignored_models.push(:bar) + expect(subject.ignored_models).to eq([:foobar, :bar]) + + expect { + subject.ignored_models = "bad value" + }.to raise_exception(StandardError) end end end diff --git a/spec/active_record/annotate/file_spec.rb b/spec/active_record/annotate/file_spec.rb index 7cfba27..8a66759 100644 --- a/spec/active_record/annotate/file_spec.rb +++ b/spec/active_record/annotate/file_spec.rb @@ -52,6 +52,18 @@ class User < ActiveRecord::Base # t.integer :age, null: false, default: 0 # end +class User < ActiveRecord::Base + has_many :posts +end + FILE + end + + let(:expected_ignored_result) do + <<-FILE +# enCoding: utf-8 +# frozen_string_literal: true +# warn_indent: true + class User < ActiveRecord::Base has_many :posts end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9929083..5a34c88 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,3 +2,7 @@ require 'active_record/annotate' require 'pry' require 'awesome_print' + +class User < ActiveRecord::Base + ### Define User constant +end