ctran / annotate_models

Annotate Rails classes with schema and routes info
Other
4.41k stars 601 forks source link

not working in Rails 6.1 #845

Open joschl opened 3 years ago

joschl commented 3 years ago

In rails 6.1, routes command moved from rake to rails (rails routes vs. rake routes )

bundle exec rake annotate_routes
rake aborted!
Don't know how to build task 'routes' (See the list of available tasks with `rake --tasks`)

Version

joschl commented 3 years ago

you get the old behaviour with sth like that:

  task :routes do
    require 'rails/commands/routes/routes_command'
    Rails.application.require_environment!
    cmd = Rails::Command::RoutesCommand.new
    cmd.perform
  end
choilive commented 3 years ago

I'm using annotate on 6.1 just fine, although I havn't used the routes annotations. It looks like this issue is affecting just routes.

dcangulo commented 3 years ago

843 Will fix this issue. In my case, I currently used my forked version: https://github.com/dcangulo/annotate_models

prem-prakash commented 3 years ago

Same problem here

tugayac commented 3 years ago

This is the workaround I use:

task routes: :environment do
  puts `bundle exec rails routes`
end
overdrivemachines commented 3 years ago

This is the workaround I use:

task routes: :environment do
  puts `bundle exec rails routes`
end

Where do I put this code?

overdrivemachines commented 3 years ago

When will this be fixed?

tugayac commented 3 years ago

@overdrivemachines You can put it in any rake file you want (probably in routes.rake in this case). Think of it just like any other rake task you'd create.

SystemNinja commented 3 years ago

For those who are confused where to add code that tugayac posted and those that are following Getting Started with Rails - blog app, do following:

  1. Open your project folder
  2. Navigate to lib/tasks
  3. Create file called routes.rake
  4. Paste @tugayac 's workaround
overdrivemachines commented 2 years ago

The annotated routes file has a lot of unwanted routes in my opinion. So came up with a fix. My lib/tasts/routes.rake file looks like:

task routes: :environment do
  puts `bundle exec rails routes | awk '!/active_storage/ && !/action_mailbox/ && !/turbo_/' | sed 's/                           / /'`
end

The routes file gets annotated perfectly. Active Storage, Action Mail and Turbo routes are omitted which makes the annotated routes much cleaner. The awk command removes those unwanted routes. The sed command moves fixes the spacing.

LimeBlast commented 2 years ago

The snippet supplied by @overdrivemachines messes up the output of the annotation in the routes.rb file:

# == Route Map
#
#                           Prefix Verb   URI Pattern         Controller#Action
#                        passwords POST   /passwords(.:format)                          clearance/passwords#create
#                     new_password GET    /passwords/new(.:format)                      clearance/passwords#new
#                          session POST   /session(.:format)  clearance/sessions#create
#               edit_user_password GET    /users/:user_id/password/edit(.:format)       clearance/passwords#edit
#                    user_password PATCH  /users/:user_id/password(.:format)            clearance/passwords#update
#        PUT    /users/:user_id/password(.:format)            clearance/passwords#update
#        POST   /users/:user_id/password(.:format)            clearance/passwords#create
#  users POST   /users(.:format)                              clearance/users#create
#                          sign_in GET    /sign_in(.:format)  clearance/sessions#new
#                         sign_out DELETE /sign_out(.:format) clearance/sessions#destroy
# autocomplete_client_name_clients GET    /clients/autocomplete_client_name(.:format)   clients#autocomplete_client_name
#                      client_jobs POST   /clients/:client_id/jobs(.:format)            jobs#create
#                   new_client_job GET    /clients/:client_id/jobs/new(.:format)        jobs#new
#                client_agreements POST   /clients/:client_id/agreements(.:format)      agreements#create
#             new_client_agreement GET    /clients/:client_id/agreements/new(.:format)  agreements#new
#                          clients GET    /clients(.:format)  clients#index
#        POST   /clients(.:format)                            clients#create
#                       new_client GET    /clients/new(.:format)                        clients#new
#                           client GET    /clients/:id(.:format)                        clients#show
#        PATCH  /clients/:id(.:format)                        clients#update
#        PUT    /clients/:id(.:format)                        clients#update
#               reports_agreements GET    /reports/agreements(.:format)                 reports/agreements#index
#    job GET    /jobs/:id(.:format)                           jobs#show
#        PATCH  /jobs/:id(.:format)                           jobs#update
#        PUT    /jobs/:id(.:format)                           jobs#update
#                  agreement_terms POST   /agreements/:agreement_id/terms(.:format)     terms#create
#               new_agreement_term GET    /agreements/:agreement_id/terms/new(.:format) terms#new
#                        agreement GET    /agreements/:id(.:format)                     agreements#show
#        PATCH  /agreements/:id(.:format)                     agreements#update
#        PUT    /agreements/:id(.:format)                     agreements#update
#   term GET    /terms/:id(.:format)                          terms#show
#        PATCH  /terms/:id(.:format)                          terms#update
#        PUT    /terms/:id(.:format)                          terms#update
#                   support_timers GET    /support_timers(.:format)                     support_timers#index
#                         web_apps GET    /web_apps(.:format) web_apps#index
#                     api_web_apps GET    /api/web_apps(.:format)                       api/web_apps#index
#        POST   /api/web_apps(.:format)                       api/web_apps#create
#   home GET    /home(.:format)                               redirect(301, /)
#   root GET    /                                             high_voltage/pages#show {:id=>"home"}
#   page GET    /pages/*id                                    high_voltage/pages#show

