Skip to content
Browse files

[ACTIVEMODEL][FIX] Fix the incorrect translation of namespaced models…

… into `document_type`

When loading the namespaced models from ES, the exception: "NameError: uninitialized constant" was raised.

This is due to the fact that ActiveModel does, for reasons unknown, funky shit here: <https://github.com/rails/rails/blob/v3.1.3/activemodel/lib/active_model/naming.rb#L50-52>

Closes #189.
  • Loading branch information...
1 parent 3925c24 commit 89bc6639e6c92ebf1decc1e934c894df5053164e @ddnexus ddnexus committed with karmi Dec 28, 2011
Showing with 16 additions and 1 deletion.
  1. +1 −1 lib/tire/model/naming.rb
  2. +8 −0 test/models/namespaced_model.rb
  3. +7 −0 test/unit/model_search_test.rb
View
2 lib/tire/model/naming.rb
@@ -74,7 +74,7 @@ def index_prefix(*args)
#
def document_type name=nil
@document_type = name if name
- @document_type || klass.model_name.singular
+ @document_type || klass.model_name.underscore
end
end
View
8 test/models/namespaced_model.rb
@@ -0,0 +1,8 @@
+# Example ActiveModel class with custom document type
+
+require File.expand_path('../active_model_article', __FILE__)
+
+module MyNamespace
+ class ModelInNamespace < ActiveModelArticle
+ end
+end
View
7 test/unit/model_search_test.rb
@@ -100,6 +100,13 @@ class SearchTest < Test::Unit::TestCase
end
end
+ should "properly translate namespaced model into document_type" do
+ # Watch out for <https://github.com/rails/rails/blob/v3.1.3/activemodel/lib/active_model/naming.rb#L50-52>
+ t = MyNamespace::ModelInNamespace.document_type
+ assert_equal 'my_namespace/model_in_namespace', t
+ assert defined?(t), "Cannot infer class from document type: #{t}"
+ end
+
should "allow to refresh index" do
Index.any_instance.expects(:refresh)

0 comments on commit 89bc663

Please sign in to comment.
Something went wrong with that request. Please try again.