Skip to content

Commit

Permalink
Fix table name for namespaced and inherited models #70 #67 (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkamel authored Jul 14, 2022
1 parent 3948193 commit 4362d3d
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/search_cop/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/search_cop/search_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/search_cop/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SearchCop
VERSION = "1.2.2"
VERSION = "1.2.3"
end
23 changes: 0 additions & 23 deletions test/namespace_test.rb

This file was deleted.

20 changes: 20 additions & 0 deletions test/search_cop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")])

Expand Down
16 changes: 13 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"

Expand All @@ -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
Expand Down

0 comments on commit 4362d3d

Please sign in to comment.