unlike what I get when using @tugayac's code:

# == Route Map
#
#                           Prefix Verb   URI Pattern                                   Controller#Action
#                        passwords POST   /passwords(.:format)                          clearance/passwords#create
#                     new_password GET    /passwords/new(.:format)                      clearance/passwords#new
#                          session POST   /session(.:format)                            clearance/sessions#create
#               edit_user_password GET    /users/:user_id/password/edit(.:format)       clearance/passwords#edit
#                    user_password PATCH  /users/:user_id/password(.:format)            clearance/passwords#update
#                                  PUT    /users/:user_id/password(.:format)            clearance/passwords#update
#                                  POST   /users/:user_id/password(.:format)            clearance/passwords#create
#                            users POST   /users(.:format)                              clearance/users#create
#                          sign_in GET    /sign_in(.:format)                            clearance/sessions#new
#                         sign_out DELETE /sign_out(.:format)                           clearance/sessions#destroy
# autocomplete_client_name_clients GET    /clients/autocomplete_client_name(.:format)   clients#autocomplete_client_name
#                      client_jobs POST   /clients/:client_id/jobs(.:format)            jobs#create
#                   new_client_job GET    /clients/:client_id/jobs/new(.:format)        jobs#new
#                client_agreements POST   /clients/:client_id/agreements(.:format)      agreements#create
#             new_client_agreement GET    /clients/:client_id/agreements/new(.:format)  agreements#new
#                          clients GET    /clients(.:format)                            clients#index
#                                  POST   /clients(.:format)                            clients#create
#                       new_client GET    /clients/new(.:format)                        clients#new
#                           client GET    /clients/:id(.:format)                        clients#show
#                                  PATCH  /clients/:id(.:format)                        clients#update
#                                  PUT    /clients/:id(.:format)                        clients#update
#               reports_agreements GET    /reports/agreements(.:format)                 reports/agreements#index
#                              job GET    /jobs/:id(.:format)                           jobs#show
#                                  PATCH  /jobs/:id(.:format)                           jobs#update
#                                  PUT    /jobs/:id(.:format)                           jobs#update
#                  agreement_terms POST   /agreements/:agreement_id/terms(.:format)     terms#create
#               new_agreement_term GET    /agreements/:agreement_id/terms/new(.:format) terms#new
#                        agreement GET    /agreements/:id(.:format)                     agreements#show
#                                  PATCH  /agreements/:id(.:format)                     agreements#update
#                                  PUT    /agreements/:id(.:format)                     agreements#update
#                             term GET    /terms/:id(.:format)                          terms#show
#                                  PATCH  /terms/:id(.:format)                          terms#update
#                                  PUT    /terms/:id(.:format)                          terms#update
#                   support_timers GET    /support_timers(.:format)                     support_timers#index
#                         web_apps GET    /web_apps(.:format)                           web_apps#index
#                     api_web_apps GET    /api/web_apps(.:format)                       api/web_apps#index
#                                  POST   /api/web_apps(.:format)                       api/web_apps#create
#                             home GET    /home(.:format)                               redirect(301, /)
#                             root GET    /                                             high_voltage/pages#show {:id=>"home"}
#                             page GET    /pages/*id                                    high_voltage/pages#show

I just figured you'd like to know - thank you to both for providing a fix to this issue.

JohnPettigrew commented 2 years ago

The fix from @overdrivemachines works well for me (with the sed command to fix the spacing). I've added /admin/ to the excluded routes because I use ActiveAdmin, which produces a load of its own routes that I don't need to see here! Thanks for the tip.

LucasBrandt commented 2 years ago

