Delete routes specified in Gem?

Is there a way to remove the routes indicated in stone in Rails 3? The original logger logger defines the routes that I do not want. I need to specify route restrictions as follows:

scope :constraints => {:subdomain => 'secure', :protocol => 'https'} do collection do post :query post :destroy_all get :feed end end 

Based on the Rails Engine docs , I thought I could create a monkey patch and add a routes file without the routes specified for the paths ["config / routes"]. paths Array, but the file is not added to ExceptionLogger :: Engine.paths ["config / routes"]. paths

 File: config/initializers/exception_logger_hacks.rb ExceptionLogger::Engine.paths["config/routes"].paths.unshift(File.expand_path(File.join(File.dirname(__FILE__), "exception_logger_routes.rb"))) 

Am I really here? Maybe there is a better way to do this?

+6
source share
2 answers

You can prevent Rails from loading the routes of a particular gem so that none of the gem routes are added, so you will need to add the ones you want manually:

Add an initializer to application.rb as follows:

 class Application < Rails::Application ... initializer "myinitializer", :after => "add_routing_paths" do |app| app.routes_reloader.paths.delete_if{ |path| path.include?("NAME_OF_GEM_GOES_HERE") } end 
+7
source

Here is one way that worked for me.

It does not “delete” routes, but allows you to control where they match. You probably want each requested route to match something, even if it catches all 404 below.

Your application routes (MyApp / config / routes.rb) will be loaded first (unless you have changed the default boot process). And the routes matching the first will take precedence.

Thus, you can redefine the routes that you want to block, or block them by catching the entire route at the bottom of YourApp / config / routes.rb.

Named routes, unfortunately, seem to follow the ruby ​​rule "last definition wins." Therefore, if the routes are named, and your application or engine uses these names, you need to determine the routes both first (so that yours coincide in the first place) and the last (the so-called route routes, as you expected, and not how the engine defines )

To redefine engine routes after adding them, create a file with the name

 # config/named_routes_overrides.rb Rails.application.routes.draw do # put your named routes here, which you also included in config/routes.rb end # config/application.rb class Application < Rails::Application # ... initializer 'add named route overrides' do |app| app.routes_reloader.paths << File.expand_path('../named_routes_overrides.rb',__FILE__) # this seems to cause these extra routes to be loaded last, so they will define named routes last. end end 

You can test this routing sandwich in the console:

 > Rails.application.routes.url_helpers.my_named_route_path => # before your fix, this will be the engine named route, since it was defined last. > Rails.application.routes.recognize_path("/route/you/want/to/stop/gem/from/controlling") => # before your fix, this will route to the controller and method you defined, rather than what the engine defined, because your route comes first. 

After correction, these calls should coincide.

(I posted this initially in the google gem google group here: https://groups.google.com/forum/?fromgroups#!topic/refinery-cms/N5F-Insm9co )

+2
source

Source: https://habr.com/ru/post/887328/


All Articles