rails-simple-search 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +4 -0
- data/lib/rails-simple-search.rb +3 -18
- data/lib/sql_handler.rb +5 -19
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d4ae3b14edf55ea1998b7f337e1ea6ab23038ef
|
4
|
+
data.tar.gz: 31ef250c3e06666d9929066b1d45c6d3d57ef462
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fc0b0e70fa4665031575f847fcea808b72bc73ee60d0252e668b0d69ed871ecc3147753a67a161a742040b3c954452d24d74053ca2b27824b38de469a8676c4
|
7
|
+
data.tar.gz: fc434f7363b1287d207ef8ef885f223080cc36c82fd6a9c06ef8a6b04a88624225dacbd886948972f8d048bfc19213d2970bff3015a8f6e1b6666e80fdcd23d6
|
data/README.rdoc
CHANGED
@@ -47,6 +47,9 @@ The following is how we implement this searching function with rails-simple-sear
|
|
47
47
|
<%=f.label :email %>
|
48
48
|
<%=f.text_field :email %>
|
49
49
|
|
50
|
+
<%=f.label "first name or last name" %>
|
51
|
+
<%=f.text_field "first_name_or_last_name" %>
|
52
|
+
|
50
53
|
<%=f.label :from_birth_date %>
|
51
54
|
<%=f.text_field :birth_date_greater_than_or_equal_to %>
|
52
55
|
|
@@ -78,6 +81,7 @@ From version 0.9.1 to 0.9.7, Rails 3 is supported.
|
|
78
81
|
From version 0.9.8 on, this gem started to support Rails 4. Version 0.9.8 is tested under Rails 4.1.1, and version 0.9.9 fixed an issue under
|
79
82
|
Rails 4.2.
|
80
83
|
|
84
|
+
From version 1.1.0 on, we started to support the "or" relation, e.g., we can use field like "first_name_or_last_name".
|
81
85
|
|
82
86
|
There is a real demo application which you can download and run immediately: https://github.com/yzhanginwa/demo_app_for_rails_simple_search
|
83
87
|
|
data/lib/rails-simple-search.rb
CHANGED
@@ -2,12 +2,7 @@ require_relative 'sql_handler'
|
|
2
2
|
|
3
3
|
module RailsSimpleSearch
|
4
4
|
DEFAULT_CONFIG = { :exact_match => [],
|
5
|
-
:or_separator => '_or_'
|
6
|
-
:paginate => true,
|
7
|
-
:page_name => 'page',
|
8
|
-
:offset => 0,
|
9
|
-
:limit => 1000,
|
10
|
-
:per_page => 20
|
5
|
+
:or_separator => '_or_'
|
11
6
|
}
|
12
7
|
|
13
8
|
module FixModelName
|
@@ -32,6 +27,8 @@ module RailsSimpleSearch
|
|
32
27
|
def initialize(model_class, criteria={}, config={})
|
33
28
|
@criteria = sanitize_criteria(criteria)
|
34
29
|
@config = DEFAULT_CONFIG.merge(config)
|
30
|
+
@config[:exact_match] = [@config[:exact_match]] unless @config[:exact_match].respond_to?(:map!)
|
31
|
+
@config[:exact_match].map!{|em| em.to_s}
|
35
32
|
|
36
33
|
@model_class = (model_class.is_a?(Symbol) || model_class.is_a?(String))? model_class.to_s.camelize.constantize : model_class
|
37
34
|
load_database_handler(@model_class)
|
@@ -50,18 +47,6 @@ module RailsSimpleSearch
|
|
50
47
|
@count || 0
|
51
48
|
end
|
52
49
|
|
53
|
-
def pages
|
54
|
-
(count == 0)? 0 : (count * 1.0 / @config[:per_page]).ceil
|
55
|
-
end
|
56
|
-
|
57
|
-
def pages_for_select
|
58
|
-
(1..pages).to_a
|
59
|
-
end
|
60
|
-
|
61
|
-
def order=(str)
|
62
|
-
@order = str
|
63
|
-
end
|
64
|
-
|
65
50
|
def add_conditions(h={})
|
66
51
|
@criteria.merge!(h)
|
67
52
|
end
|
data/lib/sql_handler.rb
CHANGED
@@ -21,18 +21,7 @@ module RailsSimpleSearch
|
|
21
21
|
|
22
22
|
query = @model_class.joins(@joins_str)
|
23
23
|
query = query.where(@condition_group.to_ar_condition) unless @condition_group.empty?
|
24
|
-
|
25
|
-
if @config[:paginate]
|
26
|
-
@count = query.count
|
27
|
-
offset = [((@page || 0) - 1) * @config[:per_page], 0].max
|
28
|
-
limit = @config[:per_page]
|
29
|
-
else
|
30
|
-
offset = @config[:offset]
|
31
|
-
limit = @config[:limit]
|
32
|
-
end
|
33
|
-
|
34
|
-
query = query.order(@order) if @order
|
35
|
-
query.select("distinct #{@model_class.table_name}.*").offset(offset).limit(limit)
|
24
|
+
query.select("distinct #{@model_class.table_name}.*")
|
36
25
|
end
|
37
26
|
|
38
27
|
private
|
@@ -54,18 +43,14 @@ module RailsSimpleSearch
|
|
54
43
|
@condition_group.set_relation(:and)
|
55
44
|
|
56
45
|
@criteria.each do |key, value|
|
57
|
-
|
58
|
-
@page = value.to_i
|
59
|
-
@criteria[key] = @page
|
60
|
-
else
|
61
|
-
@condition_group.add(parse_attribute(key, value))
|
62
|
-
end
|
46
|
+
@condition_group.add(parse_attribute(key, value))
|
63
47
|
end
|
64
48
|
|
65
49
|
make_joins
|
66
50
|
end
|
67
51
|
|
68
52
|
def insert_condition(base_class, attribute, field, value)
|
53
|
+
debugger
|
69
54
|
name_hash = parse_field_name(field)
|
70
55
|
field = name_hash[:field_name]
|
71
56
|
operator = name_hash[:operator]
|
@@ -75,6 +60,7 @@ module RailsSimpleSearch
|
|
75
60
|
|
76
61
|
@conditions ||= []
|
77
62
|
column = base_class.columns_hash[field.to_s]
|
63
|
+
return nil unless column
|
78
64
|
|
79
65
|
if !column.text? && value.is_a?(String)
|
80
66
|
if column.respond_to?(:type_cast)
|
@@ -182,7 +168,7 @@ module RailsSimpleSearch
|
|
182
168
|
if leaf?
|
183
169
|
raise "It's not allowed to add child into leaf node"
|
184
170
|
end
|
185
|
-
@children << cg
|
171
|
+
@children << cg if cg
|
186
172
|
end
|
187
173
|
|
188
174
|
def set_relation(and_or)
|
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.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yi Zhang
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-02 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.
|