The fix from @overdrivemachines mostly worked for me in rails 7, but spacing wasn't quite right even with the sed command. It might be because I don't have many routes currently. If anyone else has the same trouble, using grep instead worked:

task routes: :environment do
  puts `bundle exec rails routes | grep --invert-match --extended-regexp 'turbo\/native\|\/action_mailbox\/\|\/active_storage\/'`
end
austin-wang commented 2 years ago

just use rails routes /NOT/ rake routes its working

Thanks @Nourfaress — that worked perfectly. bundle exec rails routes

overdrivemachines commented 1 year ago

In most cases you only want to show routes that contain your model name and controller name. This takes care of all the spacing without the use of sed or awk:

task routes: :environment do
  Rails.application.eager_load!
  models = ApplicationRecord.descendants.collect(&:name).join("|").downcase
  controllers = ApplicationController.descendants.collect(&:name)
  controllers = (controllers.map { |controller| controller[0..-11].downcase }).join("|")
  if models
    puts `bundle exec rails routes -g "#{models}|#{controllers}"`
  else
    puts `bundle exec rails routes -g "#{controllers}"`
  end
end
afittro commented 1 year ago

You could just change annotate_routes\header_generator.rb line 19 from rake routes... to rails routes...

def routes_map(options)
  result = `rails routes`.chomp("\n").split(/\n/, -1)
bingDBdu commented 8 months ago

So it's not still fixed, right? when I upgrade rails to 6.1, it removes all the annotations from my routes.rb

bundle exec annotate --routes                                                                                                                                                                                [2.7.7]
/Users/bdu.wbrq/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/net-protocol-0.2.2/lib/net/protocol.rb:68: warning: already initialized constant Net::ProtocRetryError
/Users/bdu.wbrq/.rbenv/versions/2.7.7/lib/ruby/2.7.0/net/protocol.rb:66: warning: previous definition of ProtocRetryError was here
/Users/bdu.wbrq/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/net-protocol-0.2.2/lib/net/protocol.rb:214: warning: already initialized constant Net::BufferedIO::BUFSIZE
/Users/bdu.wbrq/.rbenv/versions/2.7.7/lib/ruby/2.7.0/net/protocol.rb:206: warning: previous definition of BUFSIZE was here
/Users/bdu.wbrq/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/net-protocol-0.2.2/lib/net/protocol.rb:541: warning: already initialized constant Net::NetPrivate::Socket
/Users/bdu.wbrq/.rbenv/versions/2.7.7/lib/ruby/2.7.0/net/protocol.rb:503: warning: previous definition of Socket was here
DEPRECATION WARNING: Support for Ruby versions < 3.0.0 is deprecated and will be removed from ViewComponent 4.0.0 (ViewComponent 4.0 will remove support for Ruby versions < 3.0.0 ) (called from <main> at /Users/bdu.wbrq/workespace/codes/booster-api/config/environment.rb:7)
WARNING: Active Record does not support composite primary key.

public.booster_smslite_mappings has composite primary key. Composite primary key is ignored.
WARNING: Active Record does not support composite primary key.

public.booster_smslite_opp_competitors has composite primary key. Composite primary key is ignored.
Annotated (45): spec/models/project_spec.rb, spec/models/feed_comment_spec.rb, spec/factories/feed_comments.rb, spec/models/travel_log_notification_spec.rb, spec/factories/travel_log_notifications.rb, spec/models/user_project_list_spec.rb, spec/models/sms_opportunity_spec.rb, spec/factories/sms_opportunities.rb, spec/models/user_project_watch_spec.rb, spec/factories/user_project_watches.rb, spec/models/group_spec.rb, spec/factories/groups.rb, spec/models/feed_like_spec.rb, spec/factories/feed_likes.rb, spec/models/project_list_spec.rb, spec/factories/project_lists.rb, spec/models/cached/pingboard_profile_spec.rb, spec/models/universal_field_spec.rb, spec/factories/universal_fields.rb, spec/models/universal_comment_spec.rb, spec/factories/universal_comments.rb, spec/models/member_attr_spec.rb, spec/factories/member_attrs.rb, spec/models/followup_spec.rb, spec/factories/followups.rb, spec/models/yue/score_spec.rb, spec/factories/yue/scores.rb, spec/factories/yue_projects.rb, spec/models/feed_spec.rb, spec/factories/feeds.rb, spec/models/role_spec.rb, spec/models/rating_spec.rb, spec/factories/ratings.rb, spec/models/users_group_spec.rb, spec/factories/users_groups.rb, spec/models/project_member_spec.rb, spec/models/cortellis_target_spec.rb, spec/factories/cortellis_targets.rb, spec/models/project_score_spec.rb, spec/factories/project_scores.rb, spec/models/project_member_comment_spec.rb, spec/factories/project_member_comments.rb, spec/factories/member_focuses.rb, spec/models/user_spec.rb, spec/factories/users.rb
rake aborted!
Don't know how to build task 'routes' (See the list of available tasks with `rake --tasks`)

(See full trace by running task with --trace)
config/routes.rb was annotated.
git diff config/routes.rb                                                                                                                                                                                        
diff --git i/config/routes.rb w/config/routes.rb
index 3f56f30..9e93818 100644
--- i/config/routes.rb
+++ w/config/routes.rb
@@ -2,461 +2,6 @@

 # == Route Map
 #
-#                                            Prefix Verb   URI Pattern                                                             Controller#Action
-#                                              root GET    /                                                                       redirect(301, https://www.hongshan.com/)
-#                                      healthz_hook GET    /healthz/hook(.:format)                                                 healthzs#hook
-#                              healthz_status_check GET    /healthz/status_check(.:format)                                         healthzs#status_check
-#                                       sidekiq_web        /ada/sidekiq                                                            Sidekiq::Web
-#                                           healthz GET    /healthz(.:format)                                                      api/healthzs#show {:format=>"json"}
-#                                              meta GET    /meta(.:format)                                                         api/meta#show {:format=>"json"}
-#                                         ai_search GET    /ai/search(.:format)                                                    api/ai_search#search {:format=>"json"}
-#                                        ai_revenue GET    /ai/revenue(.:format)                                                   api/ai_search#revenue {:format=>"json"}
-#                                          users_me GET    /users/me(.:format)                                                     api/users#show_me {:format=>"json"}
-#                               users_me_watch_list GET    /users/me/watch_list(.:format)                                          api/users#watch_project {:format=>"json"}
-#                             users_me_projectLists GET    /users/me/projectLists(.:format)                                        api/project_lists#index_my_project_list {:format=>"json"}
-#                     users_all_watch_projects_news GET    /users/all_watch_projects_news(.:format)                                api/users#watch_projects_news {:format=>"json"}
-#                                             users GET    /users(.:format)                                                        api/users#index {:format=>"json"}
-#                                              user GET    /users/:id(.:format)                                                    api/users#show {:format=>"json"}
-#                                 feeds_hot_hashtag GET    /feeds/hot_hashtag(.:format)                                            api/feeds#hot_hashtag {:format=>"json"}
-#                                  feeds_interested GET    /feeds/interested(.:format)                                             api/feeds#my_interest_feeds {:format=>"json"}
-#                                   feeds_commented GET    /feeds/commented(.:format)                                              api/feeds#my_comment_feeds {:format=>"json"}
-#                                                   GET    /feeds/:id/oppotunity(.:format)                                         api/feeds#relate_project_opportunity {:format=>"json"}
-#                                         feed_like DELETE /feeds/:feed_id/like(.:format)                                          api/likes#destroy {:format=>"json"}
-#                                                   POST   /feeds/:feed_id/like(.:format)                                          api/likes#create {:format=>"json"}
-#                                      feed_comment DELETE /feeds/:feed_id/comment(.:format)                                       api/comments#destroy {:format=>"json"}
-#                                                   POST   /feeds/:feed_id/comment(.:format)                                       api/comments#create {:format=>"json"}
-#                                             feeds GET    /feeds(.:format)                                                        api/feeds#index {:format=>"json"}
-#                                                   POST   /feeds(.:format)                                                        api/feeds#create {:format=>"json"}
-#                                              feed GET    /feeds/:id(.:format)                                                    api/feeds#show {:format=>"json"}
-#                                                   PATCH  /feeds/:id(.:format)                                                    api/feeds#update {:format=>"json"}
-#                                                   PUT    /feeds/:id(.:format)                                                    api/feeds#update {:format=>"json"}
-#                                                   DELETE /feeds/:id(.:format)                                                    api/feeds#destroy {:format=>"json"}
-#                                          finances GET    /finances(.:format)                                                     api/finances#index {:format=>"json"}
-#                            finances_organizations GET    /finances/organizations(.:format)                                       api/finances#org_index {:format=>"json"}
-#                           finances_important_orgs GET    /finances/important_orgs(.:format)                                      api/finances#org_report {:format=>"json"}
-#                             finances_growth_deals POST   /finances/growth_deals(.:format)                                        api/finances#create_growth_deal {:format=>"json"}
-#                        finances_growth_deals_date GET    /finances/growth_deals/date(.:format)                                   api/finances#growth_deal_date {:format=>"json"}
-#                                                   GET    /finances/growth_deals(.:format)                                        api/finances#growth_deal_index {:format=>"json"}
-#                 finances_growth_deals_clear_cache DELETE /finances/growth_deals/clear_cache(.:format)                            api/finances#clear_cache {:format=>"json"}
-#                                    projects_watch GET    /projects/watch(.:format)                                               api/projects#watch_list {:format=>"json"}
-#                            project_project_scores GET    /projects/:project_id/scores(.:format)                                  api/project_scores#index {:format=>"json"}
-#                                                   POST   /projects/:project_id/scores(.:format)                                  api/project_scores#create {:format=>"json"}
-#                                     project_feeds GET    /projects/:project_id/feeds(.:format)                                   api/feeds#index {:format=>"json"}
-#                           project_project_members POST   /projects/:project_id/members(.:format)                                 api/project_members#create {:format=>"json"}
-#                            project_project_member DELETE /projects/:project_id/members/:id(.:format)                             api/project_members#destroy {:format=>"json"}
-#                                          projects GET    /projects(.:format)                                                     api/projects#index {:format=>"json"}
-#                                                   POST   /projects(.:format)                                                     api/projects#create {:format=>"json"}
-#                                           project GET    /projects/:id(.:format)                                                 api/projects#show {:format=>"json"}
-#                                                   PATCH  /projects/:id(.:format)                                                 api/projects#update {:format=>"json"}
-#                                                   PUT    /projects/:id(.:format)                                                 api/projects#update {:format=>"json"}
-#                                                   DELETE /projects/:id(.:format)                                                 api/projects#destroy {:format=>"json"}
-#                                  projects_advance GET    /projects_advance(.:format)                                             api/projects#index_advance {:format=>"json"}
-#                                   projects_search GET    /projects_search(.:format)                                              api/projects#index_search {:format=>"json"}
-#                               projects_pure_index POST   /projects_pure_index(.:format)                                          api/projects#pure_name_index {:format=>"json"}
-#                                                   POST   /projects/:project_id/ratings(.:format)                                 api/projects#create_rating {:format=>"json"}
-#                                                   GET    /projects/:project_id/ratings(.:format)                                 api/ratings#index {:format=>"json"}
-#                                                   GET    /projects/:id/watch(.:format)                                           api/projects#is_watched {:format=>"json"}
-#                                                   GET    /projects/:id/competitors_index(.:format)                               api/projects#competitors_index {:format=>"json"}
-#                                                   GET    /projects/:project_id/lite_talents(.:format)                            api/project_members#index_for_lite {:format=>"json"}
-#                                                   POST   /projects/:id/watch(.:format)                                           api/projects#watch {:format=>"json"}
-#                                                   DELETE /projects/:id/watch(.:format)                                           api/projects#unwatch {:format=>"json"}
-#                                 projects_has_feed POST   /projects/has_feed(.:format)                                            api/projects#has_feed {:format=>"json"}
-#                                      project_tags GET    /project_tags(.:format)                                                 api/project_tags#index {:format=>"json"}
-#                          project_tags_tree_search GET    /project_tags/tree_search(.:format)                                     api/project_tags#search {:format=>"json"}
-#                                                   GET    /projects/:id/shareholders(.:format)                                    api/entities#show_shareholders {:format=>"json"}
-#                              applications_qm_apps GET    /applications/qm_apps(.:format)                                         api/apps#qm_apps {:format=>"json"}
-#                         applications_oversea_apps GET    /applications/oversea_apps(.:format)                                    api/apps#oversea_apps {:format=>"json"}
-#                         applications_qm_apps_date GET    /applications/qm_apps/date(.:format)                                    api/apps#qm_apps_date {:format=>"json"}
afittro commented 7 months ago

I always just copy this rake task to any rails project:

./lib/tasks/fix_annotate_routes.rake:

desc "Monkey patch the annotate gem's routes option"
task :fix_annotate_routes do

  # old = result = %$`rake routes`.chomp("\n").split(/\n/, -1)$
  # new = result = %$`rails routes`.chomp("\n").split(/\n/, -1)$

  file_name = Gem.loaded_specs['annotate'].full_gem_path + '/lib/annotate/annotate_routes/header_generator.rb'
  puts 'patching '+file_name+' to work with rails'

  data = File.read(file_name)

  text = data.gsub('rake routes', 'rails routes')

  data = File.open(file_name, 'w') { |f| f.write(text) }
end

And then I just call from a shell like any other rails tasks rails fix_annotate_routes.

From then on I can call annotate from the shell, with all the available options I want.