Skip to content

Instantly share code, notes, and snippets.

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

What would you like to do?
# 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
thredded_methods = (Thredded::Engine.routes.url_helpers.methods + Thredded::UrlsHelper.instance_methods)
.select { |s| s.to_s.ends_with?("_path", "_url") }
main_app_delegator = do
.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)} }
::Thredded::ApplicationController.helper main_app_delegator

This comment has been minimized.

Copy link
Owner Author

timdiggins commented Oct 20, 2016

NB: there is a tempting much simpler solution:

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

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


This comment has been minimized.

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