diff --git a/CHANGELOG.md b/CHANGELOG.md index 363e247..e504077 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog +Version 1.2.3: + +* Fix table name for namespaced and inherited models #70 #67 + Version 1.2.2: * Fix table name for namespaced models #70 diff --git a/lib/search_cop/query_builder.rb b/lib/search_cop/query_builder.rb index d098bdd..d340dcb 100644 --- a/lib/search_cop/query_builder.rb +++ b/lib/search_cop/query_builder.rb @@ -12,7 +12,7 @@ def initialize(model, query, scope, query_options) end def associations - all_associations - [query_info.model.table_name.to_sym] + all_associations - [query_info.model.name.tableize.to_sym] - [query_info.model.table_name.to_sym] end private diff --git a/lib/search_cop/search_scope.rb b/lib/search_cop/search_scope.rb index 62cd120..fbf06c8 100644 --- a/lib/search_cop/search_scope.rb +++ b/lib/search_cop/search_scope.rb @@ -55,7 +55,7 @@ def generator(name, &block) def attributes_hash(hash) hash.each do |key, value| reflection.attributes[key.to_s] = Array(value).collect do |column| - table, attribute = column.to_s =~ /\./ ? column.to_s.split(".") : [model.table_name, column] + table, attribute = column.to_s =~ /\./ ? column.to_s.split(".") : [model.name.tableize, column] "#{table}.#{attribute}" end diff --git a/lib/search_cop/version.rb b/lib/search_cop/version.rb index 9333f92..a36055e 100644 --- a/lib/search_cop/version.rb +++ b/lib/search_cop/version.rb @@ -1,3 +1,3 @@ module SearchCop - VERSION = "1.2.2" + VERSION = "1.2.3" end diff --git a/test/namespace_test.rb b/test/namespace_test.rb deleted file mode 100644 index a528c4c..0000000 --- a/test/namespace_test.rb +++ /dev/null @@ -1,23 +0,0 @@ -require File.expand_path("test_helper", __dir__) - -class NamespaceTest < SearchCop::TestCase - def test_model_namespace - expected = create(:product, title: "Expected") - rejected = create(:product, title: "Rejected") - - results = SomeNamespace::Product.search("Expected") - - assert_includes results.map(&:id), expected.id - refute_includes results.map(&:id), rejected.id - end - - def test_model_namespace_with_associations - expected = create(:product, user: create(:user, username: "Expected")) - rejected = create(:product, user: create(:user, username: "Rejected")) - - results = SomeNamespace::Product.search("user:Expected") - - assert_includes results.map(&:id), expected.id - refute_includes results.map(&:id), rejected.id - end -end diff --git a/test/search_cop_test.rb b/test/search_cop_test.rb index ddbee07..524d740 100644 --- a/test/search_cop_test.rb +++ b/test/search_cop_test.rb @@ -70,6 +70,26 @@ def test_inherited_model refute_includes results, rejected end + def test_namespaced_model + expected = create(:blog_post, title: "Expected") + rejected = create(:blog_post, title: "Rejected") + + results = Blog::Post.search("Expected") + + assert_includes results, expected + refute_includes results, rejected + end + + def test_namespaced_model_with_associations + expected = create(:blog_post, user: create(:user, username: "Expected")) + rejected = create(:blog_post, user: create(:user, username: "Rejected")) + + results = Blog::Post.search("user:Expected") + + assert_includes results, expected + refute_includes results, rejected + end + def test_multiple product = create(:product, comments: [create(:comment, title: "Title", message: "Message")]) diff --git a/test/test_helper.rb b/test/test_helper.rb index 1d4d257..c19ccce 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -77,14 +77,14 @@ class Product < ActiveRecord::Base belongs_to :user end -module SomeNamespace - class Product < ActiveRecord::Base +module Blog + class Post < ActiveRecord::Base include SearchCop belongs_to :user search_scope :search do - attributes :title, :description + attributes :title, :content attributes user: ["user.username"] end end @@ -98,6 +98,9 @@ class AvailableProduct < Product factory :product do end + factory :blog_post, class: Blog::Post do + end + factory :available_product do available { true } end @@ -110,6 +113,7 @@ class AvailableProduct < Product end ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS products" +ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS posts" ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS comments" ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS users" @@ -126,6 +130,12 @@ class AvailableProduct < Product t.string :notice end +ActiveRecord::Base.connection.create_table :posts do |t| + t.references :user + t.string :title + t.text :content +end + ActiveRecord::Base.connection.create_table :comments do |t| t.references :product t.references :user