Skip to content

Instantly share code, notes, and snippets.

@timdiggins timdiggins/thredded.rb
Last active Jan 10, 2017

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Oct 20, 2016

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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.