rails-simple-search 1.2.0 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +68 -2
  3. data/lib/sql_handler.rb +5 -3
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 290634cd836385acaa327ac5b5971e75a716291d51629d8c54dc3c8db728e466
4
- data.tar.gz: 9a8c7d6985147d3669c9cf1d98cf41fed6854e1039bdddf7fc271b6b4c4d5f01
3
+ metadata.gz: c6310535f7e545c8b29ca69421df5a8f23efca79dab7ccec6311ed00c632c51d
4
+ data.tar.gz: 5379c314d28a7e06acf4e12ec23d78799e81b89843d1abf2717122e2f90cbbfc
5
5
  SHA512:
6
- metadata.gz: 890be5b99ab922d000adad63885a50a4bd678a1294b4915c32db93d808ac89774934626a0b73b3dd117728beb7d6b62a2df15c3b9353fb0113a80a05f11a57fa
7
- data.tar.gz: 2e72fd3aef31f0b0b3fdf7b34e80a5f1035cac36b869a62d543d178c5bee132a78caf00605bd1bc93e60e675b52ced76a9fffffb0d22be23acad1e65adf0269b
6
+ metadata.gz: 25829545f58e17b7234400719ef2707369cc2a5dbd608d3527c1aafd9a41cf7a5189e4f04857ea263887307faaaa66cce9850c8af024e450fd98095c95ea2989
7
+ data.tar.gz: 47bc9c46c5c56017bc1b6c468e811987f1738e1f38c4379217d77f474c62c640bbf1d9e154d7eaa099905b3337f7819d937718964b441af2c6c87958eb169259
data/README.md CHANGED
@@ -1,10 +1,30 @@
1
+ [![Gem Version](https://badge.fury.io/rb/rails-simple-search.svg)](https://badge.fury.io/rb/rails-simple-search)
2
+
3
+ ### Table of Contents
4
+
5
+ * [What is rails-simple-search?](#what-is-rails-simple-search)
6
+
7
+ * [Why rails-simple-search?](#why-rails-simple-search)
8
+
9
+ * [Installation](#installation)
10
+
11
+ * [Usage](#usage)
12
+
13
+ * [How to construct field names](#how-to-construct-field-names)
14
+
15
+ * [Demo projects](#demo-projects)
16
+
17
+ * [Version history](#version-history)
18
+
19
+ * [License](#license)
20
+
1
21
  ## What is rails-simple-search?
2
22
  rails-simple-search is a Ruby gem. It can help Ruby on Rails developers **quickly**
3
23
  implement searching/filtering function against database. If you're not looking
4
24
  for a full-text searching solution, this plugin will most probably **satisfy all**
5
25
  your searching requirement.
6
26
 
7
- ## Why?
27
+ ## Why rails-simple-search?
8
28
  From time to time, I need to build pages to show a list of narrowed down records
9
29
  from a database table by giving some searching criteria on some columns of the
10
30
  table and/or of some referencing tables. Before I implemented this plugin, I usually
@@ -67,6 +87,8 @@ The following is how we implement this searching function with rails-simple-sear
67
87
  ```
68
88
 
69
89
  4. Code in views:
90
+ **Pay attention to the name of the form fields**.
91
+
70
92
  ```
71
93
  <% form_for @search, url => "/xxxxxx", data: {turbo: false} do |f| %>
72
94
 
@@ -105,6 +127,50 @@ The following is how we implement this searching function with rails-simple-sear
105
127
  post "/xxxxxx" => "yyyyyyy#zzzzzz"
106
128
  ```
107
129
 
130
+ ## How to construct field names
131
+ 1. Let's call the model we're search for is the base model. If you just want to search
132
+ against any direct fields of the base model, we just use the table field name as the html
133
+ input field name. For example
134
+ ```
135
+ "email"
136
+ ```
137
+
138
+ 2. If you need to search against fields of the base model's association, you can just
139
+ use the association name and the table field name with a dot "." connecting them. Like this
140
+ ```
141
+ "address.city"
142
+ ```
143
+
144
+ 3. You can chain the associations more than 2 layers. For example, we're going to
145
+ find out the users whose posts have been commented by someone whose first name we happen to know.
146
+ ```
147
+ "posts.comments.user.first_name"
148
+ ```
149
+
150
+ 4. Sometimes we need to find out something according to a range of time, or a range of numbers,
151
+ we can attach "_greater_than", "_greater_than_equal_to", "_less_than", or "_less_than_equal_to".
152
+ For example, we need to find out the users who birth date is between a range, the field names
153
+ can be like this
154
+ ```
155
+ birth_date_greater_than
156
+ ```
157
+ and
158
+ ```
159
+ birth_date_greater_than
160
+ ```
161
+
162
+ 5. Sometimes we need to express the idea of "or", for example, I know roughly a user's name, but
163
+ not sure if it's her first name or last name, we can do it like this
164
+ ```
165
+ first_name_or_last_name
166
+ ```
167
+
168
+ 6. We can even use the "or" relation with association fields. For example
169
+ ```
170
+ posts.comments.user.first_name_or_posts.comments.user.last_name
171
+ ```
172
+
173
+
108
174
  ## Demo projects
109
175
  There are 2 demo projects for this gem, one for [Rails 5](https://github.com/yzhanginwa/demo_app_for_rails_simple_search)
110
176
  and one for [Rails 7](https://github.com/yzhanginwa/rails_simple_search_demo). You are encouraged to clone them to your local and
@@ -122,7 +188,7 @@ From version 1.1.0 on, we started to support the "or" relation, e.g., we can use
122
188
 
123
189
  From version 1.1.3 on, we started to support Rails 5.
124
190
 
125
- For Rails 7, please use version 1.2.0.
191
+ For Rails 7, please use version 1.2.2.
126
192
 
127
193
  ## License
128
194
 
data/lib/sql_handler.rb CHANGED
@@ -80,7 +80,7 @@ module RailsSimpleSearch
80
80
  format('A%02d', @joins[table_name][0])
81
81
  end
82
82
 
83
- def insert_join(base_class, asso_ref)
83
+ def insert_join(base_class, asso_ref, new_asso_chain)
84
84
  base_table = base_class.table_name
85
85
  asso_table = asso_ref.klass.table_name
86
86
 
@@ -89,7 +89,7 @@ module RailsSimpleSearch
89
89
  return unless @joins[asso_table].nil?
90
90
 
91
91
  @join_count += 1
92
- base_table_alias = @join_count < 2 ? base_table : table_name_to_alias(base_table)
92
+ base_table_alias = new_asso_chain ? base_table : table_name_to_alias(base_table)
93
93
  asso_table_alias = format('A%02d', @join_count)
94
94
 
95
95
  if asso_ref.belongs_to?
@@ -126,9 +126,11 @@ module RailsSimpleSearch
126
126
  field = association_fields.pop
127
127
 
128
128
  base_class = @model_class
129
+ new_asso_chain = true
129
130
  until association_fields.empty?
130
131
  association_fields[0] = base_class.reflect_on_association(association_fields[0].to_sym)
131
- insert_join(base_class, association_fields[0])
132
+ insert_join(base_class, association_fields[0], new_asso_chain)
133
+ new_asso_chain = false
132
134
  base_class = association_fields.shift.klass
133
135
  end
134
136
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-simple-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yi Zhang
@@ -32,7 +32,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
32
32
  requirements:
33
33
  - - ">="
34
34
  - !ruby/object:Gem::Version
35
- version: '0'
35
+ version: 2.7.0
36
36
  required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="