search_cop 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +15 -0
- data/README.md +6 -3
- data/lib/search_cop/query_builder.rb +10 -1
- data/lib/search_cop/search_scope.rb +1 -1
- data/lib/search_cop/version.rb +1 -1
- data/lib/search_cop_grammar/attributes.rb +7 -3
- data/test/scope_test.rb +24 -4
- data/test/test_helper.rb +3 -1
- metadata +2 -2
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
|
2
2
|
# Changelog
|
3
3
|
|
4
|
+
Version 1.0.4:
|
5
|
+
|
6
|
+
* Fix for Rails 4.2 regression regarding reflection access
|
7
|
+
|
8
|
+
Version 1.0.3:
|
9
|
+
|
10
|
+
* Supporting Rails 4.2
|
11
|
+
* Dropped Arel dependencies
|
12
|
+
|
13
|
+
Version 1.0.2:
|
14
|
+
|
15
|
+
* Avoid eager loading when no associations referenced
|
16
|
+
* Prefer objects over class names
|
17
|
+
* Readme extended
|
18
|
+
|
4
19
|
Version 1.0.1:
|
5
20
|
|
6
21
|
* Inheritance fix
|
data/README.md
CHANGED
@@ -393,7 +393,7 @@ class Book < ActiveRecord::Base
|
|
393
393
|
belongs_to :user
|
394
394
|
|
395
395
|
search_scope :search do
|
396
|
-
attributes :user => ["
|
396
|
+
attributes :user => ["user.username", "users_books.username"]
|
397
397
|
end
|
398
398
|
|
399
399
|
# ...
|
@@ -412,14 +412,17 @@ class Book < ActiveRecord::Base
|
|
412
412
|
search_scope :search do
|
413
413
|
# ...
|
414
414
|
|
415
|
-
aliases :users_books =>
|
415
|
+
aliases :users_books => :users
|
416
416
|
end
|
417
417
|
|
418
418
|
# ...
|
419
419
|
end
|
420
420
|
```
|
421
421
|
|
422
|
-
to tell SearchCop about the custom SQL alias and mapping.
|
422
|
+
to tell SearchCop about the custom SQL alias and mapping. In addition, you can
|
423
|
+
always do the joins yourself via a `scope {}` block plus `aliases` and use your
|
424
|
+
own custom sql aliases to become independent of names auto-assigned by
|
425
|
+
ActiveRecord.
|
423
426
|
|
424
427
|
## Supported operators
|
425
428
|
|
@@ -19,7 +19,16 @@ module SearchCop
|
|
19
19
|
private
|
20
20
|
|
21
21
|
def all_associations
|
22
|
-
scope.reflection.attributes.values.flatten.
|
22
|
+
scope.reflection.attributes.values.flatten.collect { |column| association_for column.split(".").first }.uniq
|
23
|
+
end
|
24
|
+
|
25
|
+
def association_for(column)
|
26
|
+
alias_value = scope.reflection.aliases[column]
|
27
|
+
|
28
|
+
association = alias_value.respond_to?(:table_name) ? alias_value.table_name : alias_value
|
29
|
+
association ||= column
|
30
|
+
|
31
|
+
association.to_sym
|
23
32
|
end
|
24
33
|
end
|
25
34
|
end
|
data/lib/search_cop/version.rb
CHANGED
@@ -58,16 +58,20 @@ module SearchCopGrammar
|
|
58
58
|
def klass_for_association(name)
|
59
59
|
reflections = query_info.model.reflections
|
60
60
|
|
61
|
+
return reflections[name].klass if reflections[name]
|
61
62
|
return reflections[name.to_sym].klass if reflections[name.to_sym]
|
62
63
|
|
63
64
|
nil
|
64
65
|
end
|
65
66
|
|
66
67
|
def klass_for(name)
|
67
|
-
|
68
|
-
mapped_name ||= name
|
68
|
+
alias_value = query_info.scope.reflection.aliases[name]
|
69
69
|
|
70
|
-
|
70
|
+
return alias_value if alias_value.is_a?(Class)
|
71
|
+
|
72
|
+
value = alias_value || name
|
73
|
+
|
74
|
+
klass_for_association(value) || value.classify.constantize
|
71
75
|
end
|
72
76
|
|
73
77
|
def alias_for(name)
|
data/test/scope_test.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
require File.expand_path("../test_helper", __FILE__)
|
3
3
|
|
4
4
|
class ScopeTest < SearchCop::TestCase
|
5
|
-
def
|
5
|
+
def test_scope_name
|
6
6
|
expected = create(:product, :title => "Expected")
|
7
7
|
rejected = create(:product, :notice => "Expected")
|
8
8
|
|
@@ -22,14 +22,34 @@ class ScopeTest < SearchCop::TestCase
|
|
22
22
|
refute_includes results, rejected
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
26
|
-
expected = create(:product, :
|
27
|
-
rejected = create(:product, :
|
25
|
+
def test_custom_scope
|
26
|
+
expected = create(:product, :user => create(:user, :username => "Expected"))
|
27
|
+
rejected = create(:product, :user => create(:user, :username => "Rejected"))
|
28
|
+
|
29
|
+
results = Product.user_search("user: Expected")
|
30
|
+
|
31
|
+
assert_includes results, expected
|
32
|
+
refute_includes results, rejected
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_aliases_with_association
|
36
|
+
expected = create(:product, :user => create(:user, :username => "Expected"))
|
37
|
+
rejected = create(:product, :user => create(:user, :username => "Rejected"))
|
28
38
|
|
29
39
|
results = Product.search("user: Expected")
|
30
40
|
|
31
41
|
assert_includes results, expected
|
32
42
|
refute_includes results, rejected
|
33
43
|
end
|
44
|
+
|
45
|
+
def test_aliases_with_model
|
46
|
+
expected = create(:product, :user => create(:user, :username => "Expected"))
|
47
|
+
rejected = create(:product, :user => create(:user, :username => "Rejected"))
|
48
|
+
|
49
|
+
results = Product.user_search("user: Expected")
|
50
|
+
|
51
|
+
assert_includes results, expected
|
52
|
+
refute_includes results, rejected
|
53
|
+
end
|
34
54
|
end
|
35
55
|
|
data/test/test_helper.rb
CHANGED
@@ -55,11 +55,13 @@ class Product < ActiveRecord::Base
|
|
55
55
|
end
|
56
56
|
|
57
57
|
search_scope :user_search do
|
58
|
+
scope { joins "LEFT OUTER JOIN users users_products ON users_products.id = products.user_id" }
|
59
|
+
|
58
60
|
attributes :title, :description
|
59
61
|
attributes :user => "users_products.username"
|
60
62
|
|
61
63
|
options :title, :default => true
|
62
|
-
aliases :users_products =>
|
64
|
+
aliases :users_products => User
|
63
65
|
end
|
64
66
|
|
65
67
|
has_many :comments
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: search_cop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-12-
|
12
|
+
date: 2014-12-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: treetop
|