simple-search 0.10.3 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -13,26 +13,54 @@ In your Gemfile:
13
13
 
14
14
  gem "simple-search" # Last officially released gem
15
15
 
16
+ In your config/environment.rb
17
+
18
+ require 'simple_search'
19
+
16
20
  In your controller:
17
21
 
18
22
  def index
19
- @posts = Post.simplesearch(params)
20
- # or, @posts = Post.simplesearch(params[:ss]) # when group options to "ss"
21
- # or, @posts = Post.simplesearch(:id_gt=>1,:id_lt=>3)
23
+ @posts = Post.select('posts.*, comments.id as comment_id, comments.body as comment').joins(:comments)
24
+ @posts = @posts.simplesearch(params)
25
+ render :index
22
26
  end
23
27
 
24
28
  In your view:
25
29
 
26
- <%= form_tag('/posts') do -%>
27
- <%= text_field_tag 'id_gt' %>
28
- <%= text_field_tag 'id_lt' %>
29
- .....
30
- <%= submit_tag %>
30
+ <%= form_tag('/posts', :method=>:GET) do -%>
31
+ ID &gt; <%= text_field_tag 'id_gt', params[:id_gt] %> <br/>
32
+ Subject contains <%= text_field_tag 'subject_ct', params[:subject_ct] %> <br/>
33
+ <%= submit_tag "Search" %>
31
34
  <% end -%>
32
35
 
33
- <%=order_link(:id, "Order by ID" )%>
36
+ <style>
37
+ #pages a {border:1px solid grey; display:inline-block; min-width:13px; text-align:center; text-decoration: none}
38
+ #pages a:first-child, #pages a:last-child {background-color:grey}
39
+ #pages a.current {color:white; background-color:#333}
40
+ </style>
41
+ <div id="pages">
42
+ Pages : <%=raw page_urls(@posts).join(" ") %>
43
+ </div>
44
+
45
+ <table>
46
+ <tr>
47
+ <th><%=order_link(:id)%></th>
48
+ <th><%=order_link(:subject)%></th>
49
+ <th><%=order_link(:body)%></th>
50
+ <th><%=order_link(:comment_id)%></th>
51
+ <th><%=order_link(:comment)%></th>
52
+ </tr>
53
+ <% @posts.each do |post| %>
54
+ <tr>
55
+ <td><%=h post.id %></td>
56
+ <td><%=h post.subject %></td>
57
+ <td><%=h post.body %></td>
58
+ <td><%=h post.comment_id %></td>
59
+ <td><%=h post.comment %></td>
60
+ </tr>
61
+ <% end %>
62
+ </table>
34
63
 
35
- <%=page_urls(@posts)%>
36
64
 
37
65
  === Search postfixes
38
66
 
@@ -43,15 +71,15 @@ In your view:
43
71
  * _ge, greater or equal to, <=
44
72
  * _in, includes, IN
45
73
  i.e., &id_in=1,2,3
46
- * _bt(_between), between, BETWEEN
74
+ * _bt (alias of _between), between, BETWEEN
47
75
  i.e., &id_between=1,3
