rails-simple-search 1.1.10 → 1.2.0
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.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/sql_handler.rb +31 -13
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 290634cd836385acaa327ac5b5971e75a716291d51629d8c54dc3c8db728e466
|
4
|
+
data.tar.gz: 9a8c7d6985147d3669c9cf1d98cf41fed6854e1039bdddf7fc271b6b4c4d5f01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 890be5b99ab922d000adad63885a50a4bd678a1294b4915c32db93d808ac89774934626a0b73b3dd117728beb7d6b62a2df15c3b9353fb0113a80a05f11a57fa
|
7
|
+
data.tar.gz: 2e72fd3aef31f0b0b3fdf7b34e80a5f1035cac36b869a62d543d178c5bee132a78caf00605bd1bc93e60e675b52ced76a9fffffb0d22be23acad1e65adf0269b
|
data/README.md
CHANGED
@@ -89,7 +89,7 @@ The following is how we implement this searching function with rails-simple-sear
|
|
89
89
|
<%=f.text_field "address.city" %> <!-- address is an association of model User -->
|
90
90
|
|
91
91
|
<%=f.label "name of any one who commented to my posts" %>
|
92
|
-
<%=f.text_field "posts.comments.
|
92
|
+
<%=f.text_field "posts.comments.user.first_name_or_posts.comments.user.last_name" %>
|
93
93
|
<!-- the associations could go even deeper, isn't it POWERFUL? -->
|
94
94
|
|
95
95
|
<%=f.submit %>
|
@@ -122,7 +122,7 @@ From version 1.1.0 on, we started to support the "or" relation, e.g., we can use
|
|
122
122
|
|
123
123
|
From version 1.1.3 on, we started to support Rails 5.
|
124
124
|
|
125
|
-
For Rails 7, please use version 1.
|
125
|
+
For Rails 7, please use version 1.2.0.
|
126
126
|
|
127
127
|
## License
|
128
128
|
|
data/lib/sql_handler.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module RailsSimpleSearch
|
2
2
|
module SqlHandler
|
3
3
|
def init
|
4
|
-
@
|
4
|
+
@model_table_name = @model_class.table_name
|
5
5
|
@joins = {}
|
6
6
|
end
|
7
7
|
|
@@ -36,7 +36,7 @@ module RailsSimpleSearch
|
|
36
36
|
joins.each do |j|
|
37
37
|
table = j[1]
|
38
38
|
constrain = j[2]
|
39
|
-
@joins_str << " inner join #{table} on #{constrain}"
|
39
|
+
@joins_str << format(" inner join #{table} AS A%02d on #{constrain}", j[0])
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -45,16 +45,17 @@ module RailsSimpleSearch
|
|
45
45
|
@condition_group.set_relation(:and)
|
46
46
|
|
47
47
|
@criteria.each do |key, value|
|
48
|
-
@condition_group.add(
|
48
|
+
@condition_group.add(parse_search_parameters(key, value))
|
49
49
|
end
|
50
50
|
|
51
51
|
make_joins
|
52
52
|
end
|
53
53
|
|
54
|
-
def
|
54
|
+
def build_single_condition(base_class, association_alias, field, value)
|
55
55
|
field, operator = parse_field_name(field)
|
56
56
|
table = base_class.table_name
|
57
57
|
key = "#{table}.#{field}"
|
58
|
+
final_key = "#{association_alias}.#{field}"
|
58
59
|
|
59
60
|
column = base_class.columns_hash[field.to_s]
|
60
61
|
return nil unless column
|
@@ -65,14 +66,18 @@ module RailsSimpleSearch
|
|
65
66
|
verb = 'in'
|
66
67
|
elsif operator
|
67
68
|
verb = operator
|
68
|
-
elsif text_column?(column) && ! @config[:exact_match].include?((@
|
69
|
+
elsif text_column?(column) && ! @config[:exact_match].include?((@model_table_name == table) ? field : key)
|
69
70
|
verb = 'like'
|
70
71
|
value = "%#{value}%"
|
71
72
|
else
|
72
73
|
verb = '='
|
73
74
|
end
|
74
75
|
|
75
|
-
ConditionGroup.new(ConditionItem.new(
|
76
|
+
ConditionGroup.new(ConditionItem.new(final_key, verb, value))
|
77
|
+
end
|
78
|
+
|
79
|
+
def table_name_to_alias(table_name)
|
80
|
+
format('A%02d', @joins[table_name][0])
|
76
81
|
end
|
77
82
|
|
78
83
|
def insert_join(base_class, asso_ref)
|
@@ -84,31 +89,39 @@ module RailsSimpleSearch
|
|
84
89
|
return unless @joins[asso_table].nil?
|
85
90
|
|
86
91
|
@join_count += 1
|
92
|
+
base_table_alias = @join_count < 2 ? base_table : table_name_to_alias(base_table)
|
93
|
+
asso_table_alias = format('A%02d', @join_count)
|
94
|
+
|
87
95
|
if asso_ref.belongs_to?
|
88
|
-
@joins[asso_table] =[@join_count, asso_table, "#{
|
96
|
+
@joins[asso_table] =[@join_count, asso_table, "#{base_table_alias}.#{asso_ref.foreign_key} = #{asso_table_alias}.#{asso_ref.klass.primary_key}"]
|
89
97
|
else
|
90
|
-
join_cond = "#{
|
91
|
-
join_cond = "#{
|
98
|
+
join_cond = "#{base_table_alias}.#{base_class.primary_key} = #{asso_table_alias}.#{asso_ref.foreign_key}"
|
99
|
+
join_cond = "#{asso_table_alias}.#{asso_ref.type} = '#{base_class.name}' and #{join_cond}" if asso_ref.type
|
92
100
|
@joins[asso_table] = [@join_count, asso_table, join_cond]
|
93
101
|
end
|
94
102
|
end
|
95
103
|
|
96
|
-
|
104
|
+
# This method parse a search parameter and its value
|
105
|
+
# then produce a ConditionGroup
|
106
|
+
def parse_search_parameters(attribute, value)
|
107
|
+
# handle _or_ parameters
|
97
108
|
attributes = attribute.split(@config[:or_separator])
|
98
109
|
if attributes.size > 1
|
99
110
|
cg = ConditionGroup.new
|
100
111
|
cg.set_relation(:or)
|
101
112
|
attributes.each do |a|
|
102
|
-
cg.add(
|
113
|
+
cg.add(parse_search_parameters(a, value))
|
103
114
|
end
|
104
115
|
return cg
|
105
116
|
end
|
106
117
|
|
118
|
+
# handle direct fields
|
107
119
|
unless attribute =~ /\./
|
108
|
-
condition =
|
120
|
+
condition = build_single_condition(@model_class, @model_class.table_name, attribute, value)
|
109
121
|
return condition
|
110
122
|
end
|
111
123
|
|
124
|
+
# handle association fields
|
112
125
|
association_fields = attribute.split(/\./)
|
113
126
|
field = association_fields.pop
|
114
127
|
|
@@ -119,10 +132,12 @@ module RailsSimpleSearch
|
|
119
132
|
base_class = association_fields.shift.klass
|
120
133
|
end
|
121
134
|
|
122
|
-
|
135
|
+
association_alias = table_name_to_alias(base_class.table_name)
|
136
|
+
build_single_condition(base_class, association_alias, field, value)
|
123
137
|
end
|
124
138
|
end
|
125
139
|
|
140
|
+
# This class holds a single condition
|
126
141
|
class ConditionItem
|
127
142
|
attr_reader :field, :verb, :value
|
128
143
|
|
@@ -133,6 +148,9 @@ module RailsSimpleSearch
|
|
133
148
|
end
|
134
149
|
end
|
135
150
|
|
151
|
+
# This class holds a ConditionGroup
|
152
|
+
# One ConditionGroup can hold one or more
|
153
|
+
# conditions
|
136
154
|
class ConditionGroup
|
137
155
|
def initialize(item = nil)
|
138
156
|
if item
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-simple-search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yi Zhang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-12-
|
11
|
+
date: 2023-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: rails-simple-search is a light and easy to use gem. It could help developers
|
14
14
|
quickly build a search page.
|