Last active
October 11, 2018 18:08
-
-
Save skunkworker/fe527c7e5ea7c889569314d4a11a365a to your computer and use it in GitHub Desktop.
Add custom models to Annotate Gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Foo: | |
- interactions/foo/ | |
- interactions/user/bar/ | |
- interactions/baz.rb | |
Bar: | |
- interactions/bar/ | |
- interactions/user/bar/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For 'annotate' gem version 2.7.4 | |
module AnnotateModels | |
class << self | |
def annotate(klass, file, header, options = {}) | |
begin | |
klass.reset_column_information | |
info = get_schema_info(klass, header, options) | |
model_name = klass.name.underscore | |
table_name = klass.table_name | |
model_file_name = File.join(file) | |
annotated = [] | |
if annotate_one_file(model_file_name, info, :position_in_class, options_with_position(options, :position_in_class)) | |
annotated << model_file_name | |
end | |
# Begin Custom loading | |
options[:custom_dir] = YAML.load(File.read(Rails.root.join("config","annotate.yml"))) | |
custom_directories = options[:custom_dir][klass.to_s] || [] | |
custom_directories.each do |custom_directory| | |
dir = Rails.root.join("app", custom_directory) | |
if File.file?(dir) | |
found_files = [dir] | |
elsif File.directory?(dir) | |
found_files = Dir[Rails.root.join("app", custom_directory)+"*.rb"] | |
else | |
found_files = [] | |
end | |
found_files.each do |found_file_name| | |
if annotate_one_file(found_file_name, info, :position_in_class, options_with_position(options, :position_in_class)) | |
annotated << found_file_name | |
end | |
end | |
end | |
# End Custom Loading | |
matched_types(options).each do |key| | |
exclusion_key = "exclude_#{key.pluralize}".to_sym | |
position_key = "position_in_#{key}".to_sym | |
# Same options for active_admin models | |
if key == 'admin' | |
exclusion_key = 'exclude_class'.to_sym | |
position_key = 'position_in_class'.to_sym | |
end | |
next if options[exclusion_key] | |
get_patterns(key) | |
.map { |f| resolve_filename(f, model_name, table_name) } | |
.each do |f| | |
if annotate_one_file(f, info, position_key, options_with_position(options, position_key)) | |
annotated << f | |
end | |
end | |
end | |
rescue StandardError => e | |
$stderr.puts "Unable to annotate #{file}: #{e.message}" | |
$stderr.puts "\t" + e.backtrace.join("\n\t") if options[:trace] | |
end | |
annotated | |
end | |
end | |
end |
I added a file/directory check to allow for individual files to be added.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This allows for custom loading of extra directories, in this case I am using the ActiveInteraction gem and custom load the files to annotate.