rails-simple-search 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README +32 -32
- data/lib/rails-simple-search.rb +14 -8
- metadata +3 -3
data/README
CHANGED
@@ -1,67 +1,67 @@
|
|
1
1
|
rails-simple-search
|
2
2
|
===================
|
3
3
|
|
4
|
-
rails-simple-search is a Ruby gem. It helps you quickly implement searching/filtering function for your web site.
|
5
|
-
|
6
|
-
|
4
|
+
rails-simple-search is a Ruby gem. It helps you quickly implement searching/filtering function for your web site. This plugin has paginating feature built in. If you're not looking for a full-text searching solution, this plugin will most probably satisfy all your searching requirement.
|
5
|
+
|
6
|
+
From time to time, I need to build pages to show a list of narrowed down records from a database table by giving some searching criteria on some columns of the table and/or of some referencing tables. Before I implemented this plugin, I usually do the searching in the following way:
|
7
7
|
|
8
|
-
From time to time, I need to build pages to show a list of narrowed down records from a database table by giving
|
9
|
-
some searching criteria on some columns of the table and/or of some referencing tables. Before I implemented this
|
10
|
-
plugin, I usually do the searching in the following way:
|
11
8
|
1) Use <%= form_tag %> to build a form in the view
|
12
9
|
2) Get the searching criteria from the params hash individually in the controller and put them into instance variable to be used in view
|
13
10
|
3) Build the SQL WHERE clause and sometimes the JOIN clause according to the values from the form
|
14
11
|
4) Run the find(:all, :conditions => [xxxxxx], :joins => "yyyyyy") with the WHERE and JOIN clauses
|
15
12
|
|
16
|
-
After having used this pattern a few times, I realized I could DRY it to make future coding of this kind of searching
|
17
|
-
much simpler. That's where the rails-simple-search plugin comes in.
|
13
|
+
After having used this pattern a few times, I realized I could DRY it to make future coding of this kind of searching much simpler. That's where the rails-simple-search plugin comes in.
|
18
14
|
|
19
|
-
Now implementing the searching/filter page is a lot easier for me. You're see how easy it is by taking a look at the
|
20
|
-
following example. I may give more examples in the future when I have some spare time.
|
15
|
+
Now implementing the searching/filter page is a lot easier for me. You're see how easy it is by taking a look at the following example. I may give more examples in the future when I have some spare time.
|
21
16
|
|
22
17
|
|
23
18
|
Example
|
24
19
|
=======
|
25
20
|
|
26
|
-
Let's suppose we have models of User, Address, Post and Comment.
|
27
|
-
User model has_one address and has_many posts; Post model has_many comments
|
28
|
-
We'd like to find users according to any combination of the following criteria:
|
21
|
+
Let's suppose we have models of User, Address, Post and Comment. User model has_one address and has_many posts; Post model has_many comments. We'd like to search for users according to any combination of the following criteria:
|
29
22
|
|
30
|
-
|
31
|
-
|
32
|
-
|
23
|
+
1) part of the user's email addrsss
|
24
|
+
2) state of the user's address
|
25
|
+
3) part of the name of any authors who commented the user's any posts
|
33
26
|
|
34
27
|
The following is how we implement this searching function with rails-simple-search:
|
35
28
|
|
36
29
|
Code in model (app/model/search.rb):
|
37
30
|
|
38
|
-
|
39
|
-
|
31
|
+
class Search < RailsSimpleSearch::Base
|
32
|
+
end
|
40
33
|
|
41
34
|
Code in controller:
|
42
35
|
|
43
|
-
|
44
|
-
|
36
|
+
@search = Search.new(User, params[:search])
|
37
|
+
@search.order = 'email' # optional
|
38
|
+
@users = @search.run
|
45
39
|
|
46
40
|
Code in views:
|
47
41
|
|
48
|
-
|
42
|
+
<% form_for @search do |f| %>
|
43
|
+
|
44
|
+
<%=f.label :email %>
|
45
|
+
<%=f.text_field :email %>
|
46
|
+
|
47
|
+
<%=f.label :state%>
|
48
|
+
<%=f.select "address.state_id", [['AL', 1], ...] %> <!-- address is an association of model User -->
|
49
49
|
|
50
|
-
|
51
|
-
|
50
|
+
<%=f.label :post%>
|
51
|
+
<%=f.text_field "posts.comments.author" %> <!-- the associations could go even deeper, isn't it POWERFUL? -->
|
52
52
|
|
53
|
-
|
54
|
-
|
53
|
+
<%=f.submit %>
|
54
|
+
<% end %>
|
55
55
|
|
56
|
-
|
57
|
-
|
56
|
+
<% @users.each do |user| %>
|
57
|
+
<%= # show the attributes of user %>
|
58
|
+
<% end %>
|
58
59
|
|
59
|
-
|
60
|
-
|
60
|
+
Note
|
61
|
+
====
|
62
|
+
For rails 2.x.x applications, you might want to use the version 0.9.0. Since version 0.9.1, this gem started to support rails 3 now.
|
61
63
|
|
62
|
-
|
63
|
-
<%= # show the attributes of user %>
|
64
|
-
<% end %>
|
64
|
+
There is a real demo application which you can download and run immediately: https://github.com/yzhanginwa/demo_app_for_rails_simple_search
|
65
65
|
|
66
66
|
|
67
67
|
Copyright (c) 2012 [Yi Zhang], released under the MIT license
|
data/lib/rails-simple-search.rb
CHANGED
@@ -30,7 +30,7 @@ module RailsSimpleSearch
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def pages
|
33
|
-
(count == 0)? 0 : (count / @config[:per_page].
|
33
|
+
(count == 0)? 0 : (count * 1.0 / @config[:per_page]).ceil
|
34
34
|
end
|
35
35
|
|
36
36
|
def pages_for_select
|
@@ -40,6 +40,10 @@ module RailsSimpleSearch
|
|
40
40
|
def add_conditions(h={})
|
41
41
|
@criteria.merge!(h)
|
42
42
|
end
|
43
|
+
|
44
|
+
def order=(str)
|
45
|
+
@order = str
|
46
|
+
end
|
43
47
|
|
44
48
|
def conditions
|
45
49
|
run_criteria
|
@@ -51,7 +55,7 @@ module RailsSimpleSearch
|
|
51
55
|
@joins_str
|
52
56
|
end
|
53
57
|
|
54
|
-
def run
|
58
|
+
def run
|
55
59
|
run_criteria
|
56
60
|
if @config[:paginate]
|
57
61
|
@count = @model_class.count({:select => "distinct #{@model_class.table_name}.#{@model_class.primary_key}",
|
@@ -65,12 +69,14 @@ module RailsSimpleSearch
|
|
65
69
|
limit = @config[:limit]
|
66
70
|
end
|
67
71
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
72
|
+
execute_hash = {:select => "distinct #{@model_class.table_name}.*",
|
73
|
+
:conditions => @conditions,
|
74
|
+
:joins => @joins_str,
|
75
|
+
:offset => offset,
|
76
|
+
:limit => limit
|
77
|
+
}
|
78
|
+
execute_hash[:order] = @order if @order
|
79
|
+
@model_class.all(execute_hash)
|
74
80
|
end
|
75
81
|
|
76
82
|
private
|
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: 0.9.
|
4
|
+
version: 0.9.2
|
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: 2012-
|
12
|
+
date: 2012-06-05 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: this version starts to support rails 3
|
15
15
|
email: yzhang.wa@gmail.com
|
@@ -39,7 +39,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
39
|
version: '0'
|
40
40
|
requirements: []
|
41
41
|
rubyforge_project:
|
42
|
-
rubygems_version: 1.8.
|
42
|
+
rubygems_version: 1.8.24
|
43
43
|
signing_key:
|
44
44
|
specification_version: 3
|
45
45
|
summary: A very simple and light get to quick build search function in rails
|