rails-simple-search 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,49 +1,58 @@
1
- rails-simple-search
2
- ===================
1
+ = rails-simple-search
3
2
 
4
3
  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
4
 
6
5
  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
6
 
8
- 1) Use <%= form_tag %> to build a form in the view
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
10
- 3) Build the SQL WHERE clause and sometimes the JOIN clause according to the values from the form
11
- 4) Run the find(:all, :conditions => [xxxxxx], :joins => "yyyyyy") with the WHERE and JOIN clauses
7
+ 1. Use <%= form_tag %> to build a form in the view
8
+ 2. Get the searching criteria from the params hash individually in the controller and put them into instance variable to be used in view
9
+ 3. Build the SQL WHERE clause and sometimes the JOIN clause according to the values from the form
10
+ 4. Run the find(:all, :conditions => [xxxxxx], :joins => "yyyyyy") with the WHERE and JOIN clauses
12
11
 
13
12
  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.
14
13
 
15
14
  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.
16
15
 
17
16
 
18
- Example
19
- =======
17
+ == Example
20
18
 
21
19
  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:
22
20
 
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
21
+ * part of the user's email addrsss
22
+ * range of the user's birth date
23
+ * state of the user's address
24
+ * part of the name of any authors who commented the user's any posts
26
25
 
27
26
  The following is how we implement this searching function with rails-simple-search:
28
27
 
29
- Code in model (app/model/search.rb):
28
+ 1. Include gem into Gemfile
29
+
30
+ gem 'rails-simple-search'
31
+
32
+ 2. Code in model (app/model/search.rb):
30
33
 
31
34
  class Search < RailsSimpleSearch::Base
32
35
  end
33
36
 
34
- Code in controller:
37
+ 3. Code in controller:
35
38
 
36
39
  @search = Search.new(User, params[:search])
37
40
  @search.order = 'email' # optional
38
41
  @users = @search.run
39
42
 
40
- Code in views:
43
+ 4. Code in views:
41
44
 
42
- <% form_for @search do |f| %>
45
+ <% form_for @search, url => "/xxxxxx" do |f| %>
43
46
 
44
47
  <%=f.label :email %>
45
48
  <%=f.text_field :email %>
46
49
 
50
+ <%=f.label :from_birth_date %>
51
+ <%=f.text_field :birth_date_greater_than_or_equal_to %>
52
+
53
+ <%=f.label :to_age %>
54
+ <%=f.text_field :birth_date_less_than_or_equal_to %>
55
+
47
56
  <%=f.label :state%>
48
57
  <%=f.select "address.state_id", [['AL', 1], ...] %> <!-- address is an association of model User -->
49
58
 
@@ -57,11 +66,15 @@ Code in views:
57
66
  <%= # show the attributes of user %>
58
67
  <% end %>
59
68
 
60
- Note
61
- ====
69
+ 5. Add route for the post to url "/xxxxxx" (config/route.rb)
70
+ match "/xxxxxx" => "yyyyyyy#zzzzzz"
71
+
72
+ == Note
73
+
62
74
  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. The latest version 0.9.4 is tested under rails 3.2.6.
63
75
 
64
76
  There is a real demo application which you can download and run immediately: https://github.com/yzhanginwa/demo_app_for_rails_simple_search
65
77
 
78
+ == License
66
79
 
67
80
  Copyright (c) 2012 [Yi Zhang], released under the MIT license
@@ -16,7 +16,7 @@ module RailsSimpleSearch
16
16
  end
17
17
  end
18
18
 
19
- def initialize(model_class, criteria, config={})
19
+ def initialize(model_class, criteria={}, config={})
20
20
  @model_class = (model_class.is_a?(Symbol) || model_class.is_a?(String))? model_class.to_s.camelize.constantize : model_class
21
21
  @table_name = @model_class.table_name
22
22
  @criteria = criteria.nil? ? {} : criteria
@@ -92,9 +92,9 @@ module RailsSimpleSearch
92
92
 
93
93
  def make_joins
