barely_searchable 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +20 -1
- data/lib/barely_searchable/model_injections.rb +57 -10
- data/lib/barely_searchable/railtie.rb +0 -2
- data/lib/barely_searchable/version.rb +1 -1
- metadata +24 -40
data/README.md
CHANGED
@@ -10,7 +10,7 @@ It is a terrible and slow search engine....but it'll probably work for a handful
|
|
10
10
|
|
11
11
|
Add this line to your application's Gemfile: `gem 'barely_searchable'`
|
12
12
|
|
13
|
-
##
|
13
|
+
## Quickstart
|
14
14
|
|
15
15
|
In your model:
|
16
16
|
|
@@ -29,6 +29,25 @@ Now elsewhere in your application:
|
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
32
|
+
## More details:
|
33
|
+
|
34
|
+
In your models, you can specify conditions on the search parameters to exclude them from searches. This is useful for search query optimization or selective searches.
|
35
|
+
|
36
|
+
```
|
37
|
+
class Order < ActiveRecord::Base
|
38
|
+
#search these columns always
|
39
|
+
searches_on :user_id, :shipping_city
|
40
|
+
|
41
|
+
# Will only search these columns when the type-casted value isn't zero
|
42
|
+
searches_on :subtotal, :tax, :total, unless: Proc.new{|value| value == 0}
|
43
|
+
|
44
|
+
# Only search this column if the query is longer thahn 3 characters
|
45
|
+
searches_on :email, if: Proc.new{|value| value.length > 3}
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
Search content is cast to the column data type before calling if/unless blocks.
|
50
|
+
|
32
51
|
## Contributing
|
33
52
|
|
34
53
|
1. Fork it
|
@@ -1,35 +1,56 @@
|
|
1
|
-
require 'ruby-debug'
|
2
|
-
|
3
1
|
module BarelySearchable
|
4
2
|
module ModelInjections
|
5
3
|
|
6
4
|
module ClassMethods
|
7
|
-
def searches_on *fields
|
5
|
+
def searches_on *fields, conditions
|
6
|
+
if conditions.kind_of? Symbol
|
7
|
+
fields.push conditions
|
8
|
+
conditions = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
conditions = {} if conditions.nil?
|
12
|
+
|
8
13
|
if @searches.nil?
|
9
14
|
init_searches_on
|
10
15
|
end
|
11
16
|
|
12
|
-
|
13
|
-
@searches.
|
17
|
+
fields.map! {|field| [field, conditions]}
|
18
|
+
@searches.push *fields
|
14
19
|
end
|
15
20
|
|
16
21
|
def init_searches_on
|
17
22
|
@searches = []
|
18
23
|
end
|
19
24
|
|
25
|
+
def searches
|
26
|
+
@searches
|
27
|
+
end
|
28
|
+
|
20
29
|
def search query, limit = nil
|
21
30
|
where = '0'
|
22
|
-
like = '%' + query.to_s.gsub(' ','%') + '%'
|
23
31
|
|
24
|
-
@searches.each do |
|
25
|
-
field =
|
32
|
+
@searches.each do |search_field|
|
33
|
+
field = search_field[0].to_s
|
34
|
+
hash = search_field[1]
|
35
|
+
|
36
|
+
#check to make sure the field actually exists on the table
|
26
37
|
unless self.columns_hash.include? field
|
27
38
|
raise NameError, "#{field} is not a valid column on #{self}"
|
28
39
|
end
|
29
40
|
|
30
|
-
|
31
|
-
|
41
|
+
#cast the query to the right data type for this column
|
42
|
+
col_query = cast_to_column(query, field)
|
43
|
+
|
44
|
+
#check for an if-condition
|
45
|
+
next if ! hash[:if].nil? && ! hash[:if].call(col_query)
|
46
|
+
|
47
|
+
#check for an unless condition
|
48
|
+
next if ! hash[:unless].nil? && hash[:unless].call(col_query)
|
49
|
+
|
50
|
+
if equality_columns.include? column_to_type(field)
|
51
|
+
where += ' or ' + self.arel_table[field].eq( col_query ).to_sql
|
32
52
|
else
|
53
|
+
like = '%' + col_query.to_s.gsub(' ','%') + '%'
|
33
54
|
where += ' or ' + self.arel_table[field].matches( like ).to_sql
|
34
55
|
end
|
35
56
|
end
|
@@ -41,6 +62,32 @@ module BarelySearchable
|
|
41
62
|
end
|
42
63
|
|
43
64
|
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def cast_to_column data, column
|
69
|
+
case column_to_type(column)
|
70
|
+
when :decimal, :float
|
71
|
+
data.to_f
|
72
|
+
when :integer
|
73
|
+
data.to_i
|
74
|
+
when :string, :text, :binary, :date, :datetime, :time, :timestamp
|
75
|
+
data.to_s
|
76
|
+
when :boolean
|
77
|
+
!!data
|
78
|
+
else
|
79
|
+
data
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def column_to_type column
|
84
|
+
self.columns_hash[ column ].type
|
85
|
+
end
|
86
|
+
|
87
|
+
def equality_columns
|
88
|
+
[:integer, :decimal, :float, :boolean]
|
89
|
+
end
|
90
|
+
|
44
91
|
end
|
45
92
|
|
46
93
|
def self.included to
|
metadata
CHANGED
@@ -1,33 +1,23 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: barely_searchable
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
version: 0.0.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Robert L. Carpenter
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2012-02-10 00:00:00 -07:00
|
18
|
-
default_executable:
|
12
|
+
date: 2012-09-25 00:00:00.000000000 Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
14
|
description: Adds a very rudimentary .search() function to your activerecord models
|
22
|
-
email:
|
15
|
+
email:
|
23
16
|
- codemonkey@robacarp.com
|
24
17
|
executables: []
|
25
|
-
|
26
18
|
extensions: []
|
27
|
-
|
28
19
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
20
|
+
files:
|
31
21
|
- .gitignore
|
32
22
|
- Gemfile
|
33
23
|
- LICENSE
|
@@ -38,35 +28,29 @@ files:
|
|
38
28
|
- lib/barely_searchable/model_injections.rb
|
39
29
|
- lib/barely_searchable/railtie.rb
|
40
30
|
- lib/barely_searchable/version.rb
|
41
|
-
has_rdoc: true
|
42
31
|
homepage: https://github.com/robacarp/barely_searchable
|
43
32
|
licenses: []
|
44
|
-
|
45
33
|
post_install_message:
|
46
34
|
rdoc_options: []
|
47
|
-
|
48
|
-
require_paths:
|
35
|
+
require_paths:
|
49
36
|
- lib
|
50
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
requirements:
|
59
|
-
- -
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
|
62
|
-
- 0
|
63
|
-
version: "0"
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
64
49
|
requirements: []
|
65
|
-
|
66
50
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.
|
51
|
+
rubygems_version: 1.8.22
|
68
52
|
signing_key:
|
69
53
|
specification_version: 3
|
70
|
-
summary: Add quick and dirty search functionality to your model layer without any
|
54
|
+
summary: Add quick and dirty search functionality to your model layer without any
|
55
|
+
indexing service
|
71
56
|
test_files: []
|
72
|
-
|