Skip to content

Instantly share code, notes, and snippets.

@timdiggins
Last active January 10, 2017 18:45
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 timdiggins/bf6d09b28828a392198562c93554ad07 to your computer and use it in GitHub Desktop.
Save timdiggins/bf6d09b28828a392198562c93554ad07 to your computer and use it in GitHub Desktop.
MainAppDelegator
# config/intitializers/thredded.rb
# to allow you to not to have to add `main_app` before every path helper
# when embedding Thredded within a main-app supplied layout (with navbar and links to the main_app)
# ... Add this to the bottom of the initializer
Rails.application.config.to_prepare do
Rails.application.reload_routes!
thredded_methods = (Thredded::Engine.routes.url_helpers.methods + Thredded::UrlsHelper.instance_methods)
.select { |s| s.to_s.ends_with?("_path", "_url") }
main_app_delegator = Module.new do
Rails.application.routes.url_helpers.methods
.select { |m| m.to_s.ends_with?('_path', '_url') }
.reject { |m| thredded_path_methods.include?(m).tap { |r| Rails.logger.warn "ignoring conflict: #{m}" if r } }
.each{ |method_name| send(:define_method, method_name) {|*args| main_app.send(method_name, *args)} }
end
::Thredded::ApplicationController.helper main_app_delegator
end
@timdiggins
Copy link
Author

NB: there is a tempting much simpler solution:

Rails.application.config.to_prepare do
  Rails.application.reload_routes!
  ::Thredded::ApplicationController.helper Rails.application.routes.url_helpers
end

Unfortunately this would define (and redefine) more than just the path and url helpers and would break existing routes in the Engine.

@krio
Copy link

krio commented Jan 10, 2017

There's an error here. thredded_path_methods doesn't exist it should be thredded_methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment