rails-simple-search 1.2.0 → 1.2.1

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +67 -1
  3. data/lib/sql_handler.rb +6 -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: 163e11eed3a64c95ca420d49bd6ade5d62bfeaeaff693b3b9586a387f89b7ae1
4
+ data.tar.gz: adb762be25ea2e2672d400667764728ca051df3dba97604610f4a5fe43f7d64b
5
5
  SHA512:
6
- metadata.gz: 890be5b99ab922d000adad63885a50a4bd678a1294b4915c32db93d808ac89774934626a0b73b3dd117728beb7d6b62a2df15c3b9353fb0113a80a05f11a57fa
7
- data.tar.gz: 2e72fd3aef31f0b0b3fdf7b34e80a5f1035cac36b869a62d543d178c5bee132a78caf00605bd1bc93e60e675b52ced76a9fffffb0d22be23acad1e65adf0269b
6
+ metadata.gz: d5f332112cbb55e5d966c4d0bba3044a19f56b0923ccc7f256b59fdcdee2d0e83d3708db5ffc718910b7fbe241c3f6480e5503be02ceb967603443ae5942dff7
7
+ data.tar.gz: 765ce655040b6488cb7137fa56afdc9fa3abcee70ca718f6972e0a9f8317ccb794cef9834b071638fc5a96689a22a217fd09c894787ba7d970b6df9a4a0b7ad1
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
data/lib/sql_handler.rb CHANGED
@@ -80,7 +80,8 @@ 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
+ debugger
84
85
  base_table = base_class.table_name
85
86
  asso_table = asso_ref.klass.table_name
86
87
 
@@ -89,7 +90,7 @@ module RailsSimpleSearch
89
90
  return unless @joins[asso_table].nil?
90
91
 
91
92
  @join_count += 1
92
- base_table_alias = @join_count < 2 ? base_table : table_name_to_alias(base_table)
93
+ base_table_alias = new_asso_chain ? base_table : table_name_to_alias(base_table)
93
94
  asso_table_alias = format('A%02d', @join_count)
94
95
 
95
96
  if asso_ref.belongs_to?
@@ -126,9 +127,11 @@ module RailsSimpleSearch
126
127
  field = association_fields.pop
127
128
 
128
129
  base_class = @model_class
130
+ new_asso_chain = true
129
131
  until association_fields.empty?
130
132
  association_fields[0] = base_class.reflect_on_association(association_fields[0].to_sym)
131
- insert_join(base_class, association_fields[0])
133
+ insert_join(base_class, association_fields[0], new_asso_chain)
134
+ new_asso_chain = false
132
135
  base_class = association_fields.shift.klass
133
136
  end
134
137
 
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.1
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
  - - ">="