【问题标题】:How can I override a gem file in order to access it from my engine?如何覆盖 gem 文件以便从我的引擎访问它?
【发布时间】:2017-06-16 09:20:39
【问题描述】:

我在 Rails 引擎上使用 Devise,我想覆盖一个特定文件,即:lib/devise/controller/helpers.rb

我尝试将其放入myengine/lib/devise/controller/helpers.rb,但似乎不起作用。

如何覆盖该文件以便从我的引擎进行访问?如果我能在普通的 Rails 应用程序上也能做到,那就太好了(我可以自己解决剩下的问题)

目的是调试那段代码。见this issue

【问题讨论】:

    标签: ruby-on-rails overriding rails-engines


    【解决方案1】:

    我通常将它们放入初始化程序中,以便我明确知道我猴子修补了它们:

    覆盖signed_in_root_path方法的示例:

    # initializers/devise_controllers_helpers_patch.rb
    
    module Devise
      module Controllers
        module Helpers
          def signed_in_root_path(resource_or_scope)
            puts 'THIS IS MY OWN CODE'
            puts 1 + 1 == 2
    
            scope = Devise::Mapping.find_scope!(resource_or_scope)
            router_name = Devise.mappings[scope].router_name
    
            home_path = "#{scope}_root_path"
    
            context = router_name ? send(router_name) : self
    
            if context.respond_to?(home_path, true)
              context.send(home_path)
            elsif context.respond_to?(:root_path)
              context.root_path
            elsif respond_to?(:root_path)
              root_path
            else
              "/"
            end
          end
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      相关资源
      最近更新 更多