94
94
  @joins_str = ''
95
- @joins = @joins.values
96
- @joins.sort! {|a,b| a[0] <=> b[0]}
97
- @joins.each do |j|
95
+ joins = @joins.values
96
+ joins.sort! {|a,b| a[0] <=> b[0]}
97
+ joins.each do |j|
98
98
  table = j[1]
99
99
  constrain = j[2]
100
100
  @joins_str << " inner join #{table} on #{constrain}"
@@ -102,7 +102,7 @@ module RailsSimpleSearch
102
102
  end
103
103
 
104
104
  def run_criteria
105
- return @conditions unless @conditions.nil?
105
+ return unless @conditions.nil?
106
106
  @criteria.each do |key, value|
107
107
  if @config[:page_name].to_s == key.to_s
108
108
  @page = value.to_i
@@ -115,7 +115,30 @@ module RailsSimpleSearch
115
115
  make_joins
116
116
  end
117
117
 
118
+ def parse_field_name(name)
119
+ result = {}
120
+ if name =~ /^(.*)?((_(greater|less)_than)(_or_equal_to)?)$/
121
+ result[:field_name] = $1
122
+ if $4 == 'greater'
123
+ result[:operator] = ">"
124
+ else
125
+ result[:operator] = "<"
126
+ end
127
+ if $5
128
+ result[:operator] << "="
129
+ end
130
+ else
131
+ result[:filed_name] = name
132
+ end
133
+ result
134
+ end
135
+
136
+
118
137
  def insert_condition(base_class, attribute, field, value)
138
+ name_hash = parse_field_name(field)
139
+ field = name_hash[:field_name]
140
+ operator = name_hash[:operator]
141
+
119
142
  table = base_class.table_name
120
143
  key = "#{table}.#{field}"
121
144
 
@@ -129,6 +152,8 @@ module RailsSimpleSearch
129
152
 
130
153
  if value.nil?
131
154
  verb = 'is'
155
+ elsif operator
156
+ verb = operator
132
157
  elsif column.text? && ! @config[:exact_match].include?((@table_name == table)? field : key)
133
158
  verb = 'like'
134
159
  value = "%#{value}%"
metadata CHANGED
@@ -1,68 +1,47 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: rails-simple-search
3
- version: !ruby/object:Gem::Version
4
- hash: 51
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.5
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 9
9
- - 4
10
- version: 0.9.4
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Yi Zhang
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-11-20 00:00:00 -08:00
19
- default_executable:
12
+ date: 2013-01-29 00:00:00.000000000 Z
20
13
  dependencies: []
21
-
22
- description: rails-simple-search is a light and easy to use gem. It could help developers quickly build a search page.
14
+ description: rails-simple-search is a light and easy to use gem. It could help developers
15
+ quickly build a search page.
23
16
  email: yzhang.wa@gmail.com
24
17
  executables: []
25
-
26
18
  extensions: []
27
-
28
19
  extra_rdoc_files: []
29
-
30
- files:
31
- - README
20
+ files:
21
+ - README.rdoc
32
22
  - lib/rails-simple-search.rb
33
- has_rdoc: true
34
23
  homepage: http://github.com/yzhanginwa/rails-simple-search
35
24
  licenses: []
36
-
37
25
  post_install_message:
38
26
  rdoc_options: []
39
-
40
- require_paths:
27
+ require_paths:
41
28
  - lib
42
- required_ruby_version: !ruby/object:Gem::Requirement
29
+ required_ruby_version: !ruby/object:Gem::Requirement
43
30
  none: false
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- hash: 3
48
- segments:
49
- - 0
50
- version: "0"
51
- required_rubygems_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
36
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
- version: "0"
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
60
41
  requirements: []
61
-
62
42
  rubyforge_project:
63
- rubygems_version: 1.6.2
43
+ rubygems_version: 1.8.24
64
44
  signing_key:
65
45
  specification_version: 3
66
46
  summary: A very simple and light get to quick build search function in rails
67
47
  test_files: []
68
-