48
- * _sw(_startswith), starts with, LIKE 'key%' (
49
- * _ew(_endsswith), ends with, LIKE '%key'
50
- * _ct(_contains,_like), contains, LIKE '%key%'
51
- * _nc(_notcontains,_notlike), not contains, NOT LIKE '%key%'
76
+ * _sw (alias of _startswith), starts with, LIKE 'key%' (
77
+ * _ew (alias of _endsswith), ends with, LIKE '%key'
78
+ * _ct (alias of _contains,_like), contains, LIKE '%key%'
79
+ * _nc (alias of _notcontains,_notlike), not contains, NOT LIKE '%key%'
52
80
  * _is, IS
53
81
  i.e., &id_is=null
54
- * _it(_isnot), IS NOT
82
+ * _it (alias of _isnot), IS NOT
55
83
 
56
84
  === Sorting your result
57
85
  * order_by
@@ -63,6 +91,10 @@ In your view:
63
91
  * page_by, number of rows in a page
64
92
  i.e. &page_by=10
65
93
 
94
+ === Grouping
95
+ * group_by, column to group
96
+ i.e. &group_by=posts.id
97
+
66
98
  == Contributing to simple-search
67
99
 
68
100
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.10.3
1
+ 0.11.0
@@ -37,18 +37,14 @@ module SimpleSearch
37
37
  params.each do |key,value|
38
38
  if ["group_by","page_by","page", "order_by"].include?(key)
39
39
  other_params[key]=value
40
- else
40
+ elsif value != ''
41
41
  matches = /(.*)_([a-z]+)$/.match(key)
42
- if matches.length==3
42
+ if matches!=nil && matches.length==3
43
43
  all, col, op = matches.to_a
44
44
  operands = SimpleSearch::OP_MAP.keys
45
45
  if operands.include?(op.to_sym)
46
46
  where_params[key]=value
47
- else
48
- logger.warning("SimpleSearch ignored #{key}. operand must be one of #{operands}") unless logger.nil?
49
47
  end
50
- else
51
- logger.warning("SimpleSearch ignored #{key}. it requires to end with _<operands>") unless logger.nil?
52
48
  end
53
49
  end
54
50
  end
@@ -95,9 +91,9 @@ module SimpleSearch
95
91
  end
96
92
 
97
93
  def with_ss_limit_offset(arel,params)
98
- page_num = (params['page'] || 1).to_i
99
- page_by = (params['page_by'] || 50).to_i
100
- arel = arel.limit(page_by).offset( (page_num-1)*page_by )
94
+ page_by = params['page_by'].to_i
95
+ page_num = params['page'] ? params['page'].to_i : ( page_by>0 ? 1 : 0 )
96
+ arel = arel.limit(page_by).offset( (page_num-1)*page_by ) if page_by>0 && page_num>0
101
97
  arel
102
98
  end
103
99
 
@@ -6,7 +6,7 @@ module SimpleSearch
6
6
  # <%= order_by :name %>
7
7
  # <%= order_by :name, 'Company Name' %>
8
8
  # <%= order_by :name, 'Company Name', :class=>'bold' %>
9
- def order_link(attr, name=nil, html_options={})
9
+ def order_link(order_to, name=nil, html_options={})
10
10
  raise Exception, "must be used in a view" unless (request.fullpath && request.parameters)
11
11
 
12
12
  # check if order_by is in an url array
@@ -18,27 +18,31 @@ module SimpleSearch
18
18
  if order_by_value
19
19
  order_by_col, current_order = order_by_value.split(" ")
20
20
  else
21
- order_by_col, current_order = attr, nil
21
+ order_by_col, current_order = order_to.to_s, ""
22
22
  end
23
+ order_to = order_to.to_s.strip
24
+ order_by_col.strip!
25
+ order_by_value.strip! unless order_by_value.nil?
23
26
 
24
27
  # determine next order
25
- next_order = {nil=>'asc', 'asc'=>'desc', 'desc'=>'asc'}[current_order]
28
+ next_order = order_to==order_by_col ? {'asc'=>'desc', 'desc'=>'asc'}[current_order] : 'asc'
26
29
 
27
30
  # build new url params
31
+ params = request.parameters.clone
28
32
  new_url_params = if order_arr_key
29
- old_arr = request.parameters[order_arr_key]
30
- new_arr = old_arr.merge(:order_by => "#{order_by_col} #{next_order}")
31
- request.parameters.merge(order_arr_key => new_arr)
32
- else
33
- request.parameters.merge(:order_by => "#{order_by_col} #{next_order}" )
33
+ old_arr = params[order_arr_key]
34
+ new_arr = old_arr.merge(:order_by => "#{order_to} #{next_order}")
35
+ params.merge(order_arr_key => new_arr)
36
+ else
37
+ params.merge(:order_by => "#{order_to} #{next_order}" )
34
38
  end
35
39
 
36
40
  # build link text
37
- name ||= attr.to_s.humanize.titleize.strip
38
- order_indicator = {nil=>'', 'asc'=>'&#9650;', 'desc'=>'&#9660;'}[current_order]
41
+ name ||= order_to.gsub(/_/," ").strip.titleize
42
+ order_indicator = order_to==order_by_col ? {'asc'=>'&#9650;', 'desc'=>'&#9660;'}[current_order] : ""
39
43
  link_text = "#{name} #{order_indicator}".strip
40
44
 
41
- link_to link_text, new_url_params, html_options
45
+ link_to raw(link_text), new_url_params, html_options
42
46
  end
43
47
 
44
48
  #
@@ -74,12 +78,12 @@ module SimpleSearch
74
78
  #
75
79
  # Returns a url params in hash for the page number given
76
80
  #
77
- def page_params(page_num, page_arr_key=nil)
81
+ def page_params(page_num, page_by, page_arr_key=nil)
78
82
  params = if page_arr_key
79
83
  old_arr = request.parameters[page_arr_key].with_indifferent_access
80
- request.parameters.merge(page_arr_key => old_arr.merge(:page => page_num) )
84
+ request.parameters.merge(page_arr_key => old_arr.merge(:page => page_num, :page_by => page_by ) )
81
85
  else
82
- request.parameters.merge(:page => page_num )
86
+ request.parameters.merge(:page => page_num, :page_by => page_by )
83
87
  end
84
88
  params.with_indifferent_access
85
89
  end
@@ -87,8 +91,9 @@ module SimpleSearch
87
91
  #
88
92
  # Returns page url params in hash with the page number as key
89
93
  #
90
- def page_url_params(arel, num_pages=5)
91
- page_props = page_properties(arel)
94
+ def page_url_params(arel, options={})
95
+ page_props = options[:page_props] || page_properties(arel)
96
+ num_pages = options[:num_pages] || 5
92
97
  num_bf_af = (num_pages-1)/2
93
98
  current_page = page_props[:current_page]
94
99
  last_page = page_props[:last_page]
@@ -100,7 +105,7 @@ module SimpleSearch
100
105
 
101
106
  page_url_params = {}
102
107
  for num in page_numbers
103
- page_url_params[num] = page_params(num, page_props[:page_arr_key])
108
+ page_url_params[num] = page_params(num, page_props[:rows_per_page], page_props[:page_arr_key])
104
109
  end
105
110
 
106
111
  page_url_params
@@ -109,11 +114,13 @@ module SimpleSearch
109
114
  def page_urls(arel, http_options={})
110
115
  page_urls = []
111
116
  prev_page = 0
112
- page_url_params(arel).each { |page, url_params|
117
+ page_props = page_properties(arel)
118
+ page_url_params(arel, :page_props => page_props ).each { |page, url_params|
113
119
  if page.to_i > (prev_page+1) # page number jumped up, need to have some indicator
114
120
  page_urls << "<span class='filler'>...</span>"
115
121
  end
116
- page_urls << link_to(page, url_params, http_options)
122
+ new_options = page_props[:current_page] == page ? http_options.merge({:class => 'current'}) : http_options
123
+ page_urls << link_to(page, url_params, new_options)
117
124
  prev_page = page
118
125
  }
119
126
  page_urls
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "simple-search"
8
- s.version = "0.10.3"
8
+ s.version = "0.11.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Allen Kim"]
@@ -14,30 +14,29 @@ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS 'post_likes'")
14
14
 
15
15
  # create tables
16
16
  ActiveRecord::Base.connection.create_table(:posts) do |t|
17
- t.string :subject
18
- t.text :body
17
+ t.string :subject
18
+ t.text :body
19
19
  end
20
20
  ActiveRecord::Base.connection.create_table(:comments) do |t|
21
- t.string :post_id
22
- t.text :body
23
- t.boolean :spam_flag
21
+ t.string :post_id
22
+ t.text :body
23
+ t.boolean :spam_flag
24
24
  end
25
25
  ActiveRecord::Base.connection.create_table(:post_likes) do |t|
26
- t.string :post_id
27
- t.boolean :like
26
+ t.string :post_id
27
+ t.boolean :like
28
28
  end
29
29
 
30
30
  # models
31
31
  class Post < ActiveRecord::Base
32
- has_many :comments
33
- has_many :post_likes
32
+ has_many :comments
33
+ has_many :post_likes
34
34
  end
35
35
  class Comment < ActiveRecord::Base
36
- belongs_to :post
37
- has_many :votes
36
+ belongs_to :post
38
37
  end
39
38
  class PostLike < ActiveRecord::Base
40
- belongs_to :post
39
+ belongs_to :post
41
40
  end
42
41
 
43
42
  # insert tables
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: simple-search
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.10.3
5
+ version: 0.11.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Allen Kim
@@ -152,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
152
  requirements:
153
153
  - - ">="
154
154
  - !ruby/object:Gem::Version
155
- hash: 1014388481783266956
155
+ hash: 1273856401880201172
156
156
  segments:
157
157
  - 0
158
158
  version: "0"