rooble 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 48792701842c670bc199dec1e90fc047aab59f7d
4
- data.tar.gz: a88e321916bd3e9c96b6c891ce29058555b52206
3
+ metadata.gz: 46e72546278f13c26a17692b00fd42913430bacb
4
+ data.tar.gz: 7027e46afefd2b51d5cceb2b1ba9884bebaf4475
5
5
  SHA512:
6
- metadata.gz: 3d7db4a396a694b043bbdeb60d5aaaa1c0aac32a8577c410ca1df963a84e1e1abee644b265dc36ebd08c79351f114e3d5ccaf02f2993859231ad2dbfe5eb06b4
7
- data.tar.gz: 15d4aa0ee364a70140363a277e0e0e354c5fdea107661eac6de01218e461a3558c6f8b80a7583b0d5cdab4885988a608a791ac45e132b35a2aa93c1f8ff3dda3
6
+ metadata.gz: 7f0924480a6a4b756fd8b8fd3e8971742ce0658d4a6c79c82f740da6c9101f6b27d8b018a87ec161887a45363763a9ed16d3cb16718cf9a5434dc2f769143e0d
7
+ data.tar.gz: 240d66f7efc39b7df4a86a4080346d7062cb7767706b90b61a2c2ba3f9c2344771906cd728acf4f33b1e2d680a1afc6202a325f1e6047fc6938f7c34c6e7aed2
data/README.md CHANGED
@@ -107,9 +107,9 @@ Model.search(fields, search_term, options={})
107
107
  There is an extra options hash that you can pass to this method, the options are:
108
108
 
109
109
  * `case_sensitive: false` whether you want to make the search case sensitive. Default is false. Note that this relies on the database engine defaults so if the column or table schema is set to be case insensitive it won't mater what you set here.
110
- * `match_type: all` type of match, `beginning`, `end`, `all` string or `none`. Default is all.
111
110
  * `include` an array/hash of symbols if you want to include other relations on the model. Same as with default rails include.
112
111
  * `join` an array/hash of symbols if you want to join other relations on the model. Same as with default rails join.
112
+ * `id_fields: nil` an array of strings with id fields that should be considered on the search. This is important because we cannot do `LIKE` searches on integer fields so if you want to search both for text fields and in ID fields this should be set
113
113
 
114
114
  **Examples**
115
115
 
@@ -129,13 +129,15 @@ Searching for substrings:
129
129
 
130
130
  Searching for patterns:
131
131
 
132
+ You can use the _percent_ sign to do pattern matching just like in SQL given that the database supports it of course:
133
+
132
134
  ``` ruby
133
- State.search("name", "Ma", match_type: :beginning)
135
+ State.search("name", "Ma%")
134
136
  # => Yields 3 results where the value starts with Ma: Maine, Maryland and Massachusetts
135
137
  ```
136
138
 
137
139
  ``` ruby
138
- State.search("name", "Ma", match_type: :end)
140
+ State.search("name", "%Ma", match_type: :end)
139
141
  # => Yields 2 results where the value ends with ma: Alabama, Oklahoma
140
142
  ```
141
143
 
@@ -1,3 +1,3 @@
1
1
  module Rooble
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/rooble.rb CHANGED
@@ -72,13 +72,12 @@ module Rooble
72
72
  model = self
73
73
  end
74
74
 
75
- search_beginning = "%"
76
- search_end = "%"
77
75
  fields = [].push(fields) unless fields.is_a? Array
78
76
  search_values = []
79
77
  query = ''
80
78
  case_sensitive = false
81
79
  or_cond = ''
80
+ id_fields = ['id']
82
81
 
83
82
  if options.has_key? :case_sensitive
84
83
  if options[:case_sensitive]
@@ -86,6 +85,10 @@ module Rooble
86
85
  end
87
86
  end
88
87
 
88
+ if options.has_key? :id_fields
89
+ id_fields << options[:id_fields].collect { |id| id.downcase }
90
+ end
91
+
89
92
  fields.each_with_index do |field,index|
90
93
  # set the OR if we have more than one field
91
94
  if index > 0
@@ -94,29 +97,13 @@ module Rooble
94
97
 
95
98
  # lets find out if we are looking for the ID, we can't
96
99
  # use like for integers so we use equality instead
97
- if field.downcase == "id"
98
- # check that the serch term is actually a number
100
+ if id_fields.include? field.downcase
101
+ # check that the search term is actually a number
102
+ next unless search_term.to_i > 0
103
+
99
104
  operator = "="
100
105
  search_values.push(search_term.to_i)
101
106
  else
102
-
103
- # set the type of search wether just beginning of string
104
- # the end or as a whole
105
- if options.has_key? :match_type
106
- case options[:match_type].to_s
107
- when "beginning"
108
- search_beginning = nil
109
- when "end"
110
- search_end = nil
111
- when "all"
112
- search_beginning = "%"
113
- search_end = "%"
114
- when "none"
115
- search_beginning = nil
116
- search_end = nil
117
- end
118
- end
119
-
120
107
  # set whether we want case sensitive search
121
108
  case ActiveRecord::Base.connection.adapter_name
122
109
  when "PostgreSQL"
@@ -134,7 +121,7 @@ module Rooble
134
121
 
135
122
  # downcase the search term if we are doing case insensitive search so
136
123
  # the value of downcasing the column matches
137
- search_values.push("#{search_beginning}#{search_value}#{search_end}")
124
+ search_values.push("#{search_value}")
138
125
  end
139
126
 
140
127
  query += " #{or_cond} #{field} #{operator} ? "
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rooble
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gustavo Rubio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-11 00:00:00.000000000 Z
11
+ date: 2016-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport