Skip to content

Commit

Permalink
Fix for namespaced models #70 (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkamel authored Jul 14, 2022
1 parent c879685 commit 3948193
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 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.2:

* Fix table name for namespaced models #70

Version 1.2.1:

* Fix use of `table_name` to work with inherited models
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.name.tableize, column]
table, attribute = column.to_s =~ /\./ ? column.to_s.split(".") : [model.table_name, 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.1"
VERSION = "1.2.2"
end
23 changes: 23 additions & 0 deletions test/namespace_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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
13 changes: 13 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ class Product < ActiveRecord::Base
belongs_to :user
end

module SomeNamespace
class Product < ActiveRecord::Base
include SearchCop

belongs_to :user

search_scope :search do
attributes :title, :description
attributes user: ["user.username"]
end
end
end

class AvailableProduct < Product
default_scope { where(available: true) }
end
Expand Down

0 comments on commit 3948193

Please sign in to comment.