Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix url helpers on mountable engines #191

Closed
wants to merge 1 commit into from

7 participants

@juniorz

Original pull request: #186 by @marcelloma

Changed to trust on a public API instead a obscure method (_railtie) and verifies if the engine has an isolated namespace.

@josevalim josevalim commented on the diff
lib/inherited_resources/class_methods.rb
@@ -338,6 +338,16 @@ def initialize_resources_class_accessors! #:nodoc:
# Deal with namespaced controllers
namespaces = self.controller_path.split('/')[0..-2]
+
+ # Remove namespace if its a mountable engine
+ namespaces.delete_if do |namespace|
@josevalim Owner

I believe the best way to achieve this is to simply do it as Rails:

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/naming.rb#L91

Something like this:

namespaces = self.parents
self.parents.delete_if do |n|
    n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
end
namespaces.map!(&:underscore)

It will work only on 3.2 though but I believe this is fine.

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

or +1 on this one :)

@prikha

Please post any temporary solution for this unless there no way out...

@juniorz

That is what I made: juniorz@9b51bc3

But, it looks like this gem is "deprecated" and will no longer be updated...

@panyamin

+1 this fix is very needed.

Works on mountable engine (rails 4.0.0)

@dbalexandre dbalexandre referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
@rafaelfranca rafaelfranca removed the Bug label
@rafaelfranca
Collaborator

This need a rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 27, 2011
  1. @juniorz
This page is out of date. Refresh to see the latest.
View
10 lib/inherited_resources/class_methods.rb
@@ -338,6 +338,16 @@ def initialize_resources_class_accessors! #:nodoc:
# Deal with namespaced controllers
namespaces = self.controller_path.split('/')[0..-2]
+
+ # Remove namespace if its a mountable engine
+ namespaces.delete_if do |namespace|
@josevalim Owner

I believe the best way to achieve this is to simply do it as Rails:

https://github.com/rails/rails/blob/master/activemodel/lib/active_model/naming.rb#L91

Something like this:

namespaces = self.parents
self.parents.delete_if do |n|
    n.respond_to?(:use_relative_model_naming?) && n.use_relative_model_naming?
end
namespaces.map!(&:underscore)

It will work only on 3.2 though but I believe this is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ begin
+ "#{namespace}/engine".camelize.constantize.isolated?
+ rescue
+ false
+ end
+ end
+
config[:route_prefix] = namespaces.join('_') unless namespaces.empty?
# Deal with default request parameters in namespaced controllers, e.g.
View
23 test/class_methods_test.rb
@@ -23,6 +23,18 @@ class DeansController < InheritedResources::Base
belongs_to :school
end
+module MyEngine
+ class Engine < Rails::Engine
+ isolate_namespace MyEngine
+ end
+
+ class PeopleController < InheritedResources::Base; end
+end
+
+module MyNamespace
+ class PeopleController < InheritedResources::Base; end
+end
+
class ActionsClassMethodTest < ActionController::TestCase
tests BooksController
@@ -132,3 +144,14 @@ def test_url_helpers_are_recreated_just_once_when_belongs_to_is_called_with_mult
DeansController.send(:parents_symbols=, [:school])
end
end
+
+class MountableEngineTest < ActiveSupport::TestCase
+ def test_route_prefix_do_not_include_engine_name
+ puts MyEngine::PeopleController.send(:resources_configuration)[:self][:route_prefix]
+ assert_nil MyEngine::PeopleController.send(:resources_configuration)[:self][:route_prefix]
+ end
+
+ def test_route_prefix_present_when_parent_module_is_not_a_engine
+ assert_equal 'my_namespace', MyNamespace::PeopleController.send(:resources_configuration)[:self][:route_prefix]
+ end
+end
Something went wrong with that request. Please try again.