annotate_models icon indicating copy to clipboard operation
annotate_models copied to clipboard

Add annotate.yml with custom directories for Model objects.

Open skunkworker opened this issue 5 years ago • 4 comments

Currently I am using ActiveInteraction to build out service objects for each of my models. As of right now annotate doesn't easily allow for adding more directories with models to be added inside. I've created a gist with a monkey-patch initializer locked on 2.7.4 and a config file config/annotate.yml https://gist.github.com/skunkworker/fe527c7e5ea7c889569314d4a11a365a

Currently in the gist this is how I have it working:

in config/annotate.yml

User:
  - interactions/user/

The User model would be applied to app/interactions/user/*.rb but no subdirectories below.

Is this something that would be beneficial to create a PR for this project? I feel like it could add a lot more flexibility when the naming of the file isn't immediately obvious.

skunkworker avatar Oct 02 '18 21:10 skunkworker

@skunkworker In version 3.0.0 we've added an -–additional-file-patterns flag. Does that suffice for your use case?

Currently waiting on v3.0.0 to get pushed to Rubygems there's been a bit of delay due to @ctran dealing with personal matters. If you would like to use latest development you can point your Gemfile to refer to github for the time being.

drwl avatar Sep 26 '19 04:09 drwl

@drwl I'll take a look and see if it does work for my use case.

skunkworker avatar Sep 26 '19 14:09 skunkworker

I've been looking at the syntax for the flag and it seems to cover the behavior when you have a standardized format, like a app/lib/forms directory.

In the case I am using annotate right now there isn't a way to be more specific, like saying all files under the following folder /app/path/to/folder should be a FooBar model. I believe this is because I'm using modules and lots of service objects to namespace and decouple the behavior as much as possible, with different service objects in different paths using the same database model.

Something like this

Annotate.set_defaults(
  'explicit_model_paths' => [
    "Foobar" => [
      'app/services/foobar/*.rb',
      'app/services/admin/foobar/*.rb'
    ],
    "Foobar::Baz" => [
      'app/services/foobar/baz/*.rb'
    ]
  ],
)

skunkworker avatar Oct 03 '19 05:10 skunkworker

I don't think this is the right path to take, it seems to be too manual for my taste to be in the library.

How about allowing the mapping function to be defined:

def myCustomerMapper(path)
   return ...
end

Annotate.set_defaults(model_mapper: myCustomMapper)
# or
Annotate.set_model_mapper(myCustomMapper)

ctran avatar Oct 03 '19 20:10 ctran