Skip to content

Instantly share code, notes, and snippets.

@skunkworker
Last active October 11, 2018 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skunkworker/fe527c7e5ea7c889569314d4a11a365a to your computer and use it in GitHub Desktop.
Save skunkworker/fe527c7e5ea7c889569314d4a11a365a to your computer and use it in GitHub Desktop.
Add custom models to Annotate Gem
Foo:
- interactions/foo/
- interactions/user/bar/
- interactions/baz.rb
Bar:
- interactions/bar/
- interactions/user/bar/
# 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
@skunkworker
Copy link
Author

This allows for custom loading of extra directories, in this case I am using the ActiveInteraction gem and custom load the files to annotate.

@skunkworker
Copy link
Author

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